blob: 386b512189b2e673aef08da294de9ff7dd23d33e [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);
Steve French52ace1e2016-09-22 19:23:56 -0500253
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400254 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
255 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500256
257 if (tcon->use_persistent)
258 cifs_reopen_persistent_handles(tcon);
259
Joe Perchesf96637b2013-05-04 22:12:25 -0500260 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400261 if (rc)
262 goto out;
263 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400264out:
265 /*
266 * Check if handle based operation so we know whether we can continue
267 * or not without returning to caller to reset file handle.
268 */
269 /*
270 * BB Is flush done by server on drop of tcp session? Should we special
271 * case it and skip above?
272 */
273 switch (smb2_command) {
274 case SMB2_FLUSH:
275 case SMB2_READ:
276 case SMB2_WRITE:
277 case SMB2_LOCK:
278 case SMB2_IOCTL:
279 case SMB2_QUERY_DIRECTORY:
280 case SMB2_CHANGE_NOTIFY:
281 case SMB2_QUERY_INFO:
282 case SMB2_SET_INFO:
283 return -EAGAIN;
284 }
285 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400286 return rc;
287}
288
289/*
290 * Allocate and return pointer to an SMB request hdr, and set basic
291 * SMB information in the SMB header. If the return code is zero, this
292 * function must have filled in request_buf pointer.
293 */
294static int
295small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
296 void **request_buf)
297{
298 int rc = 0;
299
300 rc = smb2_reconnect(smb2_command, tcon);
301 if (rc)
302 return rc;
303
304 /* BB eventually switch this to SMB2 specific small buf size */
305 *request_buf = cifs_small_buf_get();
306 if (*request_buf == NULL) {
307 /* BB should we add a retry in here if not a writepage? */
308 return -ENOMEM;
309 }
310
311 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
312
313 if (tcon != NULL) {
314#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400315 uint16_t com_code = le16_to_cpu(smb2_command);
316 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400317#endif
318 cifs_stats_inc(&tcon->num_smbs_sent);
319 }
320
321 return rc;
322}
323
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500324#ifdef CONFIG_CIFS_SMB311
325/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
326#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
327
328
329#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
330#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
331
332static void
333build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
334{
335 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
336 pneg_ctxt->DataLength = cpu_to_le16(38);
337 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
338 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
339 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
340 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
341}
342
343static void
344build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
345{
346 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
347 pneg_ctxt->DataLength = cpu_to_le16(6);
348 pneg_ctxt->CipherCount = cpu_to_le16(2);
349 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
350 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
351}
352
353static void
354assemble_neg_contexts(struct smb2_negotiate_req *req)
355{
356
357 /* +4 is to account for the RFC1001 len field */
358 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
359
360 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
361 /* Add 2 to size to round to 8 byte boundary */
362 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
363 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
364 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
365 req->NegotiateContextCount = cpu_to_le16(2);
366 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
367 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
368}
369#else
370static void assemble_neg_contexts(struct smb2_negotiate_req *req)
371{
372 return;
373}
374#endif /* SMB311 */
375
376
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400377/*
378 *
379 * SMB2 Worker functions follow:
380 *
381 * The general structure of the worker functions is:
382 * 1) Call smb2_init (assembles SMB2 header)
383 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
384 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
385 * 4) Decode SMB2 command specific fields in the fixed length area
386 * 5) Decode variable length data area (if any for this SMB2 command type)
387 * 6) Call free smb buffer
388 * 7) return
389 *
390 */
391
392int
393SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
394{
395 struct smb2_negotiate_req *req;
396 struct smb2_negotiate_rsp *rsp;
397 struct kvec iov[1];
398 int rc = 0;
399 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400400 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400401 int blob_offset, blob_length;
402 char *security_blob;
403 int flags = CIFS_NEG_OP;
404
Joe Perchesf96637b2013-05-04 22:12:25 -0500405 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400406
Jeff Layton3534b852013-05-24 07:41:01 -0400407 if (!server) {
408 WARN(1, "%s: server is NULL!\n", __func__);
409 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400410 }
411
412 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
413 if (rc)
414 return rc;
415
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400416 req->hdr.SessionId = 0;
417
Steve Frenche4aa25e2012-10-01 12:26:22 -0500418 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400419
Steve Frenche4aa25e2012-10-01 12:26:22 -0500420 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
421 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400422
423 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400424 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500425 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400426 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500427 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400428 else
429 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400430
Steve Frenche4aa25e2012-10-01 12:26:22 -0500431 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400432
Steve French3c5f9be12014-05-13 13:37:45 -0700433 /* ClientGUID must be zero for SMB2.02 dialect */
434 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
435 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500436 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700437 memcpy(req->ClientGUID, server->client_guid,
438 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500439 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
440 assemble_neg_contexts(req);
441 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400442 iov[0].iov_base = (char *)req;
443 /* 4 for rfc1002 length field */
444 iov[0].iov_len = get_rfc1002_length(req) + 4;
445
446 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
447
448 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
449 /*
450 * No tcon so can't do
451 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
452 */
453 if (rc != 0)
454 goto neg_exit;
455
Joe Perchesf96637b2013-05-04 22:12:25 -0500456 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400457
Steve Frenche4aa25e2012-10-01 12:26:22 -0500458 /* BB we may eventually want to match the negotiated vs. requested
459 dialect, even though we are only requesting one at a time */
460 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500461 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500462 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500463 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500464 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500465 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500466 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
467 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600468#ifdef CONFIG_CIFS_SMB311
469 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
470 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
471#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400472 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500473 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500474 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400475 rc = -EIO;
476 goto neg_exit;
477 }
478 server->dialect = le16_to_cpu(rsp->DialectRevision);
479
Jeff Laytone598d1d82013-05-26 07:00:59 -0400480 /* SMB2 only has an extended negflavor */
481 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400482 /* set it to the maximum buffer size value we can send with 1 credit */
483 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
484 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400485 server->max_read = le32_to_cpu(rsp->MaxReadSize);
486 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
487 /* BB Do we need to validate the SecurityMode? */
488 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
489 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400490 /* Internal types */
491 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400492
493 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
494 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500495 /*
496 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
497 * for us will be
498 * ses->sectype = RawNTLMSSP;
499 * but for time being this is our only auth choice so doesn't matter.
500 * We just found a server which sets blob length to zero expecting raw.
501 */
502 if (blob_length == 0)
503 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700504
Jeff Layton38d77c52013-05-26 07:01:00 -0400505 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400506 if (rc)
507 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500508 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500509 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500510 if (rc == 1)
511 rc = 0;
512 else if (rc == 0)
513 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400514 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400515neg_exit:
516 free_rsp_buf(resp_buftype, rsp);
517 return rc;
518}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400519
Steve Frenchff1c0382013-11-19 23:44:46 -0600520int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
521{
522 int rc = 0;
523 struct validate_negotiate_info_req vneg_inbuf;
524 struct validate_negotiate_info_rsp *pneg_rsp;
525 u32 rsplen;
526
527 cifs_dbg(FYI, "validate negotiate\n");
528
529 /*
530 * validation ioctl must be signed, so no point sending this if we
531 * can not sign it. We could eventually change this to selectively
532 * sign just this, the first and only signed request on a connection.
533 * This is good enough for now since a user who wants better security
534 * would also enable signing on the mount. Having validation of
535 * negotiate info for signed connections helps reduce attack vectors
536 */
537 if (tcon->ses->server->sign == false)
538 return 0; /* validation requires signing */
539
540 vneg_inbuf.Capabilities =
541 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100542 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
543 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600544
545 if (tcon->ses->sign)
546 vneg_inbuf.SecurityMode =
547 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
548 else if (global_secflags & CIFSSEC_MAY_SIGN)
549 vneg_inbuf.SecurityMode =
550 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
551 else
552 vneg_inbuf.SecurityMode = 0;
553
554 vneg_inbuf.DialectCount = cpu_to_le16(1);
555 vneg_inbuf.Dialects[0] =
556 cpu_to_le16(tcon->ses->server->vals->protocol_id);
557
558 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
559 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
560 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
561 (char **)&pneg_rsp, &rsplen);
562
563 if (rc != 0) {
564 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
565 return -EIO;
566 }
567
568 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
569 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
570 return -EIO;
571 }
572
573 /* check validate negotiate info response matches what we got earlier */
574 if (pneg_rsp->Dialect !=
575 cpu_to_le16(tcon->ses->server->vals->protocol_id))
576 goto vneg_out;
577
578 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
579 goto vneg_out;
580
581 /* do not validate server guid because not saved at negprot time yet */
582
583 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
584 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
585 goto vneg_out;
586
587 /* validate negotiate successful */
588 cifs_dbg(FYI, "validate negotiate info successful\n");
589 return 0;
590
591vneg_out:
592 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
593 return -EIO;
594}
595
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100596struct SMB2_sess_data {
597 unsigned int xid;
598 struct cifs_ses *ses;
599 struct nls_table *nls_cp;
600 void (*func)(struct SMB2_sess_data *);
601 int result;
602 u64 previous_session;
603
604 /* we will send the SMB in three pieces:
605 * a fixed length beginning part, an optional
606 * SPNEGO blob (which can be zero length), and a
607 * last part which will include the strings
608 * and rest of bcc area. This allows us to avoid
609 * a large buffer 17K allocation
610 */
611 int buf0_type;
612 struct kvec iov[2];
613};
614
615static int
616SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
617{
618 int rc;
619 struct cifs_ses *ses = sess_data->ses;
620 struct smb2_sess_setup_req *req;
621 struct TCP_Server_Info *server = ses->server;
622
623 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
624 if (rc)
625 return rc;
626
627 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
628
629 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
630 req->PreviousSessionId = sess_data->previous_session;
631
632 req->Flags = 0; /* MBZ */
633 /* to enable echos and oplocks */
634 req->hdr.CreditRequest = cpu_to_le16(3);
635
636 /* only one of SMB2 signing flags may be set in SMB2 request */
637 if (server->sign)
638 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
639 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
640 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
641 else
642 req->SecurityMode = 0;
643
644 req->Capabilities = 0;
645 req->Channel = 0; /* MBZ */
646
647 sess_data->iov[0].iov_base = (char *)req;
648 /* 4 for rfc1002 length field and 1 for pad */
649 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
650 /*
651 * This variable will be used to clear the buffer
652 * allocated above in case of any error in the calling function.
653 */
654 sess_data->buf0_type = CIFS_SMALL_BUFFER;
655
656 return 0;
657}
658
659static void
660SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
661{
662 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
663 sess_data->buf0_type = CIFS_NO_BUFFER;
664}
665
666static int
667SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
668{
669 int rc;
670 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
671
672 /* Testing shows that buffer offset must be at location of Buffer[0] */
673 req->SecurityBufferOffset =
674 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
675 1 /* pad */ - 4 /* rfc1001 len */);
676 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
677
678 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
679
680 /* BB add code to build os and lm fields */
681
682 rc = SendReceive2(sess_data->xid, sess_data->ses,
683 sess_data->iov, 2,
684 &sess_data->buf0_type,
685 CIFS_LOG_ERROR | CIFS_NEG_OP);
686
687 return rc;
688}
689
690static int
691SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
692{
693 int rc = 0;
694 struct cifs_ses *ses = sess_data->ses;
695
696 mutex_lock(&ses->server->srv_mutex);
697 if (ses->server->sign && ses->server->ops->generate_signingkey) {
698 rc = ses->server->ops->generate_signingkey(ses);
699 kfree(ses->auth_key.response);
700 ses->auth_key.response = NULL;
701 if (rc) {
702 cifs_dbg(FYI,
703 "SMB3 session key generation failed\n");
704 mutex_unlock(&ses->server->srv_mutex);
705 goto keygen_exit;
706 }
707 }
708 if (!ses->server->session_estab) {
709 ses->server->sequence_number = 0x2;
710 ses->server->session_estab = true;
711 }
712 mutex_unlock(&ses->server->srv_mutex);
713
714 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
715 spin_lock(&GlobalMid_Lock);
716 ses->status = CifsGood;
717 ses->need_reconnect = false;
718 spin_unlock(&GlobalMid_Lock);
719
720keygen_exit:
721 if (!ses->server->sign) {
722 kfree(ses->auth_key.response);
723 ses->auth_key.response = NULL;
724 }
725 return rc;
726}
727
728#ifdef CONFIG_CIFS_UPCALL
729static void
730SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
731{
732 int rc;
733 struct cifs_ses *ses = sess_data->ses;
734 struct cifs_spnego_msg *msg;
735 struct key *spnego_key = NULL;
736 struct smb2_sess_setup_rsp *rsp = NULL;
737
738 rc = SMB2_sess_alloc_buffer(sess_data);
739 if (rc)
740 goto out;
741
742 spnego_key = cifs_get_spnego_key(ses);
743 if (IS_ERR(spnego_key)) {
744 rc = PTR_ERR(spnego_key);
745 spnego_key = NULL;
746 goto out;
747 }
748
749 msg = spnego_key->payload.data[0];
750 /*
751 * check version field to make sure that cifs.upcall is
752 * sending us a response in an expected form
753 */
754 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
755 cifs_dbg(VFS,
756 "bad cifs.upcall version. Expected %d got %d",
757 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
758 rc = -EKEYREJECTED;
759 goto out_put_spnego_key;
760 }
761
762 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
763 GFP_KERNEL);
764 if (!ses->auth_key.response) {
765 cifs_dbg(VFS,
766 "Kerberos can't allocate (%u bytes) memory",
767 msg->sesskey_len);
768 rc = -ENOMEM;
769 goto out_put_spnego_key;
770 }
771 ses->auth_key.len = msg->sesskey_len;
772
773 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
774 sess_data->iov[1].iov_len = msg->secblob_len;
775
776 rc = SMB2_sess_sendreceive(sess_data);
777 if (rc)
778 goto out_put_spnego_key;
779
780 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
781 ses->Suid = rsp->hdr.SessionId;
782
783 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
784 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
785 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
786
787 rc = SMB2_sess_establish_session(sess_data);
788out_put_spnego_key:
789 key_invalidate(spnego_key);
790 key_put(spnego_key);
791out:
792 sess_data->result = rc;
793 sess_data->func = NULL;
794 SMB2_sess_free_buffer(sess_data);
795}
796#else
797static void
798SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
799{
800 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
801 sess_data->result = -EOPNOTSUPP;
802 sess_data->func = NULL;
803}
804#endif
805
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400806int
807SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
808 const struct nls_table *nls_cp)
809{
810 struct smb2_sess_setup_req *req;
811 struct smb2_sess_setup_rsp *rsp = NULL;
812 struct kvec iov[2];
813 int rc = 0;
Namjae Jeon7de975e2014-08-20 19:39:41 +0900814 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400815 __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
Jeff Layton3534b852013-05-24 07:41:01 -0400816 struct TCP_Server_Info *server = ses->server;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400817 u16 blob_length = 0;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500818 char *security_blob = NULL;
Jerome Marchandb8da3442016-05-26 11:52:25 +0200819 unsigned char *ntlmssp_blob = NULL;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400820 bool use_spnego = false; /* else use raw ntlmssp */
Steve Frenchc2afb812016-09-20 22:56:13 -0500821 u64 previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100822 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400823
Joe Perchesf96637b2013-05-04 22:12:25 -0500824 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400825
Jeff Layton3534b852013-05-24 07:41:01 -0400826 if (!server) {
827 WARN(1, "%s: server is NULL!\n", __func__);
828 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400829 }
830
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100831 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
832 if (!sess_data)
833 return -ENOMEM;
834 sess_data->xid = xid;
835 sess_data->ses = ses;
836 sess_data->buf0_type = CIFS_NO_BUFFER;
837 sess_data->nls_cp = (struct nls_table *) nls_cp;
838 sess_data->previous_session = ses->Suid;
839
840 if (ses->sectype == Kerberos) {
841 SMB2_auth_kerberos(sess_data);
842 goto out;
843 }
844
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400845 /*
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500846 * If we are here due to reconnect, free per-smb session key
847 * in case signing was required.
848 */
849 kfree(ses->auth_key.response);
850 ses->auth_key.response = NULL;
851
852 /*
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400853 * If memory allocation is successful, caller of this function
854 * frees it.
855 */
856 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
857 if (!ses->ntlmssp)
858 return -ENOMEM;
Shirish Pargaonkar5c234aa2013-08-29 08:35:10 -0500859 ses->ntlmssp->sesskey_per_smbsess = true;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400860
Jeff Layton3f618222013-06-12 19:52:14 -0500861 /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500862 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
863 ses->sectype = RawNTLMSSP;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400864
865ssetup_ntlmssp_authenticate:
866 if (phase == NtLmChallenge)
867 phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
868
869 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
870 if (rc)
871 return rc;
872
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400873 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
Steve Frenchc2afb812016-09-20 22:56:13 -0500874
875 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
876 req->PreviousSessionId = previous_session;
877
Steve Frencheed0e172015-02-06 00:03:52 -0600878 req->Flags = 0; /* MBZ */
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400879 /* to enable echos and oplocks */
880 req->hdr.CreditRequest = cpu_to_le16(3);
881
882 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400883 if (server->sign)
884 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
885 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
886 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
887 else
888 req->SecurityMode = 0;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400889
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400890 req->Capabilities = 0;
891 req->Channel = 0; /* MBZ */
892
893 iov[0].iov_base = (char *)req;
894 /* 4 for rfc1002 length field and 1 for pad */
895 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500896
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100897 if (phase == NtLmNegotiate) {
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400898 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
899 GFP_KERNEL);
900 if (ntlmssp_blob == NULL) {
901 rc = -ENOMEM;
902 goto ssetup_exit;
903 }
904 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
905 if (use_spnego) {
906 /* blob_length = build_spnego_ntlmssp_blob(
907 &security_blob,
908 sizeof(struct _NEGOTIATE_MESSAGE),
909 ntlmssp_blob); */
910 /* BB eventually need to add this */
Joe Perchesf96637b2013-05-04 22:12:25 -0500911 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400912 rc = -EOPNOTSUPP;
913 kfree(ntlmssp_blob);
914 goto ssetup_exit;
915 } else {
916 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
917 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
918 security_blob = ntlmssp_blob;
919 }
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500920 iov[1].iov_base = security_blob;
921 iov[1].iov_len = blob_length;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400922 } else if (phase == NtLmAuthenticate) {
923 req->hdr.SessionId = ses->Suid;
Jerome Marchandb8da3442016-05-26 11:52:25 +0200924 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400925 nls_cp);
926 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500927 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
928 rc);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400929 goto ssetup_exit; /* BB double check error handling */
930 }
931 if (use_spnego) {
932 /* blob_length = build_spnego_ntlmssp_blob(
933 &security_blob,
934 blob_length,
935 ntlmssp_blob); */
Joe Perchesf96637b2013-05-04 22:12:25 -0500936 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400937 rc = -EOPNOTSUPP;
938 kfree(ntlmssp_blob);
939 goto ssetup_exit;
940 } else {
941 security_blob = ntlmssp_blob;
942 }
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500943 iov[1].iov_base = security_blob;
944 iov[1].iov_len = blob_length;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400945 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -0500946 cifs_dbg(VFS, "illegal ntlmssp phase\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400947 rc = -EIO;
948 goto ssetup_exit;
949 }
950
951 /* Testing shows that buffer offset must be at location of Buffer[0] */
952 req->SecurityBufferOffset =
953 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
954 1 /* pad */ - 4 /* rfc1001 len */);
955 req->SecurityBufferLength = cpu_to_le16(blob_length);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400956
957 inc_rfc1001_len(req, blob_length - 1 /* pad */);
958
959 /* BB add code to build os and lm fields */
960
Steve French6d8b59d2012-12-08 22:36:29 -0600961 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
962 CIFS_LOG_ERROR | CIFS_NEG_OP);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400963
964 kfree(security_blob);
965 rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500966 ses->Suid = rsp->hdr.SessionId;
Pavel Shilovsky4ca3a992012-09-25 11:00:09 +0400967 if (resp_buftype != CIFS_NO_BUFFER &&
968 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400969 if (phase != NtLmNegotiate) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500970 cifs_dbg(VFS, "Unexpected more processing error\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400971 goto ssetup_exit;
972 }
973 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
Pavel Shilovsky4ca3a992012-09-25 11:00:09 +0400974 le16_to_cpu(rsp->SecurityBufferOffset)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500975 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
976 le16_to_cpu(rsp->SecurityBufferOffset));
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400977 rc = -EIO;
978 goto ssetup_exit;
979 }
980
981 /* NTLMSSP Negotiate sent now processing challenge (response) */
982 phase = NtLmChallenge; /* process ntlmssp challenge */
983 rc = 0; /* MORE_PROCESSING is not an error here but expected */
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400984 rc = decode_ntlmssp_challenge(rsp->Buffer,
985 le16_to_cpu(rsp->SecurityBufferLength), ses);
986 }
987
988 /*
989 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
990 * but at least the raw NTLMSSP case works.
991 */
992 /*
993 * No tcon so can't do
994 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
995 */
996 if (rc != 0)
997 goto ssetup_exit;
998
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400999 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Steve French0cbaa532013-11-15 23:50:24 -06001000 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1001 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001002ssetup_exit:
1003 free_rsp_buf(resp_buftype, rsp);
1004
1005 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
1006 if ((phase == NtLmChallenge) && (rc == 0))
1007 goto ssetup_ntlmssp_authenticate;
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05001008
1009 if (!rc) {
1010 mutex_lock(&server->srv_mutex);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -05001011 if (server->sign && server->ops->generate_signingkey) {
1012 rc = server->ops->generate_signingkey(ses);
1013 kfree(ses->auth_key.response);
1014 ses->auth_key.response = NULL;
1015 if (rc) {
1016 cifs_dbg(FYI,
1017 "SMB3 session key generation failed\n");
1018 mutex_unlock(&server->srv_mutex);
1019 goto keygen_exit;
1020 }
1021 }
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05001022 if (!server->session_estab) {
1023 server->sequence_number = 0x2;
1024 server->session_estab = true;
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05001025 }
1026 mutex_unlock(&server->srv_mutex);
1027
1028 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1029 spin_lock(&GlobalMid_Lock);
1030 ses->status = CifsGood;
1031 ses->need_reconnect = false;
1032 spin_unlock(&GlobalMid_Lock);
1033 }
1034
Shirish Pargaonkar32811d22013-08-29 08:35:11 -05001035keygen_exit:
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05001036 if (!server->sign) {
1037 kfree(ses->auth_key.response);
1038 ses->auth_key.response = NULL;
1039 }
1040 kfree(ses->ntlmssp);
1041
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001042 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001043out:
1044 rc = sess_data->result;
1045 kfree(sess_data);
1046 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001047}
1048
1049int
1050SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1051{
1052 struct smb2_logoff_req *req; /* response is also trivial struct */
1053 int rc = 0;
1054 struct TCP_Server_Info *server;
1055
Joe Perchesf96637b2013-05-04 22:12:25 -05001056 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001057
1058 if (ses && (ses->server))
1059 server = ses->server;
1060 else
1061 return -EIO;
1062
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001063 /* no need to send SMB logoff if uid already closed due to reconnect */
1064 if (ses->need_reconnect)
1065 goto smb2_session_already_dead;
1066
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001067 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1068 if (rc)
1069 return rc;
1070
1071 /* since no tcon, smb2_init can not do this, so do here */
1072 req->hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -04001073 if (server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001074 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001075
1076 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
1077 /*
1078 * No tcon so can't do
1079 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1080 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001081
1082smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001083 return rc;
1084}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001085
1086static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1087{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001088 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001089}
1090
1091#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1092
Steve Frenchde9f68d2013-11-15 11:26:24 -06001093/* These are similar values to what Windows uses */
1094static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1095{
1096 tcon->max_chunks = 256;
1097 tcon->max_bytes_chunk = 1048576;
1098 tcon->max_bytes_copy = 16777216;
1099}
1100
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001101int
1102SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1103 struct cifs_tcon *tcon, const struct nls_table *cp)
1104{
1105 struct smb2_tree_connect_req *req;
1106 struct smb2_tree_connect_rsp *rsp = NULL;
1107 struct kvec iov[2];
1108 int rc = 0;
1109 int resp_buftype;
1110 int unc_path_len;
1111 struct TCP_Server_Info *server;
1112 __le16 *unc_path = NULL;
1113
Joe Perchesf96637b2013-05-04 22:12:25 -05001114 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001115
1116 if ((ses->server) && tree)
1117 server = ses->server;
1118 else
1119 return -EIO;
1120
1121 if (tcon && tcon->bad_network_name)
1122 return -ENOENT;
1123
Steve Frenchff9f84b2015-09-26 09:48:58 -05001124 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -05001125 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1126 cifs_dbg(VFS, "encryption requested but no server support");
1127 return -EOPNOTSUPP;
1128 }
1129
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001130 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1131 if (unc_path == NULL)
1132 return -ENOMEM;
1133
1134 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1135 unc_path_len *= 2;
1136 if (unc_path_len < 2) {
1137 kfree(unc_path);
1138 return -EINVAL;
1139 }
1140
1141 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1142 if (rc) {
1143 kfree(unc_path);
1144 return rc;
1145 }
1146
1147 if (tcon == NULL) {
1148 /* since no tcon, smb2_init can not do this, so do here */
1149 req->hdr.SessionId = ses->Suid;
1150 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1151 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1152 }
1153
1154 iov[0].iov_base = (char *)req;
1155 /* 4 for rfc1002 length field and 1 for pad */
1156 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1157
1158 /* Testing shows that buffer offset must be at location of Buffer[0] */
1159 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1160 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1161 req->PathLength = cpu_to_le16(unc_path_len - 2);
1162 iov[1].iov_base = unc_path;
1163 iov[1].iov_len = unc_path_len;
1164
1165 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1166
1167 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1168 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
1169
1170 if (rc != 0) {
1171 if (tcon) {
1172 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1173 tcon->need_reconnect = true;
1174 }
1175 goto tcon_error_exit;
1176 }
1177
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001178 if (tcon == NULL) {
1179 ses->ipc_tid = rsp->hdr.TreeId;
1180 goto tcon_exit;
1181 }
1182
1183 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001184 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001185 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1186 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001187 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001188 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1189 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001190 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001191 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001192 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001193 rc = -EOPNOTSUPP;
1194 goto tcon_error_exit;
1195 }
1196
1197 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001198 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001199 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1200 tcon->tidStatus = CifsGood;
1201 tcon->need_reconnect = false;
1202 tcon->tid = rsp->hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001203 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001204
1205 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1206 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001207 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001208 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001209 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1210 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001211 if (tcon->ses->server->ops->validate_negotiate)
1212 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001213tcon_exit:
1214 free_rsp_buf(resp_buftype, rsp);
1215 kfree(unc_path);
1216 return rc;
1217
1218tcon_error_exit:
1219 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001220 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Steve French18f39e72014-08-17 00:22:24 -05001221 if (tcon)
1222 tcon->bad_network_name = true;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001223 }
1224 goto tcon_exit;
1225}
1226
1227int
1228SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1229{
1230 struct smb2_tree_disconnect_req *req; /* response is trivial */
1231 int rc = 0;
1232 struct TCP_Server_Info *server;
1233 struct cifs_ses *ses = tcon->ses;
1234
Joe Perchesf96637b2013-05-04 22:12:25 -05001235 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001236
1237 if (ses && (ses->server))
1238 server = ses->server;
1239 else
1240 return -EIO;
1241
1242 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1243 return 0;
1244
1245 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1246 if (rc)
1247 return rc;
1248
1249 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1250 if (rc)
1251 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1252
1253 return rc;
1254}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001255
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001256
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001257static struct create_durable *
1258create_durable_buf(void)
1259{
1260 struct create_durable *buf;
1261
1262 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1263 if (!buf)
1264 return NULL;
1265
1266 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001267 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001268 buf->ccontext.DataLength = cpu_to_le32(16);
1269 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1270 (struct create_durable, Name));
1271 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001272 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001273 buf->Name[0] = 'D';
1274 buf->Name[1] = 'H';
1275 buf->Name[2] = 'n';
1276 buf->Name[3] = 'Q';
1277 return buf;
1278}
1279
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001280static struct create_durable *
1281create_reconnect_durable_buf(struct cifs_fid *fid)
1282{
1283 struct create_durable *buf;
1284
1285 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1286 if (!buf)
1287 return NULL;
1288
1289 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1290 (struct create_durable, Data));
1291 buf->ccontext.DataLength = cpu_to_le32(16);
1292 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1293 (struct create_durable, Name));
1294 buf->ccontext.NameLength = cpu_to_le16(4);
1295 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1296 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001297 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001298 buf->Name[0] = 'D';
1299 buf->Name[1] = 'H';
1300 buf->Name[2] = 'n';
1301 buf->Name[3] = 'C';
1302 return buf;
1303}
1304
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001305static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001306parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1307 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001308{
1309 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001310 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001311 unsigned int next;
1312 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001313 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001314
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001315 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001316 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001317 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001318 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001319 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001320 if (le16_to_cpu(cc->NameLength) == 4 &&
1321 strncmp(name, "RqLs", 4) == 0)
1322 return server->ops->parse_lease_buf(cc, epoch);
1323
1324 next = le32_to_cpu(cc->Next);
1325 if (!next)
1326 break;
1327 remaining -= next;
1328 cc = (struct create_context *)((char *)cc + next);
1329 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001330
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001331 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001332}
1333
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001334static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001335add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1336 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001337{
1338 struct smb2_create_req *req = iov[0].iov_base;
1339 unsigned int num = *num_iovec;
1340
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001341 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001342 if (iov[num].iov_base == NULL)
1343 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001344 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001345 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1346 if (!req->CreateContextsOffset)
1347 req->CreateContextsOffset = cpu_to_le32(
1348 sizeof(struct smb2_create_req) - 4 +
1349 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001350 le32_add_cpu(&req->CreateContextsLength,
1351 server->vals->create_lease_size);
1352 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001353 *num_iovec = num + 1;
1354 return 0;
1355}
1356
Steve Frenchb56eae42015-11-03 09:26:27 -06001357static struct create_durable_v2 *
1358create_durable_v2_buf(struct cifs_fid *pfid)
1359{
1360 struct create_durable_v2 *buf;
1361
1362 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1363 if (!buf)
1364 return NULL;
1365
1366 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1367 (struct create_durable_v2, dcontext));
1368 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1369 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1370 (struct create_durable_v2, Name));
1371 buf->ccontext.NameLength = cpu_to_le16(4);
1372
1373 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1374 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001375 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001376 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1377
1378 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1379 buf->Name[0] = 'D';
1380 buf->Name[1] = 'H';
1381 buf->Name[2] = '2';
1382 buf->Name[3] = 'Q';
1383 return buf;
1384}
1385
1386static struct create_durable_handle_reconnect_v2 *
1387create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1388{
1389 struct create_durable_handle_reconnect_v2 *buf;
1390
1391 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1392 GFP_KERNEL);
1393 if (!buf)
1394 return NULL;
1395
1396 buf->ccontext.DataOffset =
1397 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1398 dcontext));
1399 buf->ccontext.DataLength =
1400 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1401 buf->ccontext.NameOffset =
1402 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1403 Name));
1404 buf->ccontext.NameLength = cpu_to_le16(4);
1405
1406 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1407 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1408 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1409 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1410
1411 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1412 buf->Name[0] = 'D';
1413 buf->Name[1] = 'H';
1414 buf->Name[2] = '2';
1415 buf->Name[3] = 'C';
1416 return buf;
1417}
1418
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001419static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001420add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001421 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001422{
1423 struct smb2_create_req *req = iov[0].iov_base;
1424 unsigned int num = *num_iovec;
1425
Steve Frenchb56eae42015-11-03 09:26:27 -06001426 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1427 if (iov[num].iov_base == NULL)
1428 return -ENOMEM;
1429 iov[num].iov_len = sizeof(struct create_durable_v2);
1430 if (!req->CreateContextsOffset)
1431 req->CreateContextsOffset =
1432 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1433 iov[1].iov_len);
1434 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1435 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1436 *num_iovec = num + 1;
1437 return 0;
1438}
1439
1440static int
1441add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1442 struct cifs_open_parms *oparms)
1443{
1444 struct smb2_create_req *req = iov[0].iov_base;
1445 unsigned int num = *num_iovec;
1446
1447 /* indicate that we don't need to relock the file */
1448 oparms->reconnect = false;
1449
1450 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1451 if (iov[num].iov_base == NULL)
1452 return -ENOMEM;
1453 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1454 if (!req->CreateContextsOffset)
1455 req->CreateContextsOffset =
1456 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1457 iov[1].iov_len);
1458 le32_add_cpu(&req->CreateContextsLength,
1459 sizeof(struct create_durable_handle_reconnect_v2));
1460 inc_rfc1001_len(&req->hdr,
1461 sizeof(struct create_durable_handle_reconnect_v2));
1462 *num_iovec = num + 1;
1463 return 0;
1464}
1465
1466static int
1467add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1468 struct cifs_open_parms *oparms, bool use_persistent)
1469{
1470 struct smb2_create_req *req = iov[0].iov_base;
1471 unsigned int num = *num_iovec;
1472
1473 if (use_persistent) {
1474 if (oparms->reconnect)
1475 return add_durable_reconnect_v2_context(iov, num_iovec,
1476 oparms);
1477 else
1478 return add_durable_v2_context(iov, num_iovec, oparms);
1479 }
1480
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001481 if (oparms->reconnect) {
1482 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1483 /* indicate that we don't need to relock the file */
1484 oparms->reconnect = false;
1485 } else
1486 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001487 if (iov[num].iov_base == NULL)
1488 return -ENOMEM;
1489 iov[num].iov_len = sizeof(struct create_durable);
1490 if (!req->CreateContextsOffset)
1491 req->CreateContextsOffset =
1492 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1493 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001494 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001495 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1496 *num_iovec = num + 1;
1497 return 0;
1498}
1499
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001500int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001501SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001502 __u8 *oplock, struct smb2_file_all_info *buf,
1503 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001504{
1505 struct smb2_create_req *req;
1506 struct smb2_create_rsp *rsp;
1507 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001508 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001509 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001510 struct kvec iov[4];
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001511 int resp_buftype;
1512 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001513 __le16 *copy_path = NULL;
1514 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001515 int rc = 0;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001516 unsigned int num_iovecs = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001517 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001518 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001519
Joe Perchesf96637b2013-05-04 22:12:25 -05001520 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001521
1522 if (ses && (ses->server))
1523 server = ses->server;
1524 else
1525 return -EIO;
1526
1527 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1528 if (rc)
1529 return rc;
1530
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001531 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001532 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001533 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1534 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001535
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001536 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001537 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001538 /* File attributes ignored on open (used in create though) */
1539 req->FileAttributes = cpu_to_le32(file_attributes);
1540 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001541 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1542 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001543 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001544 /* do not count rfc1001 len field */
1545 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001546
1547 iov[0].iov_base = (char *)req;
1548 /* 4 for rfc1002 length field */
1549 iov[0].iov_len = get_rfc1002_length(req) + 4;
1550
1551 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001552 req->NameLength = cpu_to_le16(uni_path_len - 2);
1553 /* -1 since last byte is buf[0] which is sent below (path) */
1554 iov[0].iov_len--;
1555 if (uni_path_len % 8 != 0) {
1556 copy_size = uni_path_len / 8 * 8;
1557 if (copy_size < uni_path_len)
1558 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001559
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001560 copy_path = kzalloc(copy_size, GFP_KERNEL);
1561 if (!copy_path)
1562 return -ENOMEM;
1563 memcpy((char *)copy_path, (const char *)path,
1564 uni_path_len);
1565 uni_path_len = copy_size;
1566 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001567 }
1568
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001569 iov[1].iov_len = uni_path_len;
1570 iov[1].iov_base = path;
1571 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1572 inc_rfc1001_len(req, uni_path_len - 1);
1573
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001574 if (!server->oplocks)
1575 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1576
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001577 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001578 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1579 req->RequestedOplockLevel = *oplock;
1580 else {
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001581 rc = add_lease_context(server, iov, &num_iovecs, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001582 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001583 cifs_small_buf_release(req);
1584 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001585 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001586 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001587 lc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001588 }
1589
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001590 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1591 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001592 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001593 struct create_context *ccontext =
1594 (struct create_context *)iov[num_iovecs-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001595 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001596 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001597 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001598
1599 rc = add_durable_context(iov, &num_iovecs, oparms,
1600 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001601 if (rc) {
1602 cifs_small_buf_release(req);
1603 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001604 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001605 return rc;
1606 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001607 dhc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001608 }
1609
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001610 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1611 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1612
1613 if (rc != 0) {
1614 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001615 if (err_buf)
1616 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1617 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001618 goto creat_exit;
1619 }
1620
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001621 oparms->fid->persistent_fid = rsp->PersistentFileId;
1622 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001623
1624 if (buf) {
1625 memcpy(buf, &rsp->CreationTime, 32);
1626 buf->AllocationSize = rsp->AllocationSize;
1627 buf->EndOfFile = rsp->EndofFile;
1628 buf->Attributes = rsp->FileAttributes;
1629 buf->NumberOfLinks = cpu_to_le32(1);
1630 buf->DeletePending = 0;
1631 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001632
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001633 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001634 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001635 else
1636 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001637creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001638 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001639 kfree(lc_buf);
1640 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001641 free_rsp_buf(resp_buftype, rsp);
1642 return rc;
1643}
1644
Steve French4a72daf2013-06-25 00:20:49 -05001645/*
1646 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1647 */
1648int
1649SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1650 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1651 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1652{
1653 struct smb2_ioctl_req *req;
1654 struct smb2_ioctl_rsp *rsp;
1655 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001656 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001657 struct kvec iov[2];
1658 int resp_buftype;
1659 int num_iovecs;
1660 int rc = 0;
1661
1662 cifs_dbg(FYI, "SMB2 IOCTL\n");
1663
Steve French3d1a3742014-08-11 21:05:25 -05001664 if (out_data != NULL)
1665 *out_data = NULL;
1666
Steve French4a72daf2013-06-25 00:20:49 -05001667 /* zero out returned data len, in case of error */
1668 if (plen)
1669 *plen = 0;
1670
Steve French8e353102015-03-26 19:47:02 -05001671 if (tcon)
1672 ses = tcon->ses;
1673 else
1674 return -EIO;
1675
Steve French4a72daf2013-06-25 00:20:49 -05001676 if (ses && (ses->server))
1677 server = ses->server;
1678 else
1679 return -EIO;
1680
1681 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1682 if (rc)
1683 return rc;
1684
1685 req->CtlCode = cpu_to_le32(opcode);
1686 req->PersistentFileId = persistent_fid;
1687 req->VolatileFileId = volatile_fid;
1688
1689 if (indatalen) {
1690 req->InputCount = cpu_to_le32(indatalen);
1691 /* do not set InputOffset if no input data */
1692 req->InputOffset =
1693 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1694 iov[1].iov_base = in_data;
1695 iov[1].iov_len = indatalen;
1696 num_iovecs = 2;
1697 } else
1698 num_iovecs = 1;
1699
1700 req->OutputOffset = 0;
1701 req->OutputCount = 0; /* MBZ */
1702
1703 /*
1704 * Could increase MaxOutputResponse, but that would require more
1705 * than one credit. Windows typically sets this smaller, but for some
1706 * ioctls it may be useful to allow server to send more. No point
1707 * limiting what the server can send as long as fits in one credit
1708 */
1709 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1710
1711 if (is_fsctl)
1712 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1713 else
1714 req->Flags = 0;
1715
1716 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001717
Steve French7ff8d452013-10-14 00:44:19 -05001718 /*
1719 * If no input data, the size of ioctl struct in
1720 * protocol spec still includes a 1 byte data buffer,
1721 * but if input data passed to ioctl, we do not
1722 * want to double count this, so we do not send
1723 * the dummy one byte of data in iovec[0] if sending
1724 * input data (in iovec[1]). We also must add 4 bytes
1725 * in first iovec to allow for rfc1002 length field.
1726 */
1727
1728 if (indatalen) {
1729 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1730 inc_rfc1001_len(req, indatalen - 1);
1731 } else
1732 iov[0].iov_len = get_rfc1002_length(req) + 4;
1733
Steve French4a72daf2013-06-25 00:20:49 -05001734
1735 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1736 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1737
Steve French9bf0c9c2013-11-16 18:05:28 -06001738 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001739 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001740 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001741 } else if (rc == -EINVAL) {
1742 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1743 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001744 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001745 goto ioctl_exit;
1746 }
Steve French4a72daf2013-06-25 00:20:49 -05001747 }
1748
1749 /* check if caller wants to look at return data or just return rc */
1750 if ((plen == NULL) || (out_data == NULL))
1751 goto ioctl_exit;
1752
1753 *plen = le32_to_cpu(rsp->OutputCount);
1754
1755 /* We check for obvious errors in the output buffer length and offset */
1756 if (*plen == 0)
1757 goto ioctl_exit; /* server returned no data */
1758 else if (*plen > 0xFF00) {
1759 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1760 *plen = 0;
1761 rc = -EIO;
1762 goto ioctl_exit;
1763 }
1764
1765 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1766 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1767 le32_to_cpu(rsp->OutputOffset));
1768 *plen = 0;
1769 rc = -EIO;
1770 goto ioctl_exit;
1771 }
1772
1773 *out_data = kmalloc(*plen, GFP_KERNEL);
1774 if (*out_data == NULL) {
1775 rc = -ENOMEM;
1776 goto ioctl_exit;
1777 }
1778
Steve French373512e2015-12-18 13:05:30 -06001779 memcpy(*out_data,
1780 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
Steve French4a72daf2013-06-25 00:20:49 -05001781 *plen);
1782ioctl_exit:
1783 free_rsp_buf(resp_buftype, rsp);
1784 return rc;
1785}
1786
Steve French64a5cfa2013-10-14 15:31:32 -05001787/*
1788 * Individual callers to ioctl worker function follow
1789 */
1790
1791int
1792SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1793 u64 persistent_fid, u64 volatile_fid)
1794{
1795 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001796 struct compress_ioctl fsctl_input;
1797 char *ret_data = NULL;
1798
1799 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001800 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001801
1802 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1803 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1804 (char *)&fsctl_input /* data input */,
1805 2 /* in data len */, &ret_data /* out data */, NULL);
1806
1807 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001808
1809 return rc;
1810}
1811
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001812int
1813SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1814 u64 persistent_fid, u64 volatile_fid)
1815{
1816 struct smb2_close_req *req;
1817 struct smb2_close_rsp *rsp;
1818 struct TCP_Server_Info *server;
1819 struct cifs_ses *ses = tcon->ses;
1820 struct kvec iov[1];
1821 int resp_buftype;
1822 int rc = 0;
1823
Joe Perchesf96637b2013-05-04 22:12:25 -05001824 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001825
1826 if (ses && (ses->server))
1827 server = ses->server;
1828 else
1829 return -EIO;
1830
1831 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1832 if (rc)
1833 return rc;
1834
1835 req->PersistentFileId = persistent_fid;
1836 req->VolatileFileId = volatile_fid;
1837
1838 iov[0].iov_base = (char *)req;
1839 /* 4 for rfc1002 length field */
1840 iov[0].iov_len = get_rfc1002_length(req) + 4;
1841
1842 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1843 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1844
1845 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001846 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001847 goto close_exit;
1848 }
1849
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001850 /* BB FIXME - decode close response, update inode for caching */
1851
1852close_exit:
1853 free_rsp_buf(resp_buftype, rsp);
1854 return rc;
1855}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001856
1857static int
1858validate_buf(unsigned int offset, unsigned int buffer_length,
1859 struct smb2_hdr *hdr, unsigned int min_buf_size)
1860
1861{
1862 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1863 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1864 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1865 char *end_of_buf = begin_of_buf + buffer_length;
1866
1867
1868 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001869 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1870 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001871 return -EINVAL;
1872 }
1873
1874 /* check if beyond RFC1001 maximum length */
1875 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001876 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1877 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001878 return -EINVAL;
1879 }
1880
1881 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001882 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001883 return -EINVAL;
1884 }
1885
1886 return 0;
1887}
1888
1889/*
1890 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1891 * Caller must free buffer.
1892 */
1893static int
1894validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1895 struct smb2_hdr *hdr, unsigned int minbufsize,
1896 char *data)
1897
1898{
1899 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1900 int rc;
1901
1902 if (!data)
1903 return -EINVAL;
1904
1905 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1906 if (rc)
1907 return rc;
1908
1909 memcpy(data, begin_of_buf, buffer_length);
1910
1911 return 0;
1912}
1913
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001914static int
1915query_info(const unsigned int xid, struct cifs_tcon *tcon,
1916 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1917 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001918{
1919 struct smb2_query_info_req *req;
1920 struct smb2_query_info_rsp *rsp = NULL;
1921 struct kvec iov[2];
1922 int rc = 0;
1923 int resp_buftype;
1924 struct TCP_Server_Info *server;
1925 struct cifs_ses *ses = tcon->ses;
1926
Joe Perchesf96637b2013-05-04 22:12:25 -05001927 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001928
1929 if (ses && (ses->server))
1930 server = ses->server;
1931 else
1932 return -EIO;
1933
1934 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1935 if (rc)
1936 return rc;
1937
1938 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001939 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001940 req->PersistentFileId = persistent_fid;
1941 req->VolatileFileId = volatile_fid;
1942 /* 4 for rfc1002 length field and 1 for Buffer */
1943 req->InputBufferOffset =
1944 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001945 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001946
1947 iov[0].iov_base = (char *)req;
1948 /* 4 for rfc1002 length field */
1949 iov[0].iov_len = get_rfc1002_length(req) + 4;
1950
1951 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001952 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1953
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001954 if (rc) {
1955 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1956 goto qinf_exit;
1957 }
1958
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001959 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1960 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001961 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001962
1963qinf_exit:
1964 free_rsp_buf(resp_buftype, rsp);
1965 return rc;
1966}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001967
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001968int
1969SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1970 u64 persistent_fid, u64 volatile_fid,
1971 struct smb2_file_all_info *data)
1972{
1973 return query_info(xid, tcon, persistent_fid, volatile_fid,
1974 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001975 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001976 sizeof(struct smb2_file_all_info), data);
1977}
1978
1979int
1980SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1981 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1982{
1983 return query_info(xid, tcon, persistent_fid, volatile_fid,
1984 FILE_INTERNAL_INFORMATION,
1985 sizeof(struct smb2_file_internal_info),
1986 sizeof(struct smb2_file_internal_info), uniqueid);
1987}
1988
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001989/*
1990 * This is a no-op for now. We're not really interested in the reply, but
1991 * rather in the fact that the server sent one and that server->lstrp
1992 * gets updated.
1993 *
1994 * FIXME: maybe we should consider checking that the reply matches request?
1995 */
1996static void
1997smb2_echo_callback(struct mid_q_entry *mid)
1998{
1999 struct TCP_Server_Info *server = mid->callback_data;
2000 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
2001 unsigned int credits_received = 1;
2002
2003 if (mid->mid_state == MID_RESPONSE_RECEIVED)
2004 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
2005
Christopher Oo5fb4e282015-06-25 16:10:48 -07002006 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002007 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002008 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002009 add_credits(server, credits_received, CIFS_ECHO_OP);
2010}
2011
2012int
2013SMB2_echo(struct TCP_Server_Info *server)
2014{
2015 struct smb2_echo_req *req;
2016 int rc = 0;
2017 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002018 struct smb_rqst rqst = { .rq_iov = &iov,
2019 .rq_nvec = 1 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002020
Joe Perchesf96637b2013-05-04 22:12:25 -05002021 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002022
Steve French4fcd1812016-06-22 20:12:05 -05002023 if (server->tcpStatus == CifsNeedNegotiate) {
2024 struct list_head *tmp, *tmp2;
2025 struct cifs_ses *ses;
2026 struct cifs_tcon *tcon;
2027
2028 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2029 spin_lock(&cifs_tcp_ses_lock);
2030 list_for_each(tmp, &server->smb_ses_list) {
2031 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
2032 list_for_each(tmp2, &ses->tcon_list) {
2033 tcon = list_entry(tmp2, struct cifs_tcon,
2034 tcon_list);
2035 /* add check for persistent handle reconnect */
2036 if (tcon && tcon->need_reconnect) {
2037 spin_unlock(&cifs_tcp_ses_lock);
2038 rc = smb2_reconnect(SMB2_ECHO, tcon);
2039 spin_lock(&cifs_tcp_ses_lock);
2040 }
2041 }
2042 }
2043 spin_unlock(&cifs_tcp_ses_lock);
2044 }
2045
2046 /* if no session, renegotiate failed above */
2047 if (server->tcpStatus == CifsNeedNegotiate)
2048 return -EIO;
2049
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002050 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2051 if (rc)
2052 return rc;
2053
2054 req->hdr.CreditRequest = cpu_to_le16(1);
2055
2056 iov.iov_base = (char *)req;
2057 /* 4 for rfc1002 length field */
2058 iov.iov_len = get_rfc1002_length(req) + 4;
2059
Jeff Laytonfec344e2012-09-18 16:20:35 -07002060 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002061 CIFS_ECHO_OP);
2062 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002063 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002064
2065 cifs_small_buf_release(req);
2066 return rc;
2067}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002068
2069int
2070SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2071 u64 volatile_fid)
2072{
2073 struct smb2_flush_req *req;
2074 struct TCP_Server_Info *server;
2075 struct cifs_ses *ses = tcon->ses;
2076 struct kvec iov[1];
2077 int resp_buftype;
2078 int rc = 0;
2079
Joe Perchesf96637b2013-05-04 22:12:25 -05002080 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002081
2082 if (ses && (ses->server))
2083 server = ses->server;
2084 else
2085 return -EIO;
2086
2087 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2088 if (rc)
2089 return rc;
2090
2091 req->PersistentFileId = persistent_fid;
2092 req->VolatileFileId = volatile_fid;
2093
2094 iov[0].iov_base = (char *)req;
2095 /* 4 for rfc1002 length field */
2096 iov[0].iov_len = get_rfc1002_length(req) + 4;
2097
2098 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
2099
Steve Frenchdfebe402015-03-27 01:00:06 -05002100 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002101 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2102
2103 free_rsp_buf(resp_buftype, iov[0].iov_base);
2104 return rc;
2105}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002106
2107/*
2108 * To form a chain of read requests, any read requests after the first should
2109 * have the end_of_chain boolean set to true.
2110 */
2111static int
2112smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2113 unsigned int remaining_bytes, int request_type)
2114{
2115 int rc = -EACCES;
2116 struct smb2_read_req *req = NULL;
2117
2118 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2119 if (rc)
2120 return rc;
2121 if (io_parms->tcon->ses->server == NULL)
2122 return -ECONNABORTED;
2123
2124 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2125
2126 req->PersistentFileId = io_parms->persistent_fid;
2127 req->VolatileFileId = io_parms->volatile_fid;
2128 req->ReadChannelInfoOffset = 0; /* reserved */
2129 req->ReadChannelInfoLength = 0; /* reserved */
2130 req->Channel = 0; /* reserved */
2131 req->MinimumCount = 0;
2132 req->Length = cpu_to_le32(io_parms->length);
2133 req->Offset = cpu_to_le64(io_parms->offset);
2134
2135 if (request_type & CHAINED_REQUEST) {
2136 if (!(request_type & END_OF_CHAIN)) {
2137 /* 4 for rfc1002 length field */
2138 req->hdr.NextCommand =
2139 cpu_to_le32(get_rfc1002_length(req) + 4);
2140 } else /* END_OF_CHAIN */
2141 req->hdr.NextCommand = 0;
2142 if (request_type & RELATED_REQUEST) {
2143 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2144 /*
2145 * Related requests use info from previous read request
2146 * in chain.
2147 */
2148 req->hdr.SessionId = 0xFFFFFFFF;
2149 req->hdr.TreeId = 0xFFFFFFFF;
2150 req->PersistentFileId = 0xFFFFFFFF;
2151 req->VolatileFileId = 0xFFFFFFFF;
2152 }
2153 }
2154 if (remaining_bytes > io_parms->length)
2155 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2156 else
2157 req->RemainingBytes = 0;
2158
2159 iov[0].iov_base = (char *)req;
2160 /* 4 for rfc1002 length field */
2161 iov[0].iov_len = get_rfc1002_length(req) + 4;
2162 return rc;
2163}
2164
2165static void
2166smb2_readv_callback(struct mid_q_entry *mid)
2167{
2168 struct cifs_readdata *rdata = mid->callback_data;
2169 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2170 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07002171 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002172 unsigned int credits_received = 1;
Jeff Layton58195752012-09-19 06:22:34 -07002173 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Layton8321fec2012-09-19 06:22:32 -07002174 .rq_nvec = 1,
2175 .rq_pages = rdata->pages,
2176 .rq_npages = rdata->nr_pages,
2177 .rq_pagesz = rdata->pagesz,
2178 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002179
Joe Perchesf96637b2013-05-04 22:12:25 -05002180 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2181 __func__, mid->mid, mid->mid_state, rdata->result,
2182 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002183
2184 switch (mid->mid_state) {
2185 case MID_RESPONSE_RECEIVED:
2186 credits_received = le16_to_cpu(buf->CreditRequest);
2187 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04002188 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002189 int rc;
2190
Jeff Layton0b688cf2012-09-18 16:20:34 -07002191 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002192 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002193 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2194 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002195 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002196 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002197 task_io_account_read(rdata->got_bytes);
2198 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002199 break;
2200 case MID_REQUEST_SUBMITTED:
2201 case MID_RETRY_NEEDED:
2202 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002203 if (server->sign && rdata->got_bytes)
2204 /* reset bytes number since we can not check a sign */
2205 rdata->got_bytes = 0;
2206 /* FIXME: should this be counted toward the initiating task? */
2207 task_io_account_read(rdata->got_bytes);
2208 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002209 break;
2210 default:
2211 if (rdata->result != -ENODATA)
2212 rdata->result = -EIO;
2213 }
2214
2215 if (rdata->result)
2216 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2217
2218 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002219 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002220 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002221 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002222 add_credits(server, credits_received, 0);
2223}
2224
2225/* smb2_async_readv - send an async write, and set up mid to handle result */
2226int
2227smb2_async_readv(struct cifs_readdata *rdata)
2228{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002229 int rc, flags = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002230 struct smb2_hdr *buf;
2231 struct cifs_io_parms io_parms;
Jeff Layton58195752012-09-19 06:22:34 -07002232 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07002233 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002234 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002235
Joe Perchesf96637b2013-05-04 22:12:25 -05002236 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2237 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002238
2239 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2240 io_parms.offset = rdata->offset;
2241 io_parms.length = rdata->bytes;
2242 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2243 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2244 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002245
2246 server = io_parms.tcon->ses->server;
2247
Jeff Layton58195752012-09-19 06:22:34 -07002248 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002249 if (rc) {
2250 if (rc == -EAGAIN && rdata->credits) {
2251 /* credits was reset by reconnect */
2252 rdata->credits = 0;
2253 /* reduce in_flight value since we won't send the req */
2254 spin_lock(&server->req_lock);
2255 server->in_flight--;
2256 spin_unlock(&server->req_lock);
2257 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002258 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002259 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002260
Jeff Layton58195752012-09-19 06:22:34 -07002261 buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002262 /* 4 for rfc1002 length field */
Jeff Layton58195752012-09-19 06:22:34 -07002263 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002264
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002265 if (rdata->credits) {
2266 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2267 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002268 buf->CreditRequest = buf->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002269 spin_lock(&server->req_lock);
2270 server->credits += rdata->credits -
2271 le16_to_cpu(buf->CreditCharge);
2272 spin_unlock(&server->req_lock);
2273 wake_up(&server->request_q);
2274 flags = CIFS_HAS_CREDITS;
2275 }
2276
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002277 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002278 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002279 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002280 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002281 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002282 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002283 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2284 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002285
2286 cifs_small_buf_release(buf);
2287 return rc;
2288}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002289
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002290int
2291SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2292 unsigned int *nbytes, char **buf, int *buf_type)
2293{
2294 int resp_buftype, rc = -EACCES;
2295 struct smb2_read_rsp *rsp = NULL;
2296 struct kvec iov[1];
2297
2298 *nbytes = 0;
2299 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2300 if (rc)
2301 return rc;
2302
2303 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2304 &resp_buftype, CIFS_LOG_ERROR);
2305
2306 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2307
2308 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2309 free_rsp_buf(resp_buftype, iov[0].iov_base);
2310 return 0;
2311 }
2312
2313 if (rc) {
2314 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002315 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002316 } else {
2317 *nbytes = le32_to_cpu(rsp->DataLength);
2318 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2319 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002320 cifs_dbg(FYI, "bad length %d for count %d\n",
2321 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002322 rc = -EIO;
2323 *nbytes = 0;
2324 }
2325 }
2326
2327 if (*buf) {
Steve French373512e2015-12-18 13:05:30 -06002328 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002329 *nbytes);
2330 free_rsp_buf(resp_buftype, iov[0].iov_base);
2331 } else if (resp_buftype != CIFS_NO_BUFFER) {
2332 *buf = iov[0].iov_base;
2333 if (resp_buftype == CIFS_SMALL_BUFFER)
2334 *buf_type = CIFS_SMALL_BUFFER;
2335 else if (resp_buftype == CIFS_LARGE_BUFFER)
2336 *buf_type = CIFS_LARGE_BUFFER;
2337 }
2338 return rc;
2339}
2340
Pavel Shilovsky33319142012-09-18 16:20:29 -07002341/*
2342 * Check the mid_state and signature on received buffer (if any), and queue the
2343 * workqueue completion task.
2344 */
2345static void
2346smb2_writev_callback(struct mid_q_entry *mid)
2347{
2348 struct cifs_writedata *wdata = mid->callback_data;
2349 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002350 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002351 unsigned int written;
2352 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2353 unsigned int credits_received = 1;
2354
2355 switch (mid->mid_state) {
2356 case MID_RESPONSE_RECEIVED:
2357 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2358 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2359 if (wdata->result != 0)
2360 break;
2361
2362 written = le32_to_cpu(rsp->DataLength);
2363 /*
2364 * Mask off high 16 bits when bytes written as returned
2365 * by the server is greater than bytes requested by the
2366 * client. OS/2 servers are known to set incorrect
2367 * CountHigh values.
2368 */
2369 if (written > wdata->bytes)
2370 written &= 0xFFFF;
2371
2372 if (written < wdata->bytes)
2373 wdata->result = -ENOSPC;
2374 else
2375 wdata->bytes = written;
2376 break;
2377 case MID_REQUEST_SUBMITTED:
2378 case MID_RETRY_NEEDED:
2379 wdata->result = -EAGAIN;
2380 break;
2381 default:
2382 wdata->result = -EIO;
2383 break;
2384 }
2385
2386 if (wdata->result)
2387 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2388
2389 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002390 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002391 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002392 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002393 add_credits(tcon->ses->server, credits_received, 0);
2394}
2395
2396/* smb2_async_writev - send an async write, and set up mid to handle result */
2397int
Steve French4a5c80d2014-02-07 20:45:12 -06002398smb2_async_writev(struct cifs_writedata *wdata,
2399 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002400{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002401 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002402 struct smb2_write_req *req = NULL;
2403 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002404 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002405 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002406 struct smb_rqst rqst;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002407
2408 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002409 if (rc) {
2410 if (rc == -EAGAIN && wdata->credits) {
2411 /* credits was reset by reconnect */
2412 wdata->credits = 0;
2413 /* reduce in_flight value since we won't send the req */
2414 spin_lock(&server->req_lock);
2415 server->in_flight--;
2416 spin_unlock(&server->req_lock);
2417 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002418 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002419 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002420
Pavel Shilovsky33319142012-09-18 16:20:29 -07002421 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2422
2423 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2424 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2425 req->WriteChannelInfoOffset = 0;
2426 req->WriteChannelInfoLength = 0;
2427 req->Channel = 0;
2428 req->Offset = cpu_to_le64(wdata->offset);
2429 /* 4 for rfc1002 length field */
2430 req->DataOffset = cpu_to_le16(
2431 offsetof(struct smb2_write_req, Buffer) - 4);
2432 req->RemainingBytes = 0;
2433
2434 /* 4 for rfc1002 length field and 1 for Buffer */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002435 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2436 iov.iov_base = req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002437
Jeff Laytoneddb0792012-09-18 16:20:35 -07002438 rqst.rq_iov = &iov;
2439 rqst.rq_nvec = 1;
2440 rqst.rq_pages = wdata->pages;
2441 rqst.rq_npages = wdata->nr_pages;
2442 rqst.rq_pagesz = wdata->pagesz;
2443 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002444
Joe Perchesf96637b2013-05-04 22:12:25 -05002445 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2446 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002447
2448 req->Length = cpu_to_le32(wdata->bytes);
2449
2450 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2451
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002452 if (wdata->credits) {
2453 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2454 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002455 req->hdr.CreditRequest = req->hdr.CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002456 spin_lock(&server->req_lock);
2457 server->credits += wdata->credits -
2458 le16_to_cpu(req->hdr.CreditCharge);
2459 spin_unlock(&server->req_lock);
2460 wake_up(&server->request_q);
2461 flags = CIFS_HAS_CREDITS;
2462 }
2463
Pavel Shilovsky33319142012-09-18 16:20:29 -07002464 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002465 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2466 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002467
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002468 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002469 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002470 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2471 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002472
Pavel Shilovsky33319142012-09-18 16:20:29 -07002473async_writev_out:
2474 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002475 return rc;
2476}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002477
2478/*
2479 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2480 * The length field from io_parms must be at least 1 and indicates a number of
2481 * elements with data to write that begins with position 1 in iov array. All
2482 * data length is specified by count.
2483 */
2484int
2485SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2486 unsigned int *nbytes, struct kvec *iov, int n_vec)
2487{
2488 int rc = 0;
2489 struct smb2_write_req *req = NULL;
2490 struct smb2_write_rsp *rsp = NULL;
2491 int resp_buftype;
2492 *nbytes = 0;
2493
2494 if (n_vec < 1)
2495 return rc;
2496
2497 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2498 if (rc)
2499 return rc;
2500
2501 if (io_parms->tcon->ses->server == NULL)
2502 return -ECONNABORTED;
2503
2504 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2505
2506 req->PersistentFileId = io_parms->persistent_fid;
2507 req->VolatileFileId = io_parms->volatile_fid;
2508 req->WriteChannelInfoOffset = 0;
2509 req->WriteChannelInfoLength = 0;
2510 req->Channel = 0;
2511 req->Length = cpu_to_le32(io_parms->length);
2512 req->Offset = cpu_to_le64(io_parms->offset);
2513 /* 4 for rfc1002 length field */
2514 req->DataOffset = cpu_to_le16(
2515 offsetof(struct smb2_write_req, Buffer) - 4);
2516 req->RemainingBytes = 0;
2517
2518 iov[0].iov_base = (char *)req;
2519 /* 4 for rfc1002 length field and 1 for Buffer */
2520 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2521
2522 /* length of entire message including data to be written */
2523 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2524
2525 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2526 &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002527 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002528
2529 if (rc) {
2530 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002531 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002532 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002533 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002534
2535 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002536 return rc;
2537}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002538
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002539static unsigned int
2540num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2541{
2542 int len;
2543 unsigned int entrycount = 0;
2544 unsigned int next_offset = 0;
2545 FILE_DIRECTORY_INFO *entryptr;
2546
2547 if (bufstart == NULL)
2548 return 0;
2549
2550 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2551
2552 while (1) {
2553 entryptr = (FILE_DIRECTORY_INFO *)
2554 ((char *)entryptr + next_offset);
2555
2556 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002557 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002558 break;
2559 }
2560
2561 len = le32_to_cpu(entryptr->FileNameLength);
2562 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002563 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2564 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002565 break;
2566 }
2567
2568 *lastentry = (char *)entryptr;
2569 entrycount++;
2570
2571 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2572 if (!next_offset)
2573 break;
2574 }
2575
2576 return entrycount;
2577}
2578
2579/*
2580 * Readdir/FindFirst
2581 */
2582int
2583SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2584 u64 persistent_fid, u64 volatile_fid, int index,
2585 struct cifs_search_info *srch_inf)
2586{
2587 struct smb2_query_directory_req *req;
2588 struct smb2_query_directory_rsp *rsp = NULL;
2589 struct kvec iov[2];
2590 int rc = 0;
2591 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002592 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002593 unsigned char *bufptr;
2594 struct TCP_Server_Info *server;
2595 struct cifs_ses *ses = tcon->ses;
2596 __le16 asteriks = cpu_to_le16('*');
2597 char *end_of_smb;
2598 unsigned int output_size = CIFSMaxBufSize;
2599 size_t info_buf_size;
2600
2601 if (ses && (ses->server))
2602 server = ses->server;
2603 else
2604 return -EIO;
2605
2606 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2607 if (rc)
2608 return rc;
2609
2610 switch (srch_inf->info_level) {
2611 case SMB_FIND_FILE_DIRECTORY_INFO:
2612 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2613 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2614 break;
2615 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2616 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2617 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2618 break;
2619 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002620 cifs_dbg(VFS, "info level %u isn't supported\n",
2621 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002622 rc = -EINVAL;
2623 goto qdir_exit;
2624 }
2625
2626 req->FileIndex = cpu_to_le32(index);
2627 req->PersistentFileId = persistent_fid;
2628 req->VolatileFileId = volatile_fid;
2629
2630 len = 0x2;
2631 bufptr = req->Buffer;
2632 memcpy(bufptr, &asteriks, len);
2633
2634 req->FileNameOffset =
2635 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2636 req->FileNameLength = cpu_to_le16(len);
2637 /*
2638 * BB could be 30 bytes or so longer if we used SMB2 specific
2639 * buffer lengths, but this is safe and close enough.
2640 */
2641 output_size = min_t(unsigned int, output_size, server->maxBuf);
2642 output_size = min_t(unsigned int, output_size, 2 << 15);
2643 req->OutputBufferLength = cpu_to_le32(output_size);
2644
2645 iov[0].iov_base = (char *)req;
2646 /* 4 for RFC1001 length and 1 for Buffer */
2647 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2648
2649 iov[1].iov_base = (char *)(req->Buffer);
2650 iov[1].iov_len = len;
2651
2652 inc_rfc1001_len(req, len - 1 /* Buffer */);
2653
2654 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002655 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2656
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002657 if (rc) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002658 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2659 srch_inf->endOfSearch = true;
2660 rc = 0;
2661 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002662 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2663 goto qdir_exit;
2664 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002665
2666 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2667 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2668 info_buf_size);
2669 if (rc)
2670 goto qdir_exit;
2671
2672 srch_inf->unicode = true;
2673
2674 if (srch_inf->ntwrk_buf_start) {
2675 if (srch_inf->smallBuf)
2676 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2677 else
2678 cifs_buf_release(srch_inf->ntwrk_buf_start);
2679 }
2680 srch_inf->ntwrk_buf_start = (char *)rsp;
2681 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2682 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2683 /* 4 for rfc1002 length field */
2684 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2685 srch_inf->entries_in_buffer =
2686 num_entries(srch_inf->srch_entries_start, end_of_smb,
2687 &srch_inf->last_entry, info_buf_size);
2688 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002689 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2690 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2691 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002692 if (resp_buftype == CIFS_LARGE_BUFFER)
2693 srch_inf->smallBuf = false;
2694 else if (resp_buftype == CIFS_SMALL_BUFFER)
2695 srch_inf->smallBuf = true;
2696 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002697 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002698
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002699 return rc;
2700
2701qdir_exit:
2702 free_rsp_buf(resp_buftype, rsp);
2703 return rc;
2704}
2705
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002706static int
2707send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002708 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002709 unsigned int num, void **data, unsigned int *size)
2710{
2711 struct smb2_set_info_req *req;
2712 struct smb2_set_info_rsp *rsp = NULL;
2713 struct kvec *iov;
2714 int rc = 0;
2715 int resp_buftype;
2716 unsigned int i;
2717 struct TCP_Server_Info *server;
2718 struct cifs_ses *ses = tcon->ses;
2719
2720 if (ses && (ses->server))
2721 server = ses->server;
2722 else
2723 return -EIO;
2724
2725 if (!num)
2726 return -EINVAL;
2727
2728 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2729 if (!iov)
2730 return -ENOMEM;
2731
2732 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2733 if (rc) {
2734 kfree(iov);
2735 return rc;
2736 }
2737
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002738 req->hdr.ProcessId = cpu_to_le32(pid);
2739
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002740 req->InfoType = SMB2_O_INFO_FILE;
2741 req->FileInfoClass = info_class;
2742 req->PersistentFileId = persistent_fid;
2743 req->VolatileFileId = volatile_fid;
2744
2745 /* 4 for RFC1001 length and 1 for Buffer */
2746 req->BufferOffset =
2747 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2748 req->BufferLength = cpu_to_le32(*size);
2749
2750 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2751
2752 memcpy(req->Buffer, *data, *size);
2753
2754 iov[0].iov_base = (char *)req;
2755 /* 4 for RFC1001 length */
2756 iov[0].iov_len = get_rfc1002_length(req) + 4;
2757
2758 for (i = 1; i < num; i++) {
2759 inc_rfc1001_len(req, size[i]);
2760 le32_add_cpu(&req->BufferLength, size[i]);
2761 iov[i].iov_base = (char *)data[i];
2762 iov[i].iov_len = size[i];
2763 }
2764
2765 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2766 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2767
Steve French7d3fb242013-11-18 09:56:28 -06002768 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002769 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002770
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002771 free_rsp_buf(resp_buftype, rsp);
2772 kfree(iov);
2773 return rc;
2774}
2775
2776int
2777SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2778 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2779{
2780 struct smb2_file_rename_info info;
2781 void **data;
2782 unsigned int size[2];
2783 int rc;
2784 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2785
2786 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2787 if (!data)
2788 return -ENOMEM;
2789
2790 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2791 /* 0 = fail if target already exists */
2792 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2793 info.FileNameLength = cpu_to_le32(len);
2794
2795 data[0] = &info;
2796 size[0] = sizeof(struct smb2_file_rename_info);
2797
2798 data[1] = target_file;
2799 size[1] = len + 2 /* null */;
2800
2801 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002802 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2803 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002804 kfree(data);
2805 return rc;
2806}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002807
2808int
Steve French897fba12016-05-12 21:20:36 -05002809SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2810 u64 persistent_fid, u64 volatile_fid)
2811{
2812 __u8 delete_pending = 1;
2813 void *data;
2814 unsigned int size;
2815
2816 data = &delete_pending;
2817 size = 1; /* sizeof __u8 */
2818
2819 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2820 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2821 &size);
2822}
2823
2824int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002825SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2826 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2827{
2828 struct smb2_file_link_info info;
2829 void **data;
2830 unsigned int size[2];
2831 int rc;
2832 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2833
2834 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2835 if (!data)
2836 return -ENOMEM;
2837
2838 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2839 /* 0 = fail if link already exists */
2840 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2841 info.FileNameLength = cpu_to_le32(len);
2842
2843 data[0] = &info;
2844 size[0] = sizeof(struct smb2_file_link_info);
2845
2846 data[1] = target_file;
2847 size[1] = len + 2 /* null */;
2848
2849 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002850 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002851 kfree(data);
2852 return rc;
2853}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002854
2855int
2856SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002857 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002858{
2859 struct smb2_file_eof_info info;
2860 void *data;
2861 unsigned int size;
2862
2863 info.EndOfFile = *eof;
2864
2865 data = &info;
2866 size = sizeof(struct smb2_file_eof_info);
2867
Steve Frenchf29ebb42014-07-19 21:44:58 -05002868 if (is_falloc)
2869 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2870 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2871 else
2872 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2873 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002874}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002875
2876int
2877SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2878 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2879{
2880 unsigned int size;
2881 size = sizeof(FILE_BASIC_INFO);
2882 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2883 current->tgid, FILE_BASIC_INFORMATION, 1,
2884 (void **)&buf, &size);
2885}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002886
2887int
2888SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2889 const u64 persistent_fid, const u64 volatile_fid,
2890 __u8 oplock_level)
2891{
2892 int rc;
2893 struct smb2_oplock_break *req = NULL;
2894
Joe Perchesf96637b2013-05-04 22:12:25 -05002895 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002896 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2897
2898 if (rc)
2899 return rc;
2900
2901 req->VolatileFid = volatile_fid;
2902 req->PersistentFid = persistent_fid;
2903 req->OplockLevel = oplock_level;
2904 req->hdr.CreditRequest = cpu_to_le16(1);
2905
2906 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2907 /* SMB2 buffer freed by function above */
2908
2909 if (rc) {
2910 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002911 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002912 }
2913
2914 return rc;
2915}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002916
2917static void
2918copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2919 struct kstatfs *kst)
2920{
2921 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2922 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2923 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2924 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2925 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2926 return;
2927}
2928
2929static int
2930build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2931 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2932{
2933 int rc;
2934 struct smb2_query_info_req *req;
2935
Joe Perchesf96637b2013-05-04 22:12:25 -05002936 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002937
2938 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2939 return -EIO;
2940
2941 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2942 if (rc)
2943 return rc;
2944
2945 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2946 req->FileInfoClass = level;
2947 req->PersistentFileId = persistent_fid;
2948 req->VolatileFileId = volatile_fid;
2949 /* 4 for rfc1002 length field and 1 for pad */
2950 req->InputBufferOffset =
2951 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2952 req->OutputBufferLength = cpu_to_le32(
2953 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2954
2955 iov->iov_base = (char *)req;
2956 /* 4 for rfc1002 length field */
2957 iov->iov_len = get_rfc1002_length(req) + 4;
2958 return 0;
2959}
2960
2961int
2962SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2963 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2964{
2965 struct smb2_query_info_rsp *rsp = NULL;
2966 struct kvec iov;
2967 int rc = 0;
2968 int resp_buftype;
2969 struct cifs_ses *ses = tcon->ses;
2970 struct smb2_fs_full_size_info *info = NULL;
2971
2972 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2973 sizeof(struct smb2_fs_full_size_info),
2974 persistent_fid, volatile_fid);
2975 if (rc)
2976 return rc;
2977
2978 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2979 if (rc) {
2980 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05002981 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002982 }
2983 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2984
2985 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2986 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2987 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2988 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2989 sizeof(struct smb2_fs_full_size_info));
2990 if (!rc)
2991 copy_fs_info_to_kstatfs(info, fsdata);
2992
Steve French34f62642013-10-09 02:07:00 -05002993qfsinf_exit:
2994 free_rsp_buf(resp_buftype, iov.iov_base);
2995 return rc;
2996}
2997
2998int
2999SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003000 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003001{
3002 struct smb2_query_info_rsp *rsp = NULL;
3003 struct kvec iov;
3004 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003005 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003006 struct cifs_ses *ses = tcon->ses;
3007 unsigned int rsp_len, offset;
3008
Steven French21671142013-10-09 13:36:35 -05003009 if (level == FS_DEVICE_INFORMATION) {
3010 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3011 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3012 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3013 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3014 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003015 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3016 max_len = sizeof(struct smb3_fs_ss_info);
3017 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003018 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003019 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003020 return -EINVAL;
3021 }
3022
3023 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003024 persistent_fid, volatile_fid);
3025 if (rc)
3026 return rc;
3027
3028 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3029 if (rc) {
3030 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3031 goto qfsattr_exit;
3032 }
3033 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3034
3035 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3036 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003037 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3038 if (rc)
3039 goto qfsattr_exit;
3040
3041 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003042 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3043 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003044 rsp_len, max_len));
3045 else if (level == FS_DEVICE_INFORMATION)
3046 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3047 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003048 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3049 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3050 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3051 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3052 tcon->perf_sector_size =
3053 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3054 }
Steve French34f62642013-10-09 02:07:00 -05003055
3056qfsattr_exit:
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003057 free_rsp_buf(resp_buftype, iov.iov_base);
3058 return rc;
3059}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003060
3061int
3062smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3063 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3064 const __u32 num_lock, struct smb2_lock_element *buf)
3065{
3066 int rc = 0;
3067 struct smb2_lock_req *req = NULL;
3068 struct kvec iov[2];
3069 int resp_buf_type;
3070 unsigned int count;
3071
Joe Perchesf96637b2013-05-04 22:12:25 -05003072 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003073
3074 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3075 if (rc)
3076 return rc;
3077
3078 req->hdr.ProcessId = cpu_to_le32(pid);
3079 req->LockCount = cpu_to_le16(num_lock);
3080
3081 req->PersistentFileId = persist_fid;
3082 req->VolatileFileId = volatile_fid;
3083
3084 count = num_lock * sizeof(struct smb2_lock_element);
3085 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3086
3087 iov[0].iov_base = (char *)req;
3088 /* 4 for rfc1002 length field and count for all locks */
3089 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3090 iov[1].iov_base = (char *)buf;
3091 iov[1].iov_len = count;
3092
3093 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3094 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
3095 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003096 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003097 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3098 }
3099
3100 return rc;
3101}
3102
3103int
3104SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3105 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3106 const __u64 length, const __u64 offset, const __u32 lock_flags,
3107 const bool wait)
3108{
3109 struct smb2_lock_element lock;
3110
3111 lock.Offset = cpu_to_le64(offset);
3112 lock.Length = cpu_to_le64(length);
3113 lock.Flags = cpu_to_le32(lock_flags);
3114 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3115 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3116
3117 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3118}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003119
3120int
3121SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3122 __u8 *lease_key, const __le32 lease_state)
3123{
3124 int rc;
3125 struct smb2_lease_ack *req = NULL;
3126
Joe Perchesf96637b2013-05-04 22:12:25 -05003127 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003128 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3129
3130 if (rc)
3131 return rc;
3132
3133 req->hdr.CreditRequest = cpu_to_le16(1);
3134 req->StructureSize = cpu_to_le16(36);
3135 inc_rfc1001_len(req, 12);
3136
3137 memcpy(req->LeaseKey, lease_key, 16);
3138 req->LeaseState = lease_state;
3139
3140 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3141 /* SMB2 buffer freed by function above */
3142
3143 if (rc) {
3144 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003145 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003146 }
3147
3148 return rc;
3149}