blob: 4d944c4c55a825241118756c9c0776b42378d196 [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. */
108 if (server->credits >= SMB2_MAX_CREDITS_AVAILABLE)
109 hdr->CreditRequest = cpu_to_le16(0);
110 else
111 hdr->CreditRequest = cpu_to_le16(
112 min_t(int, SMB2_MAX_CREDITS_AVAILABLE -
113 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
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400596int
597SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
598 const struct nls_table *nls_cp)
599{
600 struct smb2_sess_setup_req *req;
601 struct smb2_sess_setup_rsp *rsp = NULL;
602 struct kvec iov[2];
603 int rc = 0;
Namjae Jeon7de975e2014-08-20 19:39:41 +0900604 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400605 __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
Jeff Layton3534b852013-05-24 07:41:01 -0400606 struct TCP_Server_Info *server = ses->server;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400607 u16 blob_length = 0;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500608 struct key *spnego_key = NULL;
609 char *security_blob = NULL;
Jerome Marchandb8da3442016-05-26 11:52:25 +0200610 unsigned char *ntlmssp_blob = NULL;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400611 bool use_spnego = false; /* else use raw ntlmssp */
Steve Frenchc2afb812016-09-20 22:56:13 -0500612 u64 previous_session = ses->Suid;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400613
Joe Perchesf96637b2013-05-04 22:12:25 -0500614 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400615
Jeff Layton3534b852013-05-24 07:41:01 -0400616 if (!server) {
617 WARN(1, "%s: server is NULL!\n", __func__);
618 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400619 }
620
621 /*
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500622 * If we are here due to reconnect, free per-smb session key
623 * in case signing was required.
624 */
625 kfree(ses->auth_key.response);
626 ses->auth_key.response = NULL;
627
628 /*
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400629 * If memory allocation is successful, caller of this function
630 * frees it.
631 */
632 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
633 if (!ses->ntlmssp)
634 return -ENOMEM;
Shirish Pargaonkar5c234aa2013-08-29 08:35:10 -0500635 ses->ntlmssp->sesskey_per_smbsess = true;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400636
Jeff Layton3f618222013-06-12 19:52:14 -0500637 /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500638 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
639 ses->sectype = RawNTLMSSP;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400640
641ssetup_ntlmssp_authenticate:
642 if (phase == NtLmChallenge)
643 phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
644
645 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
646 if (rc)
647 return rc;
648
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400649 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
Steve Frenchc2afb812016-09-20 22:56:13 -0500650
651 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
652 req->PreviousSessionId = previous_session;
653
Steve Frencheed0e172015-02-06 00:03:52 -0600654 req->Flags = 0; /* MBZ */
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400655 /* to enable echos and oplocks */
656 req->hdr.CreditRequest = cpu_to_le16(3);
657
658 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400659 if (server->sign)
660 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
661 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
662 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
663 else
664 req->SecurityMode = 0;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400665
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400666 req->Capabilities = 0;
667 req->Channel = 0; /* MBZ */
668
669 iov[0].iov_base = (char *)req;
670 /* 4 for rfc1002 length field and 1 for pad */
671 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500672
673 if (ses->sectype == Kerberos) {
674#ifdef CONFIG_CIFS_UPCALL
675 struct cifs_spnego_msg *msg;
676
677 spnego_key = cifs_get_spnego_key(ses);
678 if (IS_ERR(spnego_key)) {
679 rc = PTR_ERR(spnego_key);
680 spnego_key = NULL;
681 goto ssetup_exit;
682 }
683
David Howells146aa8b2015-10-21 14:04:48 +0100684 msg = spnego_key->payload.data[0];
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500685 /*
686 * check version field to make sure that cifs.upcall is
687 * sending us a response in an expected form
688 */
689 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
690 cifs_dbg(VFS,
691 "bad cifs.upcall version. Expected %d got %d",
692 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
693 rc = -EKEYREJECTED;
694 goto ssetup_exit;
695 }
696 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
697 GFP_KERNEL);
698 if (!ses->auth_key.response) {
699 cifs_dbg(VFS,
700 "Kerberos can't allocate (%u bytes) memory",
701 msg->sesskey_len);
702 rc = -ENOMEM;
703 goto ssetup_exit;
704 }
705 ses->auth_key.len = msg->sesskey_len;
706 blob_length = msg->secblob_len;
707 iov[1].iov_base = msg->data + msg->sesskey_len;
708 iov[1].iov_len = blob_length;
709#else
710 rc = -EOPNOTSUPP;
711 goto ssetup_exit;
712#endif /* CONFIG_CIFS_UPCALL */
713 } else if (phase == NtLmNegotiate) { /* if not krb5 must be ntlmssp */
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400714 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
715 GFP_KERNEL);
716 if (ntlmssp_blob == NULL) {
717 rc = -ENOMEM;
718 goto ssetup_exit;
719 }
720 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
721 if (use_spnego) {
722 /* blob_length = build_spnego_ntlmssp_blob(
723 &security_blob,
724 sizeof(struct _NEGOTIATE_MESSAGE),
725 ntlmssp_blob); */
726 /* BB eventually need to add this */
Joe Perchesf96637b2013-05-04 22:12:25 -0500727 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400728 rc = -EOPNOTSUPP;
729 kfree(ntlmssp_blob);
730 goto ssetup_exit;
731 } else {
732 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
733 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
734 security_blob = ntlmssp_blob;
735 }
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500736 iov[1].iov_base = security_blob;
737 iov[1].iov_len = blob_length;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400738 } else if (phase == NtLmAuthenticate) {
739 req->hdr.SessionId = ses->Suid;
Jerome Marchandb8da3442016-05-26 11:52:25 +0200740 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400741 nls_cp);
742 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500743 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
744 rc);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400745 goto ssetup_exit; /* BB double check error handling */
746 }
747 if (use_spnego) {
748 /* blob_length = build_spnego_ntlmssp_blob(
749 &security_blob,
750 blob_length,
751 ntlmssp_blob); */
Joe Perchesf96637b2013-05-04 22:12:25 -0500752 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400753 rc = -EOPNOTSUPP;
754 kfree(ntlmssp_blob);
755 goto ssetup_exit;
756 } else {
757 security_blob = ntlmssp_blob;
758 }
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500759 iov[1].iov_base = security_blob;
760 iov[1].iov_len = blob_length;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400761 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -0500762 cifs_dbg(VFS, "illegal ntlmssp phase\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400763 rc = -EIO;
764 goto ssetup_exit;
765 }
766
767 /* Testing shows that buffer offset must be at location of Buffer[0] */
768 req->SecurityBufferOffset =
769 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
770 1 /* pad */ - 4 /* rfc1001 len */);
771 req->SecurityBufferLength = cpu_to_le16(blob_length);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400772
773 inc_rfc1001_len(req, blob_length - 1 /* pad */);
774
775 /* BB add code to build os and lm fields */
776
Steve French6d8b59d2012-12-08 22:36:29 -0600777 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
778 CIFS_LOG_ERROR | CIFS_NEG_OP);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400779
780 kfree(security_blob);
781 rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500782 ses->Suid = rsp->hdr.SessionId;
Pavel Shilovsky4ca3a992012-09-25 11:00:09 +0400783 if (resp_buftype != CIFS_NO_BUFFER &&
784 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400785 if (phase != NtLmNegotiate) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500786 cifs_dbg(VFS, "Unexpected more processing error\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400787 goto ssetup_exit;
788 }
789 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
Pavel Shilovsky4ca3a992012-09-25 11:00:09 +0400790 le16_to_cpu(rsp->SecurityBufferOffset)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500791 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
792 le16_to_cpu(rsp->SecurityBufferOffset));
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400793 rc = -EIO;
794 goto ssetup_exit;
795 }
796
797 /* NTLMSSP Negotiate sent now processing challenge (response) */
798 phase = NtLmChallenge; /* process ntlmssp challenge */
799 rc = 0; /* MORE_PROCESSING is not an error here but expected */
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400800 rc = decode_ntlmssp_challenge(rsp->Buffer,
801 le16_to_cpu(rsp->SecurityBufferLength), ses);
802 }
803
804 /*
805 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
806 * but at least the raw NTLMSSP case works.
807 */
808 /*
809 * No tcon so can't do
810 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
811 */
812 if (rc != 0)
813 goto ssetup_exit;
814
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400815 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Steve French0cbaa532013-11-15 23:50:24 -0600816 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
817 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400818ssetup_exit:
819 free_rsp_buf(resp_buftype, rsp);
820
821 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
822 if ((phase == NtLmChallenge) && (rc == 0))
823 goto ssetup_ntlmssp_authenticate;
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500824
825 if (!rc) {
826 mutex_lock(&server->srv_mutex);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500827 if (server->sign && server->ops->generate_signingkey) {
828 rc = server->ops->generate_signingkey(ses);
829 kfree(ses->auth_key.response);
830 ses->auth_key.response = NULL;
831 if (rc) {
832 cifs_dbg(FYI,
833 "SMB3 session key generation failed\n");
834 mutex_unlock(&server->srv_mutex);
835 goto keygen_exit;
836 }
837 }
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500838 if (!server->session_estab) {
839 server->sequence_number = 0x2;
840 server->session_estab = true;
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500841 }
842 mutex_unlock(&server->srv_mutex);
843
844 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
845 spin_lock(&GlobalMid_Lock);
846 ses->status = CifsGood;
847 ses->need_reconnect = false;
848 spin_unlock(&GlobalMid_Lock);
849 }
850
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500851keygen_exit:
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500852 if (!server->sign) {
853 kfree(ses->auth_key.response);
854 ses->auth_key.response = NULL;
855 }
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500856 if (spnego_key) {
857 key_invalidate(spnego_key);
858 key_put(spnego_key);
859 }
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500860 kfree(ses->ntlmssp);
861
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400862 return rc;
863}
864
865int
866SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
867{
868 struct smb2_logoff_req *req; /* response is also trivial struct */
869 int rc = 0;
870 struct TCP_Server_Info *server;
871
Joe Perchesf96637b2013-05-04 22:12:25 -0500872 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400873
874 if (ses && (ses->server))
875 server = ses->server;
876 else
877 return -EIO;
878
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500879 /* no need to send SMB logoff if uid already closed due to reconnect */
880 if (ses->need_reconnect)
881 goto smb2_session_already_dead;
882
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400883 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
884 if (rc)
885 return rc;
886
887 /* since no tcon, smb2_init can not do this, so do here */
888 req->hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -0400889 if (server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700890 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400891
892 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
893 /*
894 * No tcon so can't do
895 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
896 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500897
898smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400899 return rc;
900}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400901
902static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
903{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400904 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400905}
906
907#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
908
Steve Frenchde9f68d2013-11-15 11:26:24 -0600909/* These are similar values to what Windows uses */
910static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
911{
912 tcon->max_chunks = 256;
913 tcon->max_bytes_chunk = 1048576;
914 tcon->max_bytes_copy = 16777216;
915}
916
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400917int
918SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
919 struct cifs_tcon *tcon, const struct nls_table *cp)
920{
921 struct smb2_tree_connect_req *req;
922 struct smb2_tree_connect_rsp *rsp = NULL;
923 struct kvec iov[2];
924 int rc = 0;
925 int resp_buftype;
926 int unc_path_len;
927 struct TCP_Server_Info *server;
928 __le16 *unc_path = NULL;
929
Joe Perchesf96637b2013-05-04 22:12:25 -0500930 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400931
932 if ((ses->server) && tree)
933 server = ses->server;
934 else
935 return -EIO;
936
937 if (tcon && tcon->bad_network_name)
938 return -ENOENT;
939
Steve Frenchff9f84b2015-09-26 09:48:58 -0500940 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -0500941 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
942 cifs_dbg(VFS, "encryption requested but no server support");
943 return -EOPNOTSUPP;
944 }
945
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400946 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
947 if (unc_path == NULL)
948 return -ENOMEM;
949
950 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
951 unc_path_len *= 2;
952 if (unc_path_len < 2) {
953 kfree(unc_path);
954 return -EINVAL;
955 }
956
957 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
958 if (rc) {
959 kfree(unc_path);
960 return rc;
961 }
962
963 if (tcon == NULL) {
964 /* since no tcon, smb2_init can not do this, so do here */
965 req->hdr.SessionId = ses->Suid;
966 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
967 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
968 }
969
970 iov[0].iov_base = (char *)req;
971 /* 4 for rfc1002 length field and 1 for pad */
972 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
973
974 /* Testing shows that buffer offset must be at location of Buffer[0] */
975 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
976 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
977 req->PathLength = cpu_to_le16(unc_path_len - 2);
978 iov[1].iov_base = unc_path;
979 iov[1].iov_len = unc_path_len;
980
981 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
982
983 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
984 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
985
986 if (rc != 0) {
987 if (tcon) {
988 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
989 tcon->need_reconnect = true;
990 }
991 goto tcon_error_exit;
992 }
993
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400994 if (tcon == NULL) {
995 ses->ipc_tid = rsp->hdr.TreeId;
996 goto tcon_exit;
997 }
998
999 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001000 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001001 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1002 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001003 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001004 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1005 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001006 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001007 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001008 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001009 rc = -EOPNOTSUPP;
1010 goto tcon_error_exit;
1011 }
1012
1013 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001014 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001015 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1016 tcon->tidStatus = CifsGood;
1017 tcon->need_reconnect = false;
1018 tcon->tid = rsp->hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001019 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001020
1021 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1022 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001023 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001024 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001025 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1026 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001027 if (tcon->ses->server->ops->validate_negotiate)
1028 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001029tcon_exit:
1030 free_rsp_buf(resp_buftype, rsp);
1031 kfree(unc_path);
1032 return rc;
1033
1034tcon_error_exit:
1035 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001036 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Steve French18f39e72014-08-17 00:22:24 -05001037 if (tcon)
1038 tcon->bad_network_name = true;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001039 }
1040 goto tcon_exit;
1041}
1042
1043int
1044SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1045{
1046 struct smb2_tree_disconnect_req *req; /* response is trivial */
1047 int rc = 0;
1048 struct TCP_Server_Info *server;
1049 struct cifs_ses *ses = tcon->ses;
1050
Joe Perchesf96637b2013-05-04 22:12:25 -05001051 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001052
1053 if (ses && (ses->server))
1054 server = ses->server;
1055 else
1056 return -EIO;
1057
1058 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1059 return 0;
1060
1061 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1062 if (rc)
1063 return rc;
1064
1065 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1066 if (rc)
1067 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1068
1069 return rc;
1070}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001071
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001072
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001073static struct create_durable *
1074create_durable_buf(void)
1075{
1076 struct create_durable *buf;
1077
1078 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1079 if (!buf)
1080 return NULL;
1081
1082 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001083 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001084 buf->ccontext.DataLength = cpu_to_le32(16);
1085 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1086 (struct create_durable, Name));
1087 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001088 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001089 buf->Name[0] = 'D';
1090 buf->Name[1] = 'H';
1091 buf->Name[2] = 'n';
1092 buf->Name[3] = 'Q';
1093 return buf;
1094}
1095
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001096static struct create_durable *
1097create_reconnect_durable_buf(struct cifs_fid *fid)
1098{
1099 struct create_durable *buf;
1100
1101 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1102 if (!buf)
1103 return NULL;
1104
1105 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1106 (struct create_durable, Data));
1107 buf->ccontext.DataLength = cpu_to_le32(16);
1108 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1109 (struct create_durable, Name));
1110 buf->ccontext.NameLength = cpu_to_le16(4);
1111 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1112 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001113 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001114 buf->Name[0] = 'D';
1115 buf->Name[1] = 'H';
1116 buf->Name[2] = 'n';
1117 buf->Name[3] = 'C';
1118 return buf;
1119}
1120
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001121static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001122parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1123 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001124{
1125 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001126 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001127 unsigned int next;
1128 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001129 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001130
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001131 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001132 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001133 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001134 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001135 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001136 if (le16_to_cpu(cc->NameLength) == 4 &&
1137 strncmp(name, "RqLs", 4) == 0)
1138 return server->ops->parse_lease_buf(cc, epoch);
1139
1140 next = le32_to_cpu(cc->Next);
1141 if (!next)
1142 break;
1143 remaining -= next;
1144 cc = (struct create_context *)((char *)cc + next);
1145 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001146
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001147 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001148}
1149
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001150static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001151add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1152 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001153{
1154 struct smb2_create_req *req = iov[0].iov_base;
1155 unsigned int num = *num_iovec;
1156
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001157 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001158 if (iov[num].iov_base == NULL)
1159 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001160 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001161 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1162 if (!req->CreateContextsOffset)
1163 req->CreateContextsOffset = cpu_to_le32(
1164 sizeof(struct smb2_create_req) - 4 +
1165 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001166 le32_add_cpu(&req->CreateContextsLength,
1167 server->vals->create_lease_size);
1168 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001169 *num_iovec = num + 1;
1170 return 0;
1171}
1172
Steve Frenchb56eae42015-11-03 09:26:27 -06001173static struct create_durable_v2 *
1174create_durable_v2_buf(struct cifs_fid *pfid)
1175{
1176 struct create_durable_v2 *buf;
1177
1178 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1179 if (!buf)
1180 return NULL;
1181
1182 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1183 (struct create_durable_v2, dcontext));
1184 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1185 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1186 (struct create_durable_v2, Name));
1187 buf->ccontext.NameLength = cpu_to_le16(4);
1188
1189 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1190 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001191 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001192 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1193
1194 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1195 buf->Name[0] = 'D';
1196 buf->Name[1] = 'H';
1197 buf->Name[2] = '2';
1198 buf->Name[3] = 'Q';
1199 return buf;
1200}
1201
1202static struct create_durable_handle_reconnect_v2 *
1203create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1204{
1205 struct create_durable_handle_reconnect_v2 *buf;
1206
1207 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1208 GFP_KERNEL);
1209 if (!buf)
1210 return NULL;
1211
1212 buf->ccontext.DataOffset =
1213 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1214 dcontext));
1215 buf->ccontext.DataLength =
1216 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1217 buf->ccontext.NameOffset =
1218 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1219 Name));
1220 buf->ccontext.NameLength = cpu_to_le16(4);
1221
1222 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1223 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1224 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1225 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1226
1227 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1228 buf->Name[0] = 'D';
1229 buf->Name[1] = 'H';
1230 buf->Name[2] = '2';
1231 buf->Name[3] = 'C';
1232 return buf;
1233}
1234
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001235static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001236add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001237 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001238{
1239 struct smb2_create_req *req = iov[0].iov_base;
1240 unsigned int num = *num_iovec;
1241
Steve Frenchb56eae42015-11-03 09:26:27 -06001242 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1243 if (iov[num].iov_base == NULL)
1244 return -ENOMEM;
1245 iov[num].iov_len = sizeof(struct create_durable_v2);
1246 if (!req->CreateContextsOffset)
1247 req->CreateContextsOffset =
1248 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1249 iov[1].iov_len);
1250 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1251 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1252 *num_iovec = num + 1;
1253 return 0;
1254}
1255
1256static int
1257add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1258 struct cifs_open_parms *oparms)
1259{
1260 struct smb2_create_req *req = iov[0].iov_base;
1261 unsigned int num = *num_iovec;
1262
1263 /* indicate that we don't need to relock the file */
1264 oparms->reconnect = false;
1265
1266 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1267 if (iov[num].iov_base == NULL)
1268 return -ENOMEM;
1269 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1270 if (!req->CreateContextsOffset)
1271 req->CreateContextsOffset =
1272 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1273 iov[1].iov_len);
1274 le32_add_cpu(&req->CreateContextsLength,
1275 sizeof(struct create_durable_handle_reconnect_v2));
1276 inc_rfc1001_len(&req->hdr,
1277 sizeof(struct create_durable_handle_reconnect_v2));
1278 *num_iovec = num + 1;
1279 return 0;
1280}
1281
1282static int
1283add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1284 struct cifs_open_parms *oparms, bool use_persistent)
1285{
1286 struct smb2_create_req *req = iov[0].iov_base;
1287 unsigned int num = *num_iovec;
1288
1289 if (use_persistent) {
1290 if (oparms->reconnect)
1291 return add_durable_reconnect_v2_context(iov, num_iovec,
1292 oparms);
1293 else
1294 return add_durable_v2_context(iov, num_iovec, oparms);
1295 }
1296
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001297 if (oparms->reconnect) {
1298 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1299 /* indicate that we don't need to relock the file */
1300 oparms->reconnect = false;
1301 } else
1302 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001303 if (iov[num].iov_base == NULL)
1304 return -ENOMEM;
1305 iov[num].iov_len = sizeof(struct create_durable);
1306 if (!req->CreateContextsOffset)
1307 req->CreateContextsOffset =
1308 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1309 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001310 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001311 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1312 *num_iovec = num + 1;
1313 return 0;
1314}
1315
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001316int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001317SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001318 __u8 *oplock, struct smb2_file_all_info *buf,
1319 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001320{
1321 struct smb2_create_req *req;
1322 struct smb2_create_rsp *rsp;
1323 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001324 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001325 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001326 struct kvec iov[4];
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001327 int resp_buftype;
1328 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001329 __le16 *copy_path = NULL;
1330 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001331 int rc = 0;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001332 unsigned int num_iovecs = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001333 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001334 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001335
Joe Perchesf96637b2013-05-04 22:12:25 -05001336 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001337
1338 if (ses && (ses->server))
1339 server = ses->server;
1340 else
1341 return -EIO;
1342
1343 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1344 if (rc)
1345 return rc;
1346
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001347 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001348 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001349 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1350 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001351
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001352 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001353 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001354 /* File attributes ignored on open (used in create though) */
1355 req->FileAttributes = cpu_to_le32(file_attributes);
1356 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001357 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1358 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001359 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001360 /* do not count rfc1001 len field */
1361 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001362
1363 iov[0].iov_base = (char *)req;
1364 /* 4 for rfc1002 length field */
1365 iov[0].iov_len = get_rfc1002_length(req) + 4;
1366
1367 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001368 req->NameLength = cpu_to_le16(uni_path_len - 2);
1369 /* -1 since last byte is buf[0] which is sent below (path) */
1370 iov[0].iov_len--;
1371 if (uni_path_len % 8 != 0) {
1372 copy_size = uni_path_len / 8 * 8;
1373 if (copy_size < uni_path_len)
1374 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001375
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001376 copy_path = kzalloc(copy_size, GFP_KERNEL);
1377 if (!copy_path)
1378 return -ENOMEM;
1379 memcpy((char *)copy_path, (const char *)path,
1380 uni_path_len);
1381 uni_path_len = copy_size;
1382 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001383 }
1384
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001385 iov[1].iov_len = uni_path_len;
1386 iov[1].iov_base = path;
1387 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1388 inc_rfc1001_len(req, uni_path_len - 1);
1389
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001390 if (!server->oplocks)
1391 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1392
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001393 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001394 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1395 req->RequestedOplockLevel = *oplock;
1396 else {
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001397 rc = add_lease_context(server, iov, &num_iovecs, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001398 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001399 cifs_small_buf_release(req);
1400 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001401 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001402 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001403 lc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001404 }
1405
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001406 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1407 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001408 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001409 struct create_context *ccontext =
1410 (struct create_context *)iov[num_iovecs-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001411 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001412 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001413 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001414
1415 rc = add_durable_context(iov, &num_iovecs, oparms,
1416 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001417 if (rc) {
1418 cifs_small_buf_release(req);
1419 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001420 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001421 return rc;
1422 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001423 dhc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001424 }
1425
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001426 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1427 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1428
1429 if (rc != 0) {
1430 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001431 if (err_buf)
1432 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1433 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001434 goto creat_exit;
1435 }
1436
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001437 oparms->fid->persistent_fid = rsp->PersistentFileId;
1438 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001439
1440 if (buf) {
1441 memcpy(buf, &rsp->CreationTime, 32);
1442 buf->AllocationSize = rsp->AllocationSize;
1443 buf->EndOfFile = rsp->EndofFile;
1444 buf->Attributes = rsp->FileAttributes;
1445 buf->NumberOfLinks = cpu_to_le32(1);
1446 buf->DeletePending = 0;
1447 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001448
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001449 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001450 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001451 else
1452 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001453creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001454 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001455 kfree(lc_buf);
1456 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001457 free_rsp_buf(resp_buftype, rsp);
1458 return rc;
1459}
1460
Steve French4a72daf2013-06-25 00:20:49 -05001461/*
1462 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1463 */
1464int
1465SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1466 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1467 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1468{
1469 struct smb2_ioctl_req *req;
1470 struct smb2_ioctl_rsp *rsp;
1471 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001472 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001473 struct kvec iov[2];
1474 int resp_buftype;
1475 int num_iovecs;
1476 int rc = 0;
1477
1478 cifs_dbg(FYI, "SMB2 IOCTL\n");
1479
Steve French3d1a3742014-08-11 21:05:25 -05001480 if (out_data != NULL)
1481 *out_data = NULL;
1482
Steve French4a72daf2013-06-25 00:20:49 -05001483 /* zero out returned data len, in case of error */
1484 if (plen)
1485 *plen = 0;
1486
Steve French8e353102015-03-26 19:47:02 -05001487 if (tcon)
1488 ses = tcon->ses;
1489 else
1490 return -EIO;
1491
Steve French4a72daf2013-06-25 00:20:49 -05001492 if (ses && (ses->server))
1493 server = ses->server;
1494 else
1495 return -EIO;
1496
1497 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1498 if (rc)
1499 return rc;
1500
1501 req->CtlCode = cpu_to_le32(opcode);
1502 req->PersistentFileId = persistent_fid;
1503 req->VolatileFileId = volatile_fid;
1504
1505 if (indatalen) {
1506 req->InputCount = cpu_to_le32(indatalen);
1507 /* do not set InputOffset if no input data */
1508 req->InputOffset =
1509 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1510 iov[1].iov_base = in_data;
1511 iov[1].iov_len = indatalen;
1512 num_iovecs = 2;
1513 } else
1514 num_iovecs = 1;
1515
1516 req->OutputOffset = 0;
1517 req->OutputCount = 0; /* MBZ */
1518
1519 /*
1520 * Could increase MaxOutputResponse, but that would require more
1521 * than one credit. Windows typically sets this smaller, but for some
1522 * ioctls it may be useful to allow server to send more. No point
1523 * limiting what the server can send as long as fits in one credit
1524 */
1525 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1526
1527 if (is_fsctl)
1528 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1529 else
1530 req->Flags = 0;
1531
1532 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001533
Steve French7ff8d452013-10-14 00:44:19 -05001534 /*
1535 * If no input data, the size of ioctl struct in
1536 * protocol spec still includes a 1 byte data buffer,
1537 * but if input data passed to ioctl, we do not
1538 * want to double count this, so we do not send
1539 * the dummy one byte of data in iovec[0] if sending
1540 * input data (in iovec[1]). We also must add 4 bytes
1541 * in first iovec to allow for rfc1002 length field.
1542 */
1543
1544 if (indatalen) {
1545 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1546 inc_rfc1001_len(req, indatalen - 1);
1547 } else
1548 iov[0].iov_len = get_rfc1002_length(req) + 4;
1549
Steve French4a72daf2013-06-25 00:20:49 -05001550
1551 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1552 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1553
Steve French9bf0c9c2013-11-16 18:05:28 -06001554 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001555 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001556 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001557 } else if (rc == -EINVAL) {
1558 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1559 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001560 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001561 goto ioctl_exit;
1562 }
Steve French4a72daf2013-06-25 00:20:49 -05001563 }
1564
1565 /* check if caller wants to look at return data or just return rc */
1566 if ((plen == NULL) || (out_data == NULL))
1567 goto ioctl_exit;
1568
1569 *plen = le32_to_cpu(rsp->OutputCount);
1570
1571 /* We check for obvious errors in the output buffer length and offset */
1572 if (*plen == 0)
1573 goto ioctl_exit; /* server returned no data */
1574 else if (*plen > 0xFF00) {
1575 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1576 *plen = 0;
1577 rc = -EIO;
1578 goto ioctl_exit;
1579 }
1580
1581 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1582 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1583 le32_to_cpu(rsp->OutputOffset));
1584 *plen = 0;
1585 rc = -EIO;
1586 goto ioctl_exit;
1587 }
1588
1589 *out_data = kmalloc(*plen, GFP_KERNEL);
1590 if (*out_data == NULL) {
1591 rc = -ENOMEM;
1592 goto ioctl_exit;
1593 }
1594
Steve French373512e2015-12-18 13:05:30 -06001595 memcpy(*out_data,
1596 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
Steve French4a72daf2013-06-25 00:20:49 -05001597 *plen);
1598ioctl_exit:
1599 free_rsp_buf(resp_buftype, rsp);
1600 return rc;
1601}
1602
Steve French64a5cfa2013-10-14 15:31:32 -05001603/*
1604 * Individual callers to ioctl worker function follow
1605 */
1606
1607int
1608SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1609 u64 persistent_fid, u64 volatile_fid)
1610{
1611 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001612 struct compress_ioctl fsctl_input;
1613 char *ret_data = NULL;
1614
1615 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001616 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001617
1618 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1619 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1620 (char *)&fsctl_input /* data input */,
1621 2 /* in data len */, &ret_data /* out data */, NULL);
1622
1623 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001624
1625 return rc;
1626}
1627
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001628int
1629SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1630 u64 persistent_fid, u64 volatile_fid)
1631{
1632 struct smb2_close_req *req;
1633 struct smb2_close_rsp *rsp;
1634 struct TCP_Server_Info *server;
1635 struct cifs_ses *ses = tcon->ses;
1636 struct kvec iov[1];
1637 int resp_buftype;
1638 int rc = 0;
1639
Joe Perchesf96637b2013-05-04 22:12:25 -05001640 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001641
1642 if (ses && (ses->server))
1643 server = ses->server;
1644 else
1645 return -EIO;
1646
1647 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1648 if (rc)
1649 return rc;
1650
1651 req->PersistentFileId = persistent_fid;
1652 req->VolatileFileId = volatile_fid;
1653
1654 iov[0].iov_base = (char *)req;
1655 /* 4 for rfc1002 length field */
1656 iov[0].iov_len = get_rfc1002_length(req) + 4;
1657
1658 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1659 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1660
1661 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001662 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001663 goto close_exit;
1664 }
1665
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001666 /* BB FIXME - decode close response, update inode for caching */
1667
1668close_exit:
1669 free_rsp_buf(resp_buftype, rsp);
1670 return rc;
1671}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001672
1673static int
1674validate_buf(unsigned int offset, unsigned int buffer_length,
1675 struct smb2_hdr *hdr, unsigned int min_buf_size)
1676
1677{
1678 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1679 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1680 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1681 char *end_of_buf = begin_of_buf + buffer_length;
1682
1683
1684 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001685 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1686 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001687 return -EINVAL;
1688 }
1689
1690 /* check if beyond RFC1001 maximum length */
1691 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001692 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1693 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001694 return -EINVAL;
1695 }
1696
1697 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001698 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001699 return -EINVAL;
1700 }
1701
1702 return 0;
1703}
1704
1705/*
1706 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1707 * Caller must free buffer.
1708 */
1709static int
1710validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1711 struct smb2_hdr *hdr, unsigned int minbufsize,
1712 char *data)
1713
1714{
1715 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1716 int rc;
1717
1718 if (!data)
1719 return -EINVAL;
1720
1721 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1722 if (rc)
1723 return rc;
1724
1725 memcpy(data, begin_of_buf, buffer_length);
1726
1727 return 0;
1728}
1729
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001730static int
1731query_info(const unsigned int xid, struct cifs_tcon *tcon,
1732 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1733 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001734{
1735 struct smb2_query_info_req *req;
1736 struct smb2_query_info_rsp *rsp = NULL;
1737 struct kvec iov[2];
1738 int rc = 0;
1739 int resp_buftype;
1740 struct TCP_Server_Info *server;
1741 struct cifs_ses *ses = tcon->ses;
1742
Joe Perchesf96637b2013-05-04 22:12:25 -05001743 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001744
1745 if (ses && (ses->server))
1746 server = ses->server;
1747 else
1748 return -EIO;
1749
1750 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1751 if (rc)
1752 return rc;
1753
1754 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001755 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001756 req->PersistentFileId = persistent_fid;
1757 req->VolatileFileId = volatile_fid;
1758 /* 4 for rfc1002 length field and 1 for Buffer */
1759 req->InputBufferOffset =
1760 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001761 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001762
1763 iov[0].iov_base = (char *)req;
1764 /* 4 for rfc1002 length field */
1765 iov[0].iov_len = get_rfc1002_length(req) + 4;
1766
1767 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001768 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1769
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001770 if (rc) {
1771 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1772 goto qinf_exit;
1773 }
1774
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001775 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1776 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001777 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001778
1779qinf_exit:
1780 free_rsp_buf(resp_buftype, rsp);
1781 return rc;
1782}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001783
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001784int
1785SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1786 u64 persistent_fid, u64 volatile_fid,
1787 struct smb2_file_all_info *data)
1788{
1789 return query_info(xid, tcon, persistent_fid, volatile_fid,
1790 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001791 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001792 sizeof(struct smb2_file_all_info), data);
1793}
1794
1795int
1796SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1797 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1798{
1799 return query_info(xid, tcon, persistent_fid, volatile_fid,
1800 FILE_INTERNAL_INFORMATION,
1801 sizeof(struct smb2_file_internal_info),
1802 sizeof(struct smb2_file_internal_info), uniqueid);
1803}
1804
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001805/*
1806 * This is a no-op for now. We're not really interested in the reply, but
1807 * rather in the fact that the server sent one and that server->lstrp
1808 * gets updated.
1809 *
1810 * FIXME: maybe we should consider checking that the reply matches request?
1811 */
1812static void
1813smb2_echo_callback(struct mid_q_entry *mid)
1814{
1815 struct TCP_Server_Info *server = mid->callback_data;
1816 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1817 unsigned int credits_received = 1;
1818
1819 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1820 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1821
Christopher Oo5fb4e282015-06-25 16:10:48 -07001822 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001823 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001824 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001825 add_credits(server, credits_received, CIFS_ECHO_OP);
1826}
1827
1828int
1829SMB2_echo(struct TCP_Server_Info *server)
1830{
1831 struct smb2_echo_req *req;
1832 int rc = 0;
1833 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07001834 struct smb_rqst rqst = { .rq_iov = &iov,
1835 .rq_nvec = 1 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001836
Joe Perchesf96637b2013-05-04 22:12:25 -05001837 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001838
Steve French4fcd1812016-06-22 20:12:05 -05001839 if (server->tcpStatus == CifsNeedNegotiate) {
1840 struct list_head *tmp, *tmp2;
1841 struct cifs_ses *ses;
1842 struct cifs_tcon *tcon;
1843
1844 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
1845 spin_lock(&cifs_tcp_ses_lock);
1846 list_for_each(tmp, &server->smb_ses_list) {
1847 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
1848 list_for_each(tmp2, &ses->tcon_list) {
1849 tcon = list_entry(tmp2, struct cifs_tcon,
1850 tcon_list);
1851 /* add check for persistent handle reconnect */
1852 if (tcon && tcon->need_reconnect) {
1853 spin_unlock(&cifs_tcp_ses_lock);
1854 rc = smb2_reconnect(SMB2_ECHO, tcon);
1855 spin_lock(&cifs_tcp_ses_lock);
1856 }
1857 }
1858 }
1859 spin_unlock(&cifs_tcp_ses_lock);
1860 }
1861
1862 /* if no session, renegotiate failed above */
1863 if (server->tcpStatus == CifsNeedNegotiate)
1864 return -EIO;
1865
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001866 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
1867 if (rc)
1868 return rc;
1869
1870 req->hdr.CreditRequest = cpu_to_le16(1);
1871
1872 iov.iov_base = (char *)req;
1873 /* 4 for rfc1002 length field */
1874 iov.iov_len = get_rfc1002_length(req) + 4;
1875
Jeff Laytonfec344e2012-09-18 16:20:35 -07001876 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001877 CIFS_ECHO_OP);
1878 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001879 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001880
1881 cifs_small_buf_release(req);
1882 return rc;
1883}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001884
1885int
1886SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1887 u64 volatile_fid)
1888{
1889 struct smb2_flush_req *req;
1890 struct TCP_Server_Info *server;
1891 struct cifs_ses *ses = tcon->ses;
1892 struct kvec iov[1];
1893 int resp_buftype;
1894 int rc = 0;
1895
Joe Perchesf96637b2013-05-04 22:12:25 -05001896 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001897
1898 if (ses && (ses->server))
1899 server = ses->server;
1900 else
1901 return -EIO;
1902
1903 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
1904 if (rc)
1905 return rc;
1906
1907 req->PersistentFileId = persistent_fid;
1908 req->VolatileFileId = volatile_fid;
1909
1910 iov[0].iov_base = (char *)req;
1911 /* 4 for rfc1002 length field */
1912 iov[0].iov_len = get_rfc1002_length(req) + 4;
1913
1914 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1915
Steve Frenchdfebe402015-03-27 01:00:06 -05001916 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001917 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
1918
1919 free_rsp_buf(resp_buftype, iov[0].iov_base);
1920 return rc;
1921}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001922
1923/*
1924 * To form a chain of read requests, any read requests after the first should
1925 * have the end_of_chain boolean set to true.
1926 */
1927static int
1928smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
1929 unsigned int remaining_bytes, int request_type)
1930{
1931 int rc = -EACCES;
1932 struct smb2_read_req *req = NULL;
1933
1934 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
1935 if (rc)
1936 return rc;
1937 if (io_parms->tcon->ses->server == NULL)
1938 return -ECONNABORTED;
1939
1940 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1941
1942 req->PersistentFileId = io_parms->persistent_fid;
1943 req->VolatileFileId = io_parms->volatile_fid;
1944 req->ReadChannelInfoOffset = 0; /* reserved */
1945 req->ReadChannelInfoLength = 0; /* reserved */
1946 req->Channel = 0; /* reserved */
1947 req->MinimumCount = 0;
1948 req->Length = cpu_to_le32(io_parms->length);
1949 req->Offset = cpu_to_le64(io_parms->offset);
1950
1951 if (request_type & CHAINED_REQUEST) {
1952 if (!(request_type & END_OF_CHAIN)) {
1953 /* 4 for rfc1002 length field */
1954 req->hdr.NextCommand =
1955 cpu_to_le32(get_rfc1002_length(req) + 4);
1956 } else /* END_OF_CHAIN */
1957 req->hdr.NextCommand = 0;
1958 if (request_type & RELATED_REQUEST) {
1959 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1960 /*
1961 * Related requests use info from previous read request
1962 * in chain.
1963 */
1964 req->hdr.SessionId = 0xFFFFFFFF;
1965 req->hdr.TreeId = 0xFFFFFFFF;
1966 req->PersistentFileId = 0xFFFFFFFF;
1967 req->VolatileFileId = 0xFFFFFFFF;
1968 }
1969 }
1970 if (remaining_bytes > io_parms->length)
1971 req->RemainingBytes = cpu_to_le32(remaining_bytes);
1972 else
1973 req->RemainingBytes = 0;
1974
1975 iov[0].iov_base = (char *)req;
1976 /* 4 for rfc1002 length field */
1977 iov[0].iov_len = get_rfc1002_length(req) + 4;
1978 return rc;
1979}
1980
1981static void
1982smb2_readv_callback(struct mid_q_entry *mid)
1983{
1984 struct cifs_readdata *rdata = mid->callback_data;
1985 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1986 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07001987 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001988 unsigned int credits_received = 1;
Jeff Layton58195752012-09-19 06:22:34 -07001989 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Layton8321fec2012-09-19 06:22:32 -07001990 .rq_nvec = 1,
1991 .rq_pages = rdata->pages,
1992 .rq_npages = rdata->nr_pages,
1993 .rq_pagesz = rdata->pagesz,
1994 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001995
Joe Perchesf96637b2013-05-04 22:12:25 -05001996 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1997 __func__, mid->mid, mid->mid_state, rdata->result,
1998 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001999
2000 switch (mid->mid_state) {
2001 case MID_RESPONSE_RECEIVED:
2002 credits_received = le16_to_cpu(buf->CreditRequest);
2003 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04002004 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002005 int rc;
2006
Jeff Layton0b688cf2012-09-18 16:20:34 -07002007 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002008 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002009 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2010 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002011 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002012 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002013 task_io_account_read(rdata->got_bytes);
2014 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002015 break;
2016 case MID_REQUEST_SUBMITTED:
2017 case MID_RETRY_NEEDED:
2018 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002019 if (server->sign && rdata->got_bytes)
2020 /* reset bytes number since we can not check a sign */
2021 rdata->got_bytes = 0;
2022 /* FIXME: should this be counted toward the initiating task? */
2023 task_io_account_read(rdata->got_bytes);
2024 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002025 break;
2026 default:
2027 if (rdata->result != -ENODATA)
2028 rdata->result = -EIO;
2029 }
2030
2031 if (rdata->result)
2032 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2033
2034 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002035 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002036 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002037 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002038 add_credits(server, credits_received, 0);
2039}
2040
2041/* smb2_async_readv - send an async write, and set up mid to handle result */
2042int
2043smb2_async_readv(struct cifs_readdata *rdata)
2044{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002045 int rc, flags = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002046 struct smb2_hdr *buf;
2047 struct cifs_io_parms io_parms;
Jeff Layton58195752012-09-19 06:22:34 -07002048 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07002049 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002050 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002051
Joe Perchesf96637b2013-05-04 22:12:25 -05002052 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2053 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002054
2055 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2056 io_parms.offset = rdata->offset;
2057 io_parms.length = rdata->bytes;
2058 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2059 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2060 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002061
2062 server = io_parms.tcon->ses->server;
2063
Jeff Layton58195752012-09-19 06:22:34 -07002064 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002065 if (rc) {
2066 if (rc == -EAGAIN && rdata->credits) {
2067 /* credits was reset by reconnect */
2068 rdata->credits = 0;
2069 /* reduce in_flight value since we won't send the req */
2070 spin_lock(&server->req_lock);
2071 server->in_flight--;
2072 spin_unlock(&server->req_lock);
2073 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002074 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002075 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002076
Jeff Layton58195752012-09-19 06:22:34 -07002077 buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002078 /* 4 for rfc1002 length field */
Jeff Layton58195752012-09-19 06:22:34 -07002079 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002080
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002081 if (rdata->credits) {
2082 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2083 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002084 buf->CreditRequest = buf->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002085 spin_lock(&server->req_lock);
2086 server->credits += rdata->credits -
2087 le16_to_cpu(buf->CreditCharge);
2088 spin_unlock(&server->req_lock);
2089 wake_up(&server->request_q);
2090 flags = CIFS_HAS_CREDITS;
2091 }
2092
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002093 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002094 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002095 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002096 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002097 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002098 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002099 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2100 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002101
2102 cifs_small_buf_release(buf);
2103 return rc;
2104}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002105
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002106int
2107SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2108 unsigned int *nbytes, char **buf, int *buf_type)
2109{
2110 int resp_buftype, rc = -EACCES;
2111 struct smb2_read_rsp *rsp = NULL;
2112 struct kvec iov[1];
2113
2114 *nbytes = 0;
2115 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2116 if (rc)
2117 return rc;
2118
2119 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2120 &resp_buftype, CIFS_LOG_ERROR);
2121
2122 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2123
2124 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2125 free_rsp_buf(resp_buftype, iov[0].iov_base);
2126 return 0;
2127 }
2128
2129 if (rc) {
2130 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002131 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002132 } else {
2133 *nbytes = le32_to_cpu(rsp->DataLength);
2134 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2135 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002136 cifs_dbg(FYI, "bad length %d for count %d\n",
2137 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002138 rc = -EIO;
2139 *nbytes = 0;
2140 }
2141 }
2142
2143 if (*buf) {
Steve French373512e2015-12-18 13:05:30 -06002144 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002145 *nbytes);
2146 free_rsp_buf(resp_buftype, iov[0].iov_base);
2147 } else if (resp_buftype != CIFS_NO_BUFFER) {
2148 *buf = iov[0].iov_base;
2149 if (resp_buftype == CIFS_SMALL_BUFFER)
2150 *buf_type = CIFS_SMALL_BUFFER;
2151 else if (resp_buftype == CIFS_LARGE_BUFFER)
2152 *buf_type = CIFS_LARGE_BUFFER;
2153 }
2154 return rc;
2155}
2156
Pavel Shilovsky33319142012-09-18 16:20:29 -07002157/*
2158 * Check the mid_state and signature on received buffer (if any), and queue the
2159 * workqueue completion task.
2160 */
2161static void
2162smb2_writev_callback(struct mid_q_entry *mid)
2163{
2164 struct cifs_writedata *wdata = mid->callback_data;
2165 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002166 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002167 unsigned int written;
2168 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2169 unsigned int credits_received = 1;
2170
2171 switch (mid->mid_state) {
2172 case MID_RESPONSE_RECEIVED:
2173 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2174 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2175 if (wdata->result != 0)
2176 break;
2177
2178 written = le32_to_cpu(rsp->DataLength);
2179 /*
2180 * Mask off high 16 bits when bytes written as returned
2181 * by the server is greater than bytes requested by the
2182 * client. OS/2 servers are known to set incorrect
2183 * CountHigh values.
2184 */
2185 if (written > wdata->bytes)
2186 written &= 0xFFFF;
2187
2188 if (written < wdata->bytes)
2189 wdata->result = -ENOSPC;
2190 else
2191 wdata->bytes = written;
2192 break;
2193 case MID_REQUEST_SUBMITTED:
2194 case MID_RETRY_NEEDED:
2195 wdata->result = -EAGAIN;
2196 break;
2197 default:
2198 wdata->result = -EIO;
2199 break;
2200 }
2201
2202 if (wdata->result)
2203 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2204
2205 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002206 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002207 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002208 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002209 add_credits(tcon->ses->server, credits_received, 0);
2210}
2211
2212/* smb2_async_writev - send an async write, and set up mid to handle result */
2213int
Steve French4a5c80d2014-02-07 20:45:12 -06002214smb2_async_writev(struct cifs_writedata *wdata,
2215 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002216{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002217 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002218 struct smb2_write_req *req = NULL;
2219 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002220 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002221 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002222 struct smb_rqst rqst;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002223
2224 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002225 if (rc) {
2226 if (rc == -EAGAIN && wdata->credits) {
2227 /* credits was reset by reconnect */
2228 wdata->credits = 0;
2229 /* reduce in_flight value since we won't send the req */
2230 spin_lock(&server->req_lock);
2231 server->in_flight--;
2232 spin_unlock(&server->req_lock);
2233 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002234 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002235 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002236
Pavel Shilovsky33319142012-09-18 16:20:29 -07002237 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2238
2239 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2240 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2241 req->WriteChannelInfoOffset = 0;
2242 req->WriteChannelInfoLength = 0;
2243 req->Channel = 0;
2244 req->Offset = cpu_to_le64(wdata->offset);
2245 /* 4 for rfc1002 length field */
2246 req->DataOffset = cpu_to_le16(
2247 offsetof(struct smb2_write_req, Buffer) - 4);
2248 req->RemainingBytes = 0;
2249
2250 /* 4 for rfc1002 length field and 1 for Buffer */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002251 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2252 iov.iov_base = req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002253
Jeff Laytoneddb0792012-09-18 16:20:35 -07002254 rqst.rq_iov = &iov;
2255 rqst.rq_nvec = 1;
2256 rqst.rq_pages = wdata->pages;
2257 rqst.rq_npages = wdata->nr_pages;
2258 rqst.rq_pagesz = wdata->pagesz;
2259 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002260
Joe Perchesf96637b2013-05-04 22:12:25 -05002261 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2262 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002263
2264 req->Length = cpu_to_le32(wdata->bytes);
2265
2266 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2267
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002268 if (wdata->credits) {
2269 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2270 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002271 req->hdr.CreditRequest = req->hdr.CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002272 spin_lock(&server->req_lock);
2273 server->credits += wdata->credits -
2274 le16_to_cpu(req->hdr.CreditCharge);
2275 spin_unlock(&server->req_lock);
2276 wake_up(&server->request_q);
2277 flags = CIFS_HAS_CREDITS;
2278 }
2279
Pavel Shilovsky33319142012-09-18 16:20:29 -07002280 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002281 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2282 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002283
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002284 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002285 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002286 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2287 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002288
Pavel Shilovsky33319142012-09-18 16:20:29 -07002289async_writev_out:
2290 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002291 return rc;
2292}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002293
2294/*
2295 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2296 * The length field from io_parms must be at least 1 and indicates a number of
2297 * elements with data to write that begins with position 1 in iov array. All
2298 * data length is specified by count.
2299 */
2300int
2301SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2302 unsigned int *nbytes, struct kvec *iov, int n_vec)
2303{
2304 int rc = 0;
2305 struct smb2_write_req *req = NULL;
2306 struct smb2_write_rsp *rsp = NULL;
2307 int resp_buftype;
2308 *nbytes = 0;
2309
2310 if (n_vec < 1)
2311 return rc;
2312
2313 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2314 if (rc)
2315 return rc;
2316
2317 if (io_parms->tcon->ses->server == NULL)
2318 return -ECONNABORTED;
2319
2320 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2321
2322 req->PersistentFileId = io_parms->persistent_fid;
2323 req->VolatileFileId = io_parms->volatile_fid;
2324 req->WriteChannelInfoOffset = 0;
2325 req->WriteChannelInfoLength = 0;
2326 req->Channel = 0;
2327 req->Length = cpu_to_le32(io_parms->length);
2328 req->Offset = cpu_to_le64(io_parms->offset);
2329 /* 4 for rfc1002 length field */
2330 req->DataOffset = cpu_to_le16(
2331 offsetof(struct smb2_write_req, Buffer) - 4);
2332 req->RemainingBytes = 0;
2333
2334 iov[0].iov_base = (char *)req;
2335 /* 4 for rfc1002 length field and 1 for Buffer */
2336 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2337
2338 /* length of entire message including data to be written */
2339 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2340
2341 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2342 &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002343 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002344
2345 if (rc) {
2346 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002347 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002348 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002349 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002350
2351 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002352 return rc;
2353}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002354
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002355static unsigned int
2356num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2357{
2358 int len;
2359 unsigned int entrycount = 0;
2360 unsigned int next_offset = 0;
2361 FILE_DIRECTORY_INFO *entryptr;
2362
2363 if (bufstart == NULL)
2364 return 0;
2365
2366 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2367
2368 while (1) {
2369 entryptr = (FILE_DIRECTORY_INFO *)
2370 ((char *)entryptr + next_offset);
2371
2372 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002373 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002374 break;
2375 }
2376
2377 len = le32_to_cpu(entryptr->FileNameLength);
2378 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002379 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2380 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002381 break;
2382 }
2383
2384 *lastentry = (char *)entryptr;
2385 entrycount++;
2386
2387 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2388 if (!next_offset)
2389 break;
2390 }
2391
2392 return entrycount;
2393}
2394
2395/*
2396 * Readdir/FindFirst
2397 */
2398int
2399SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2400 u64 persistent_fid, u64 volatile_fid, int index,
2401 struct cifs_search_info *srch_inf)
2402{
2403 struct smb2_query_directory_req *req;
2404 struct smb2_query_directory_rsp *rsp = NULL;
2405 struct kvec iov[2];
2406 int rc = 0;
2407 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002408 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002409 unsigned char *bufptr;
2410 struct TCP_Server_Info *server;
2411 struct cifs_ses *ses = tcon->ses;
2412 __le16 asteriks = cpu_to_le16('*');
2413 char *end_of_smb;
2414 unsigned int output_size = CIFSMaxBufSize;
2415 size_t info_buf_size;
2416
2417 if (ses && (ses->server))
2418 server = ses->server;
2419 else
2420 return -EIO;
2421
2422 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2423 if (rc)
2424 return rc;
2425
2426 switch (srch_inf->info_level) {
2427 case SMB_FIND_FILE_DIRECTORY_INFO:
2428 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2429 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2430 break;
2431 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2432 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2433 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2434 break;
2435 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002436 cifs_dbg(VFS, "info level %u isn't supported\n",
2437 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002438 rc = -EINVAL;
2439 goto qdir_exit;
2440 }
2441
2442 req->FileIndex = cpu_to_le32(index);
2443 req->PersistentFileId = persistent_fid;
2444 req->VolatileFileId = volatile_fid;
2445
2446 len = 0x2;
2447 bufptr = req->Buffer;
2448 memcpy(bufptr, &asteriks, len);
2449
2450 req->FileNameOffset =
2451 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2452 req->FileNameLength = cpu_to_le16(len);
2453 /*
2454 * BB could be 30 bytes or so longer if we used SMB2 specific
2455 * buffer lengths, but this is safe and close enough.
2456 */
2457 output_size = min_t(unsigned int, output_size, server->maxBuf);
2458 output_size = min_t(unsigned int, output_size, 2 << 15);
2459 req->OutputBufferLength = cpu_to_le32(output_size);
2460
2461 iov[0].iov_base = (char *)req;
2462 /* 4 for RFC1001 length and 1 for Buffer */
2463 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2464
2465 iov[1].iov_base = (char *)(req->Buffer);
2466 iov[1].iov_len = len;
2467
2468 inc_rfc1001_len(req, len - 1 /* Buffer */);
2469
2470 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002471 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2472
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002473 if (rc) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002474 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2475 srch_inf->endOfSearch = true;
2476 rc = 0;
2477 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002478 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2479 goto qdir_exit;
2480 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002481
2482 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2483 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2484 info_buf_size);
2485 if (rc)
2486 goto qdir_exit;
2487
2488 srch_inf->unicode = true;
2489
2490 if (srch_inf->ntwrk_buf_start) {
2491 if (srch_inf->smallBuf)
2492 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2493 else
2494 cifs_buf_release(srch_inf->ntwrk_buf_start);
2495 }
2496 srch_inf->ntwrk_buf_start = (char *)rsp;
2497 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2498 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2499 /* 4 for rfc1002 length field */
2500 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2501 srch_inf->entries_in_buffer =
2502 num_entries(srch_inf->srch_entries_start, end_of_smb,
2503 &srch_inf->last_entry, info_buf_size);
2504 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002505 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2506 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2507 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002508 if (resp_buftype == CIFS_LARGE_BUFFER)
2509 srch_inf->smallBuf = false;
2510 else if (resp_buftype == CIFS_SMALL_BUFFER)
2511 srch_inf->smallBuf = true;
2512 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002513 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002514
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002515 return rc;
2516
2517qdir_exit:
2518 free_rsp_buf(resp_buftype, rsp);
2519 return rc;
2520}
2521
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002522static int
2523send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002524 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002525 unsigned int num, void **data, unsigned int *size)
2526{
2527 struct smb2_set_info_req *req;
2528 struct smb2_set_info_rsp *rsp = NULL;
2529 struct kvec *iov;
2530 int rc = 0;
2531 int resp_buftype;
2532 unsigned int i;
2533 struct TCP_Server_Info *server;
2534 struct cifs_ses *ses = tcon->ses;
2535
2536 if (ses && (ses->server))
2537 server = ses->server;
2538 else
2539 return -EIO;
2540
2541 if (!num)
2542 return -EINVAL;
2543
2544 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2545 if (!iov)
2546 return -ENOMEM;
2547
2548 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2549 if (rc) {
2550 kfree(iov);
2551 return rc;
2552 }
2553
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002554 req->hdr.ProcessId = cpu_to_le32(pid);
2555
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002556 req->InfoType = SMB2_O_INFO_FILE;
2557 req->FileInfoClass = info_class;
2558 req->PersistentFileId = persistent_fid;
2559 req->VolatileFileId = volatile_fid;
2560
2561 /* 4 for RFC1001 length and 1 for Buffer */
2562 req->BufferOffset =
2563 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2564 req->BufferLength = cpu_to_le32(*size);
2565
2566 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2567
2568 memcpy(req->Buffer, *data, *size);
2569
2570 iov[0].iov_base = (char *)req;
2571 /* 4 for RFC1001 length */
2572 iov[0].iov_len = get_rfc1002_length(req) + 4;
2573
2574 for (i = 1; i < num; i++) {
2575 inc_rfc1001_len(req, size[i]);
2576 le32_add_cpu(&req->BufferLength, size[i]);
2577 iov[i].iov_base = (char *)data[i];
2578 iov[i].iov_len = size[i];
2579 }
2580
2581 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2582 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2583
Steve French7d3fb242013-11-18 09:56:28 -06002584 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002585 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002586
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002587 free_rsp_buf(resp_buftype, rsp);
2588 kfree(iov);
2589 return rc;
2590}
2591
2592int
2593SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2594 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2595{
2596 struct smb2_file_rename_info info;
2597 void **data;
2598 unsigned int size[2];
2599 int rc;
2600 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2601
2602 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2603 if (!data)
2604 return -ENOMEM;
2605
2606 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2607 /* 0 = fail if target already exists */
2608 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2609 info.FileNameLength = cpu_to_le32(len);
2610
2611 data[0] = &info;
2612 size[0] = sizeof(struct smb2_file_rename_info);
2613
2614 data[1] = target_file;
2615 size[1] = len + 2 /* null */;
2616
2617 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002618 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2619 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002620 kfree(data);
2621 return rc;
2622}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002623
2624int
Steve French897fba12016-05-12 21:20:36 -05002625SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2626 u64 persistent_fid, u64 volatile_fid)
2627{
2628 __u8 delete_pending = 1;
2629 void *data;
2630 unsigned int size;
2631
2632 data = &delete_pending;
2633 size = 1; /* sizeof __u8 */
2634
2635 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2636 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2637 &size);
2638}
2639
2640int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002641SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2642 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2643{
2644 struct smb2_file_link_info info;
2645 void **data;
2646 unsigned int size[2];
2647 int rc;
2648 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2649
2650 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2651 if (!data)
2652 return -ENOMEM;
2653
2654 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2655 /* 0 = fail if link already exists */
2656 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2657 info.FileNameLength = cpu_to_le32(len);
2658
2659 data[0] = &info;
2660 size[0] = sizeof(struct smb2_file_link_info);
2661
2662 data[1] = target_file;
2663 size[1] = len + 2 /* null */;
2664
2665 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002666 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002667 kfree(data);
2668 return rc;
2669}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002670
2671int
2672SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002673 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002674{
2675 struct smb2_file_eof_info info;
2676 void *data;
2677 unsigned int size;
2678
2679 info.EndOfFile = *eof;
2680
2681 data = &info;
2682 size = sizeof(struct smb2_file_eof_info);
2683
Steve Frenchf29ebb42014-07-19 21:44:58 -05002684 if (is_falloc)
2685 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2686 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2687 else
2688 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2689 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002690}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002691
2692int
2693SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2694 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2695{
2696 unsigned int size;
2697 size = sizeof(FILE_BASIC_INFO);
2698 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2699 current->tgid, FILE_BASIC_INFORMATION, 1,
2700 (void **)&buf, &size);
2701}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002702
2703int
2704SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2705 const u64 persistent_fid, const u64 volatile_fid,
2706 __u8 oplock_level)
2707{
2708 int rc;
2709 struct smb2_oplock_break *req = NULL;
2710
Joe Perchesf96637b2013-05-04 22:12:25 -05002711 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002712 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2713
2714 if (rc)
2715 return rc;
2716
2717 req->VolatileFid = volatile_fid;
2718 req->PersistentFid = persistent_fid;
2719 req->OplockLevel = oplock_level;
2720 req->hdr.CreditRequest = cpu_to_le16(1);
2721
2722 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2723 /* SMB2 buffer freed by function above */
2724
2725 if (rc) {
2726 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002727 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002728 }
2729
2730 return rc;
2731}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002732
2733static void
2734copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2735 struct kstatfs *kst)
2736{
2737 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2738 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2739 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2740 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2741 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2742 return;
2743}
2744
2745static int
2746build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2747 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2748{
2749 int rc;
2750 struct smb2_query_info_req *req;
2751
Joe Perchesf96637b2013-05-04 22:12:25 -05002752 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002753
2754 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2755 return -EIO;
2756
2757 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2758 if (rc)
2759 return rc;
2760
2761 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2762 req->FileInfoClass = level;
2763 req->PersistentFileId = persistent_fid;
2764 req->VolatileFileId = volatile_fid;
2765 /* 4 for rfc1002 length field and 1 for pad */
2766 req->InputBufferOffset =
2767 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2768 req->OutputBufferLength = cpu_to_le32(
2769 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2770
2771 iov->iov_base = (char *)req;
2772 /* 4 for rfc1002 length field */
2773 iov->iov_len = get_rfc1002_length(req) + 4;
2774 return 0;
2775}
2776
2777int
2778SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2779 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2780{
2781 struct smb2_query_info_rsp *rsp = NULL;
2782 struct kvec iov;
2783 int rc = 0;
2784 int resp_buftype;
2785 struct cifs_ses *ses = tcon->ses;
2786 struct smb2_fs_full_size_info *info = NULL;
2787
2788 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2789 sizeof(struct smb2_fs_full_size_info),
2790 persistent_fid, volatile_fid);
2791 if (rc)
2792 return rc;
2793
2794 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2795 if (rc) {
2796 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05002797 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002798 }
2799 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2800
2801 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2802 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2803 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2804 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2805 sizeof(struct smb2_fs_full_size_info));
2806 if (!rc)
2807 copy_fs_info_to_kstatfs(info, fsdata);
2808
Steve French34f62642013-10-09 02:07:00 -05002809qfsinf_exit:
2810 free_rsp_buf(resp_buftype, iov.iov_base);
2811 return rc;
2812}
2813
2814int
2815SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05002816 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05002817{
2818 struct smb2_query_info_rsp *rsp = NULL;
2819 struct kvec iov;
2820 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05002821 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05002822 struct cifs_ses *ses = tcon->ses;
2823 unsigned int rsp_len, offset;
2824
Steven French21671142013-10-09 13:36:35 -05002825 if (level == FS_DEVICE_INFORMATION) {
2826 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2827 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2828 } else if (level == FS_ATTRIBUTE_INFORMATION) {
2829 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
2830 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002831 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
2832 max_len = sizeof(struct smb3_fs_ss_info);
2833 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05002834 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002835 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05002836 return -EINVAL;
2837 }
2838
2839 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05002840 persistent_fid, volatile_fid);
2841 if (rc)
2842 return rc;
2843
2844 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2845 if (rc) {
2846 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2847 goto qfsattr_exit;
2848 }
2849 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2850
2851 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
2852 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05002853 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
2854 if (rc)
2855 goto qfsattr_exit;
2856
2857 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05002858 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
2859 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05002860 rsp_len, max_len));
2861 else if (level == FS_DEVICE_INFORMATION)
2862 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
2863 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002864 else if (level == FS_SECTOR_SIZE_INFORMATION) {
2865 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
2866 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
2867 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
2868 tcon->perf_sector_size =
2869 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
2870 }
Steve French34f62642013-10-09 02:07:00 -05002871
2872qfsattr_exit:
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002873 free_rsp_buf(resp_buftype, iov.iov_base);
2874 return rc;
2875}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002876
2877int
2878smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2879 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2880 const __u32 num_lock, struct smb2_lock_element *buf)
2881{
2882 int rc = 0;
2883 struct smb2_lock_req *req = NULL;
2884 struct kvec iov[2];
2885 int resp_buf_type;
2886 unsigned int count;
2887
Joe Perchesf96637b2013-05-04 22:12:25 -05002888 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002889
2890 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
2891 if (rc)
2892 return rc;
2893
2894 req->hdr.ProcessId = cpu_to_le32(pid);
2895 req->LockCount = cpu_to_le16(num_lock);
2896
2897 req->PersistentFileId = persist_fid;
2898 req->VolatileFileId = volatile_fid;
2899
2900 count = num_lock * sizeof(struct smb2_lock_element);
2901 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
2902
2903 iov[0].iov_base = (char *)req;
2904 /* 4 for rfc1002 length field and count for all locks */
2905 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
2906 iov[1].iov_base = (char *)buf;
2907 iov[1].iov_len = count;
2908
2909 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2910 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2911 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002912 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002913 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
2914 }
2915
2916 return rc;
2917}
2918
2919int
2920SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
2921 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2922 const __u64 length, const __u64 offset, const __u32 lock_flags,
2923 const bool wait)
2924{
2925 struct smb2_lock_element lock;
2926
2927 lock.Offset = cpu_to_le64(offset);
2928 lock.Length = cpu_to_le64(length);
2929 lock.Flags = cpu_to_le32(lock_flags);
2930 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
2931 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
2932
2933 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
2934}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002935
2936int
2937SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
2938 __u8 *lease_key, const __le32 lease_state)
2939{
2940 int rc;
2941 struct smb2_lease_ack *req = NULL;
2942
Joe Perchesf96637b2013-05-04 22:12:25 -05002943 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002944 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2945
2946 if (rc)
2947 return rc;
2948
2949 req->hdr.CreditRequest = cpu_to_le16(1);
2950 req->StructureSize = cpu_to_le16(36);
2951 inc_rfc1001_len(req, 12);
2952
2953 memcpy(req->LeaseKey, lease_key, 16);
2954 req->LeaseState = lease_state;
2955
2956 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2957 /* SMB2 buffer freed by function above */
2958
2959 if (rc) {
2960 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002961 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002962 }
2963
2964 return rc;
2965}