blob: 4ba3f68a1766c84518af2e428d4f02ed912b4231 [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070036#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040037#include <linux/xattr.h>
38#include "smb2pdu.h"
39#include "cifsglob.h"
40#include "cifsacl.h"
41#include "cifsproto.h"
42#include "smb2proto.h"
43#include "cifs_unicode.h"
44#include "cifs_debug.h"
45#include "ntlmssp.h"
46#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070047#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070048#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050049#include "cifs_spnego.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040050
51/*
52 * The following table defines the expected "StructureSize" of SMB2 requests
53 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
54 *
55 * Note that commands are defined in smb2pdu.h in le16 but the array below is
56 * indexed by command in host byte order.
57 */
58static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
59 /* SMB2_NEGOTIATE */ 36,
60 /* SMB2_SESSION_SETUP */ 25,
61 /* SMB2_LOGOFF */ 4,
62 /* SMB2_TREE_CONNECT */ 9,
63 /* SMB2_TREE_DISCONNECT */ 4,
64 /* SMB2_CREATE */ 57,
65 /* SMB2_CLOSE */ 24,
66 /* SMB2_FLUSH */ 24,
67 /* SMB2_READ */ 49,
68 /* SMB2_WRITE */ 49,
69 /* SMB2_LOCK */ 48,
70 /* SMB2_IOCTL */ 57,
71 /* SMB2_CANCEL */ 4,
72 /* SMB2_ECHO */ 4,
73 /* SMB2_QUERY_DIRECTORY */ 33,
74 /* SMB2_CHANGE_NOTIFY */ 32,
75 /* SMB2_QUERY_INFO */ 41,
76 /* SMB2_SET_INFO */ 33,
77 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
78};
79
80
81static void
82smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
83 const struct cifs_tcon *tcon)
84{
85 struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
86 char *temp = (char *)hdr;
87 /* lookup word count ie StructureSize from table */
88 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
89
90 /*
91 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
92 * largest operations (Create)
93 */
94 memset(temp, 0, 256);
95
96 /* Note this is only network field converted to big endian */
97 hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
98 - 4 /* RFC 1001 length field itself not counted */);
99
Steve French373512e2015-12-18 13:05:30 -0600100 hdr->ProtocolId = SMB2_PROTO_NUMBER;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400101 hdr->StructureSize = cpu_to_le16(64);
102 hdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100103 if (tcon && tcon->ses && tcon->ses->server) {
104 struct TCP_Server_Info *server = tcon->ses->server;
105
106 spin_lock(&server->req_lock);
107 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500108 if (server->credits >= server->max_credits)
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100109 hdr->CreditRequest = cpu_to_le16(0);
110 else
111 hdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500112 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100113 server->credits, 2));
114 spin_unlock(&server->req_lock);
115 } else {
116 hdr->CreditRequest = cpu_to_le16(2);
117 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400118 hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
119
120 if (!tcon)
121 goto out;
122
Steve French2b80d042013-06-23 18:43:37 -0500123 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
124 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500125 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500126 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Steve French2b80d042013-06-23 18:43:37 -0500127 hdr->CreditCharge = cpu_to_le16(1);
128 /* else CreditCharge MBZ */
129
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400130 hdr->TreeId = tcon->tid;
131 /* Uid is not converted */
132 if (tcon->ses)
133 hdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500134
135 /*
136 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
137 * to pass the path on the Open SMB prefixed by \\server\share.
138 * Not sure when we would need to do the augmented path (if ever) and
139 * setting this flag breaks the SMB2 open operation since it is
140 * illegal to send an empty path name (without \\server\share prefix)
141 * when the DFS flag is set in the SMB open header. We could
142 * consider setting the flag on all operations other than open
143 * but it is safer to net set it for now.
144 */
145/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
146 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
147
Jeff Layton38d77c52013-05-26 07:01:00 -0400148 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700149 hdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400150out:
151 pdu->StructureSize2 = cpu_to_le16(parmsize);
152 return;
153}
154
155static int
156smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
157{
158 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400159 struct nls_table *nls_codepage;
160 struct cifs_ses *ses;
161 struct TCP_Server_Info *server;
162
163 /*
164 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
165 * check for tcp and smb session status done differently
166 * for those three - in the calling routine.
167 */
168 if (tcon == NULL)
169 return rc;
170
171 if (smb2_command == SMB2_TREE_CONNECT)
172 return rc;
173
174 if (tcon->tidStatus == CifsExiting) {
175 /*
176 * only tree disconnect, open, and write,
177 * (and ulogoff which does not have tcon)
178 * are allowed as we start force umount.
179 */
180 if ((smb2_command != SMB2_WRITE) &&
181 (smb2_command != SMB2_CREATE) &&
182 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500183 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
184 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400185 return -ENODEV;
186 }
187 }
188 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
189 (!tcon->ses->server))
190 return -EIO;
191
192 ses = tcon->ses;
193 server = ses->server;
194
195 /*
196 * Give demultiplex thread up to 10 seconds to reconnect, should be
197 * greater than cifs socket timeout which is 7 seconds
198 */
199 while (server->tcpStatus == CifsNeedReconnect) {
200 /*
201 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
202 * here since they are implicitly done when session drops.
203 */
204 switch (smb2_command) {
205 /*
206 * BB Should we keep oplock break and add flush to exceptions?
207 */
208 case SMB2_TREE_DISCONNECT:
209 case SMB2_CANCEL:
210 case SMB2_CLOSE:
211 case SMB2_OPLOCK_BREAK:
212 return -EAGAIN;
213 }
214
215 wait_event_interruptible_timeout(server->response_q,
216 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
217
218 /* are we still trying to reconnect? */
219 if (server->tcpStatus != CifsNeedReconnect)
220 break;
221
222 /*
223 * on "soft" mounts we wait once. Hard mounts keep
224 * retrying until process is killed or server comes
225 * back on-line
226 */
227 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500228 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400229 return -EHOSTDOWN;
230 }
231 }
232
233 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
234 return rc;
235
236 nls_codepage = load_nls_default();
237
238 /*
239 * need to prevent multiple threads trying to simultaneously reconnect
240 * the same SMB session
241 */
242 mutex_lock(&tcon->ses->session_mutex);
243 rc = cifs_negotiate_protocol(0, tcon->ses);
244 if (!rc && tcon->ses->need_reconnect)
245 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
246
247 if (rc || !tcon->need_reconnect) {
248 mutex_unlock(&tcon->ses->session_mutex);
249 goto out;
250 }
251
252 cifs_mark_open_files_invalid(tcon);
Steve French52ace1e2016-09-22 19:23:56 -0500253
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400254 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
255 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500256
257 if (tcon->use_persistent)
258 cifs_reopen_persistent_handles(tcon);
259
Joe Perchesf96637b2013-05-04 22:12:25 -0500260 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400261 if (rc)
262 goto out;
263 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400264out:
265 /*
266 * Check if handle based operation so we know whether we can continue
267 * or not without returning to caller to reset file handle.
268 */
269 /*
270 * BB Is flush done by server on drop of tcp session? Should we special
271 * case it and skip above?
272 */
273 switch (smb2_command) {
274 case SMB2_FLUSH:
275 case SMB2_READ:
276 case SMB2_WRITE:
277 case SMB2_LOCK:
278 case SMB2_IOCTL:
279 case SMB2_QUERY_DIRECTORY:
280 case SMB2_CHANGE_NOTIFY:
281 case SMB2_QUERY_INFO:
282 case SMB2_SET_INFO:
Pavel Shilovsky69d13b62016-11-29 11:30:58 -0800283 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400284 }
285 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400286 return rc;
287}
288
289/*
290 * Allocate and return pointer to an SMB request hdr, and set basic
291 * SMB information in the SMB header. If the return code is zero, this
292 * function must have filled in request_buf pointer.
293 */
294static int
295small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
296 void **request_buf)
297{
298 int rc = 0;
299
300 rc = smb2_reconnect(smb2_command, tcon);
301 if (rc)
302 return rc;
303
304 /* BB eventually switch this to SMB2 specific small buf size */
305 *request_buf = cifs_small_buf_get();
306 if (*request_buf == NULL) {
307 /* BB should we add a retry in here if not a writepage? */
308 return -ENOMEM;
309 }
310
311 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
312
313 if (tcon != NULL) {
314#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400315 uint16_t com_code = le16_to_cpu(smb2_command);
316 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400317#endif
318 cifs_stats_inc(&tcon->num_smbs_sent);
319 }
320
321 return rc;
322}
323
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500324#ifdef CONFIG_CIFS_SMB311
325/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
326#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
327
328
329#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
330#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
331
332static void
333build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
334{
335 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
336 pneg_ctxt->DataLength = cpu_to_le16(38);
337 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
338 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
339 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
340 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
341}
342
343static void
344build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
345{
346 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
347 pneg_ctxt->DataLength = cpu_to_le16(6);
348 pneg_ctxt->CipherCount = cpu_to_le16(2);
349 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
350 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
351}
352
353static void
354assemble_neg_contexts(struct smb2_negotiate_req *req)
355{
356
357 /* +4 is to account for the RFC1001 len field */
358 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
359
360 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
361 /* Add 2 to size to round to 8 byte boundary */
362 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
363 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
364 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
365 req->NegotiateContextCount = cpu_to_le16(2);
366 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
367 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
368}
369#else
370static void assemble_neg_contexts(struct smb2_negotiate_req *req)
371{
372 return;
373}
374#endif /* SMB311 */
375
376
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400377/*
378 *
379 * SMB2 Worker functions follow:
380 *
381 * The general structure of the worker functions is:
382 * 1) Call smb2_init (assembles SMB2 header)
383 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
384 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
385 * 4) Decode SMB2 command specific fields in the fixed length area
386 * 5) Decode variable length data area (if any for this SMB2 command type)
387 * 6) Call free smb buffer
388 * 7) return
389 *
390 */
391
392int
393SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
394{
395 struct smb2_negotiate_req *req;
396 struct smb2_negotiate_rsp *rsp;
397 struct kvec iov[1];
398 int rc = 0;
399 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400400 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400401 int blob_offset, blob_length;
402 char *security_blob;
403 int flags = CIFS_NEG_OP;
404
Joe Perchesf96637b2013-05-04 22:12:25 -0500405 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400406
Jeff Layton3534b852013-05-24 07:41:01 -0400407 if (!server) {
408 WARN(1, "%s: server is NULL!\n", __func__);
409 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400410 }
411
412 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
413 if (rc)
414 return rc;
415
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400416 req->hdr.SessionId = 0;
417
Steve Frenche4aa25e2012-10-01 12:26:22 -0500418 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400419
Steve Frenche4aa25e2012-10-01 12:26:22 -0500420 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
421 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400422
423 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400424 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500425 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400426 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500427 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400428 else
429 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400430
Steve Frenche4aa25e2012-10-01 12:26:22 -0500431 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400432
Steve French3c5f9be12014-05-13 13:37:45 -0700433 /* ClientGUID must be zero for SMB2.02 dialect */
434 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
435 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500436 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700437 memcpy(req->ClientGUID, server->client_guid,
438 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500439 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
440 assemble_neg_contexts(req);
441 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400442 iov[0].iov_base = (char *)req;
443 /* 4 for rfc1002 length field */
444 iov[0].iov_len = get_rfc1002_length(req) + 4;
445
446 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
447
448 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
449 /*
450 * No tcon so can't do
451 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
452 */
453 if (rc != 0)
454 goto neg_exit;
455
Joe Perchesf96637b2013-05-04 22:12:25 -0500456 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400457
Steve Frenche4aa25e2012-10-01 12:26:22 -0500458 /* BB we may eventually want to match the negotiated vs. requested
459 dialect, even though we are only requesting one at a time */
460 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500461 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500462 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500463 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500464 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500465 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500466 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
467 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600468#ifdef CONFIG_CIFS_SMB311
469 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
470 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
471#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400472 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500473 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500474 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400475 rc = -EIO;
476 goto neg_exit;
477 }
478 server->dialect = le16_to_cpu(rsp->DialectRevision);
479
Jeff Laytone598d1d82013-05-26 07:00:59 -0400480 /* SMB2 only has an extended negflavor */
481 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400482 /* set it to the maximum buffer size value we can send with 1 credit */
483 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
484 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400485 server->max_read = le32_to_cpu(rsp->MaxReadSize);
486 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
487 /* BB Do we need to validate the SecurityMode? */
488 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
489 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400490 /* Internal types */
491 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400492
493 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
494 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500495 /*
496 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
497 * for us will be
498 * ses->sectype = RawNTLMSSP;
499 * but for time being this is our only auth choice so doesn't matter.
500 * We just found a server which sets blob length to zero expecting raw.
501 */
502 if (blob_length == 0)
503 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700504
Jeff Layton38d77c52013-05-26 07:01:00 -0400505 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400506 if (rc)
507 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500508 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500509 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500510 if (rc == 1)
511 rc = 0;
512 else if (rc == 0)
513 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400514 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400515neg_exit:
516 free_rsp_buf(resp_buftype, rsp);
517 return rc;
518}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400519
Steve Frenchff1c0382013-11-19 23:44:46 -0600520int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
521{
522 int rc = 0;
523 struct validate_negotiate_info_req vneg_inbuf;
524 struct validate_negotiate_info_rsp *pneg_rsp;
525 u32 rsplen;
526
527 cifs_dbg(FYI, "validate negotiate\n");
528
529 /*
530 * validation ioctl must be signed, so no point sending this if we
531 * can not sign it. We could eventually change this to selectively
532 * sign just this, the first and only signed request on a connection.
533 * This is good enough for now since a user who wants better security
534 * would also enable signing on the mount. Having validation of
535 * negotiate info for signed connections helps reduce attack vectors
536 */
537 if (tcon->ses->server->sign == false)
538 return 0; /* validation requires signing */
539
540 vneg_inbuf.Capabilities =
541 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100542 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
543 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600544
545 if (tcon->ses->sign)
546 vneg_inbuf.SecurityMode =
547 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
548 else if (global_secflags & CIFSSEC_MAY_SIGN)
549 vneg_inbuf.SecurityMode =
550 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
551 else
552 vneg_inbuf.SecurityMode = 0;
553
554 vneg_inbuf.DialectCount = cpu_to_le16(1);
555 vneg_inbuf.Dialects[0] =
556 cpu_to_le16(tcon->ses->server->vals->protocol_id);
557
558 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
559 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
560 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
561 (char **)&pneg_rsp, &rsplen);
562
563 if (rc != 0) {
564 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
565 return -EIO;
566 }
567
568 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
569 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
570 return -EIO;
571 }
572
573 /* check validate negotiate info response matches what we got earlier */
574 if (pneg_rsp->Dialect !=
575 cpu_to_le16(tcon->ses->server->vals->protocol_id))
576 goto vneg_out;
577
578 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
579 goto vneg_out;
580
581 /* do not validate server guid because not saved at negprot time yet */
582
583 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
584 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
585 goto vneg_out;
586
587 /* validate negotiate successful */
588 cifs_dbg(FYI, "validate negotiate info successful\n");
589 return 0;
590
591vneg_out:
592 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
593 return -EIO;
594}
595
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100596struct SMB2_sess_data {
597 unsigned int xid;
598 struct cifs_ses *ses;
599 struct nls_table *nls_cp;
600 void (*func)(struct SMB2_sess_data *);
601 int result;
602 u64 previous_session;
603
604 /* we will send the SMB in three pieces:
605 * a fixed length beginning part, an optional
606 * SPNEGO blob (which can be zero length), and a
607 * last part which will include the strings
608 * and rest of bcc area. This allows us to avoid
609 * a large buffer 17K allocation
610 */
611 int buf0_type;
612 struct kvec iov[2];
613};
614
615static int
616SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
617{
618 int rc;
619 struct cifs_ses *ses = sess_data->ses;
620 struct smb2_sess_setup_req *req;
621 struct TCP_Server_Info *server = ses->server;
622
623 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
624 if (rc)
625 return rc;
626
627 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
628
629 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
630 req->PreviousSessionId = sess_data->previous_session;
631
632 req->Flags = 0; /* MBZ */
633 /* to enable echos and oplocks */
634 req->hdr.CreditRequest = cpu_to_le16(3);
635
636 /* only one of SMB2 signing flags may be set in SMB2 request */
637 if (server->sign)
638 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
639 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
640 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
641 else
642 req->SecurityMode = 0;
643
644 req->Capabilities = 0;
645 req->Channel = 0; /* MBZ */
646
647 sess_data->iov[0].iov_base = (char *)req;
648 /* 4 for rfc1002 length field and 1 for pad */
649 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
650 /*
651 * This variable will be used to clear the buffer
652 * allocated above in case of any error in the calling function.
653 */
654 sess_data->buf0_type = CIFS_SMALL_BUFFER;
655
656 return 0;
657}
658
659static void
660SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
661{
662 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
663 sess_data->buf0_type = CIFS_NO_BUFFER;
664}
665
666static int
667SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
668{
669 int rc;
670 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
671
672 /* Testing shows that buffer offset must be at location of Buffer[0] */
673 req->SecurityBufferOffset =
674 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
675 1 /* pad */ - 4 /* rfc1001 len */);
676 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
677
678 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
679
680 /* BB add code to build os and lm fields */
681
682 rc = SendReceive2(sess_data->xid, sess_data->ses,
683 sess_data->iov, 2,
684 &sess_data->buf0_type,
685 CIFS_LOG_ERROR | CIFS_NEG_OP);
686
687 return rc;
688}
689
690static int
691SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
692{
693 int rc = 0;
694 struct cifs_ses *ses = sess_data->ses;
695
696 mutex_lock(&ses->server->srv_mutex);
697 if (ses->server->sign && ses->server->ops->generate_signingkey) {
698 rc = ses->server->ops->generate_signingkey(ses);
699 kfree(ses->auth_key.response);
700 ses->auth_key.response = NULL;
701 if (rc) {
702 cifs_dbg(FYI,
703 "SMB3 session key generation failed\n");
704 mutex_unlock(&ses->server->srv_mutex);
705 goto keygen_exit;
706 }
707 }
708 if (!ses->server->session_estab) {
709 ses->server->sequence_number = 0x2;
710 ses->server->session_estab = true;
711 }
712 mutex_unlock(&ses->server->srv_mutex);
713
714 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
715 spin_lock(&GlobalMid_Lock);
716 ses->status = CifsGood;
717 ses->need_reconnect = false;
718 spin_unlock(&GlobalMid_Lock);
719
720keygen_exit:
721 if (!ses->server->sign) {
722 kfree(ses->auth_key.response);
723 ses->auth_key.response = NULL;
724 }
725 return rc;
726}
727
728#ifdef CONFIG_CIFS_UPCALL
729static void
730SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
731{
732 int rc;
733 struct cifs_ses *ses = sess_data->ses;
734 struct cifs_spnego_msg *msg;
735 struct key *spnego_key = NULL;
736 struct smb2_sess_setup_rsp *rsp = NULL;
737
738 rc = SMB2_sess_alloc_buffer(sess_data);
739 if (rc)
740 goto out;
741
742 spnego_key = cifs_get_spnego_key(ses);
743 if (IS_ERR(spnego_key)) {
744 rc = PTR_ERR(spnego_key);
745 spnego_key = NULL;
746 goto out;
747 }
748
749 msg = spnego_key->payload.data[0];
750 /*
751 * check version field to make sure that cifs.upcall is
752 * sending us a response in an expected form
753 */
754 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
755 cifs_dbg(VFS,
756 "bad cifs.upcall version. Expected %d got %d",
757 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
758 rc = -EKEYREJECTED;
759 goto out_put_spnego_key;
760 }
761
762 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
763 GFP_KERNEL);
764 if (!ses->auth_key.response) {
765 cifs_dbg(VFS,
766 "Kerberos can't allocate (%u bytes) memory",
767 msg->sesskey_len);
768 rc = -ENOMEM;
769 goto out_put_spnego_key;
770 }
771 ses->auth_key.len = msg->sesskey_len;
772
773 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
774 sess_data->iov[1].iov_len = msg->secblob_len;
775
776 rc = SMB2_sess_sendreceive(sess_data);
777 if (rc)
778 goto out_put_spnego_key;
779
780 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
781 ses->Suid = rsp->hdr.SessionId;
782
783 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
784 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
785 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
786
787 rc = SMB2_sess_establish_session(sess_data);
788out_put_spnego_key:
789 key_invalidate(spnego_key);
790 key_put(spnego_key);
791out:
792 sess_data->result = rc;
793 sess_data->func = NULL;
794 SMB2_sess_free_buffer(sess_data);
795}
796#else
797static void
798SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
799{
800 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
801 sess_data->result = -EOPNOTSUPP;
802 sess_data->func = NULL;
803}
804#endif
805
Sachin Prabhu166cea42016-10-07 19:11:22 +0100806static void
807SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
808
809static void
810SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
811{
812 int rc;
813 struct cifs_ses *ses = sess_data->ses;
814 struct smb2_sess_setup_rsp *rsp = NULL;
815 char *ntlmssp_blob = NULL;
816 bool use_spnego = false; /* else use raw ntlmssp */
817 u16 blob_length = 0;
818
819 /*
820 * If memory allocation is successful, caller of this function
821 * frees it.
822 */
823 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
824 if (!ses->ntlmssp) {
825 rc = -ENOMEM;
826 goto out_err;
827 }
828 ses->ntlmssp->sesskey_per_smbsess = true;
829
830 rc = SMB2_sess_alloc_buffer(sess_data);
831 if (rc)
832 goto out_err;
833
834 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
835 GFP_KERNEL);
836 if (ntlmssp_blob == NULL) {
837 rc = -ENOMEM;
838 goto out;
839 }
840
841 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
842 if (use_spnego) {
843 /* BB eventually need to add this */
844 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
845 rc = -EOPNOTSUPP;
846 goto out;
847 } else {
848 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
849 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
850 }
851 sess_data->iov[1].iov_base = ntlmssp_blob;
852 sess_data->iov[1].iov_len = blob_length;
853
854 rc = SMB2_sess_sendreceive(sess_data);
855 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
856
857 /* If true, rc here is expected and not an error */
858 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
859 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
860 rc = 0;
861
862 if (rc)
863 goto out;
864
865 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
866 le16_to_cpu(rsp->SecurityBufferOffset)) {
867 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
868 le16_to_cpu(rsp->SecurityBufferOffset));
869 rc = -EIO;
870 goto out;
871 }
872 rc = decode_ntlmssp_challenge(rsp->Buffer,
873 le16_to_cpu(rsp->SecurityBufferLength), ses);
874 if (rc)
875 goto out;
876
877 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
878
879
880 ses->Suid = rsp->hdr.SessionId;
881 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
882 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
883 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
884
885out:
886 kfree(ntlmssp_blob);
887 SMB2_sess_free_buffer(sess_data);
888 if (!rc) {
889 sess_data->result = 0;
890 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
891 return;
892 }
893out_err:
894 kfree(ses->ntlmssp);
895 ses->ntlmssp = NULL;
896 sess_data->result = rc;
897 sess_data->func = NULL;
898}
899
900static void
901SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
902{
903 int rc;
904 struct cifs_ses *ses = sess_data->ses;
905 struct smb2_sess_setup_req *req;
906 struct smb2_sess_setup_rsp *rsp = NULL;
907 unsigned char *ntlmssp_blob = NULL;
908 bool use_spnego = false; /* else use raw ntlmssp */
909 u16 blob_length = 0;
910
911 rc = SMB2_sess_alloc_buffer(sess_data);
912 if (rc)
913 goto out;
914
915 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
916 req->hdr.SessionId = ses->Suid;
917
918 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
919 sess_data->nls_cp);
920 if (rc) {
921 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
922 goto out;
923 }
924
925 if (use_spnego) {
926 /* BB eventually need to add this */
927 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
928 rc = -EOPNOTSUPP;
929 goto out;
930 }
931 sess_data->iov[1].iov_base = ntlmssp_blob;
932 sess_data->iov[1].iov_len = blob_length;
933
934 rc = SMB2_sess_sendreceive(sess_data);
935 if (rc)
936 goto out;
937
938 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
939
940 ses->Suid = rsp->hdr.SessionId;
941 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
942 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
943 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
944
945 rc = SMB2_sess_establish_session(sess_data);
946out:
947 kfree(ntlmssp_blob);
948 SMB2_sess_free_buffer(sess_data);
949 kfree(ses->ntlmssp);
950 ses->ntlmssp = NULL;
951 sess_data->result = rc;
952 sess_data->func = NULL;
953}
954
955static int
956SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
957{
958 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
959 ses->sectype = RawNTLMSSP;
960
961 switch (ses->sectype) {
962 case Kerberos:
963 sess_data->func = SMB2_auth_kerberos;
964 break;
965 case RawNTLMSSP:
966 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
967 break;
968 default:
969 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
970 return -EOPNOTSUPP;
971 }
972
973 return 0;
974}
975
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400976int
977SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
978 const struct nls_table *nls_cp)
979{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400980 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -0400981 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100982 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400983
Joe Perchesf96637b2013-05-04 22:12:25 -0500984 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400985
Jeff Layton3534b852013-05-24 07:41:01 -0400986 if (!server) {
987 WARN(1, "%s: server is NULL!\n", __func__);
988 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400989 }
990
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100991 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
992 if (!sess_data)
993 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100994
995 rc = SMB2_select_sec(ses, sess_data);
996 if (rc)
997 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100998 sess_data->xid = xid;
999 sess_data->ses = ses;
1000 sess_data->buf0_type = CIFS_NO_BUFFER;
1001 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001002
Sachin Prabhu166cea42016-10-07 19:11:22 +01001003 while (sess_data->func)
1004 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001005
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001006 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001007out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001008 kfree(sess_data);
1009 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001010}
1011
1012int
1013SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1014{
1015 struct smb2_logoff_req *req; /* response is also trivial struct */
1016 int rc = 0;
1017 struct TCP_Server_Info *server;
1018
Joe Perchesf96637b2013-05-04 22:12:25 -05001019 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001020
1021 if (ses && (ses->server))
1022 server = ses->server;
1023 else
1024 return -EIO;
1025
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001026 /* no need to send SMB logoff if uid already closed due to reconnect */
1027 if (ses->need_reconnect)
1028 goto smb2_session_already_dead;
1029
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001030 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1031 if (rc)
1032 return rc;
1033
1034 /* since no tcon, smb2_init can not do this, so do here */
1035 req->hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -04001036 if (server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001037 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001038
1039 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
1040 /*
1041 * No tcon so can't do
1042 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1043 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001044
1045smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001046 return rc;
1047}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001048
1049static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1050{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001051 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001052}
1053
1054#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1055
Steve Frenchde9f68d2013-11-15 11:26:24 -06001056/* These are similar values to what Windows uses */
1057static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1058{
1059 tcon->max_chunks = 256;
1060 tcon->max_bytes_chunk = 1048576;
1061 tcon->max_bytes_copy = 16777216;
1062}
1063
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001064int
1065SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1066 struct cifs_tcon *tcon, const struct nls_table *cp)
1067{
1068 struct smb2_tree_connect_req *req;
1069 struct smb2_tree_connect_rsp *rsp = NULL;
1070 struct kvec iov[2];
1071 int rc = 0;
1072 int resp_buftype;
1073 int unc_path_len;
1074 struct TCP_Server_Info *server;
1075 __le16 *unc_path = NULL;
1076
Joe Perchesf96637b2013-05-04 22:12:25 -05001077 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001078
1079 if ((ses->server) && tree)
1080 server = ses->server;
1081 else
1082 return -EIO;
1083
1084 if (tcon && tcon->bad_network_name)
1085 return -ENOENT;
1086
Steve Frenchff9f84b2015-09-26 09:48:58 -05001087 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -05001088 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1089 cifs_dbg(VFS, "encryption requested but no server support");
1090 return -EOPNOTSUPP;
1091 }
1092
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001093 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1094 if (unc_path == NULL)
1095 return -ENOMEM;
1096
1097 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1098 unc_path_len *= 2;
1099 if (unc_path_len < 2) {
1100 kfree(unc_path);
1101 return -EINVAL;
1102 }
1103
1104 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1105 if (rc) {
1106 kfree(unc_path);
1107 return rc;
1108 }
1109
1110 if (tcon == NULL) {
1111 /* since no tcon, smb2_init can not do this, so do here */
1112 req->hdr.SessionId = ses->Suid;
1113 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1114 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1115 }
1116
1117 iov[0].iov_base = (char *)req;
1118 /* 4 for rfc1002 length field and 1 for pad */
1119 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1120
1121 /* Testing shows that buffer offset must be at location of Buffer[0] */
1122 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1123 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1124 req->PathLength = cpu_to_le16(unc_path_len - 2);
1125 iov[1].iov_base = unc_path;
1126 iov[1].iov_len = unc_path_len;
1127
1128 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1129
1130 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1131 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
1132
1133 if (rc != 0) {
1134 if (tcon) {
1135 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1136 tcon->need_reconnect = true;
1137 }
1138 goto tcon_error_exit;
1139 }
1140
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001141 if (tcon == NULL) {
1142 ses->ipc_tid = rsp->hdr.TreeId;
1143 goto tcon_exit;
1144 }
1145
1146 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001147 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001148 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1149 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001150 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001151 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1152 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001153 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001154 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001155 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001156 rc = -EOPNOTSUPP;
1157 goto tcon_error_exit;
1158 }
1159
1160 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001161 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001162 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1163 tcon->tidStatus = CifsGood;
1164 tcon->need_reconnect = false;
1165 tcon->tid = rsp->hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001166 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001167
1168 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1169 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001170 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001171 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001172 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1173 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001174 if (tcon->ses->server->ops->validate_negotiate)
1175 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001176tcon_exit:
1177 free_rsp_buf(resp_buftype, rsp);
1178 kfree(unc_path);
1179 return rc;
1180
1181tcon_error_exit:
1182 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001183 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Steve French18f39e72014-08-17 00:22:24 -05001184 if (tcon)
1185 tcon->bad_network_name = true;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001186 }
1187 goto tcon_exit;
1188}
1189
1190int
1191SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1192{
1193 struct smb2_tree_disconnect_req *req; /* response is trivial */
1194 int rc = 0;
1195 struct TCP_Server_Info *server;
1196 struct cifs_ses *ses = tcon->ses;
1197
Joe Perchesf96637b2013-05-04 22:12:25 -05001198 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001199
1200 if (ses && (ses->server))
1201 server = ses->server;
1202 else
1203 return -EIO;
1204
1205 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1206 return 0;
1207
1208 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1209 if (rc)
1210 return rc;
1211
1212 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1213 if (rc)
1214 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1215
1216 return rc;
1217}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001218
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001219
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001220static struct create_durable *
1221create_durable_buf(void)
1222{
1223 struct create_durable *buf;
1224
1225 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1226 if (!buf)
1227 return NULL;
1228
1229 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001230 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001231 buf->ccontext.DataLength = cpu_to_le32(16);
1232 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1233 (struct create_durable, Name));
1234 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001235 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001236 buf->Name[0] = 'D';
1237 buf->Name[1] = 'H';
1238 buf->Name[2] = 'n';
1239 buf->Name[3] = 'Q';
1240 return buf;
1241}
1242
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001243static struct create_durable *
1244create_reconnect_durable_buf(struct cifs_fid *fid)
1245{
1246 struct create_durable *buf;
1247
1248 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1249 if (!buf)
1250 return NULL;
1251
1252 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1253 (struct create_durable, Data));
1254 buf->ccontext.DataLength = cpu_to_le32(16);
1255 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1256 (struct create_durable, Name));
1257 buf->ccontext.NameLength = cpu_to_le16(4);
1258 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1259 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001260 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001261 buf->Name[0] = 'D';
1262 buf->Name[1] = 'H';
1263 buf->Name[2] = 'n';
1264 buf->Name[3] = 'C';
1265 return buf;
1266}
1267
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001268static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001269parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1270 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001271{
1272 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001273 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001274 unsigned int next;
1275 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001276 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001277
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001278 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001279 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001280 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001281 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001282 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001283 if (le16_to_cpu(cc->NameLength) == 4 &&
1284 strncmp(name, "RqLs", 4) == 0)
1285 return server->ops->parse_lease_buf(cc, epoch);
1286
1287 next = le32_to_cpu(cc->Next);
1288 if (!next)
1289 break;
1290 remaining -= next;
1291 cc = (struct create_context *)((char *)cc + next);
1292 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001293
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001294 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001295}
1296
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001297static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001298add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1299 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001300{
1301 struct smb2_create_req *req = iov[0].iov_base;
1302 unsigned int num = *num_iovec;
1303
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001304 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001305 if (iov[num].iov_base == NULL)
1306 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001307 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001308 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1309 if (!req->CreateContextsOffset)
1310 req->CreateContextsOffset = cpu_to_le32(
1311 sizeof(struct smb2_create_req) - 4 +
1312 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001313 le32_add_cpu(&req->CreateContextsLength,
1314 server->vals->create_lease_size);
1315 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001316 *num_iovec = num + 1;
1317 return 0;
1318}
1319
Steve Frenchb56eae42015-11-03 09:26:27 -06001320static struct create_durable_v2 *
1321create_durable_v2_buf(struct cifs_fid *pfid)
1322{
1323 struct create_durable_v2 *buf;
1324
1325 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1326 if (!buf)
1327 return NULL;
1328
1329 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1330 (struct create_durable_v2, dcontext));
1331 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1332 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1333 (struct create_durable_v2, Name));
1334 buf->ccontext.NameLength = cpu_to_le16(4);
1335
1336 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1337 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001338 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001339 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1340
1341 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1342 buf->Name[0] = 'D';
1343 buf->Name[1] = 'H';
1344 buf->Name[2] = '2';
1345 buf->Name[3] = 'Q';
1346 return buf;
1347}
1348
1349static struct create_durable_handle_reconnect_v2 *
1350create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1351{
1352 struct create_durable_handle_reconnect_v2 *buf;
1353
1354 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1355 GFP_KERNEL);
1356 if (!buf)
1357 return NULL;
1358
1359 buf->ccontext.DataOffset =
1360 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1361 dcontext));
1362 buf->ccontext.DataLength =
1363 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1364 buf->ccontext.NameOffset =
1365 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1366 Name));
1367 buf->ccontext.NameLength = cpu_to_le16(4);
1368
1369 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1370 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1371 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1372 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1373
1374 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1375 buf->Name[0] = 'D';
1376 buf->Name[1] = 'H';
1377 buf->Name[2] = '2';
1378 buf->Name[3] = 'C';
1379 return buf;
1380}
1381
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001382static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001383add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001384 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001385{
1386 struct smb2_create_req *req = iov[0].iov_base;
1387 unsigned int num = *num_iovec;
1388
Steve Frenchb56eae42015-11-03 09:26:27 -06001389 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1390 if (iov[num].iov_base == NULL)
1391 return -ENOMEM;
1392 iov[num].iov_len = sizeof(struct create_durable_v2);
1393 if (!req->CreateContextsOffset)
1394 req->CreateContextsOffset =
1395 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1396 iov[1].iov_len);
1397 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1398 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1399 *num_iovec = num + 1;
1400 return 0;
1401}
1402
1403static int
1404add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1405 struct cifs_open_parms *oparms)
1406{
1407 struct smb2_create_req *req = iov[0].iov_base;
1408 unsigned int num = *num_iovec;
1409
1410 /* indicate that we don't need to relock the file */
1411 oparms->reconnect = false;
1412
1413 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1414 if (iov[num].iov_base == NULL)
1415 return -ENOMEM;
1416 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1417 if (!req->CreateContextsOffset)
1418 req->CreateContextsOffset =
1419 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1420 iov[1].iov_len);
1421 le32_add_cpu(&req->CreateContextsLength,
1422 sizeof(struct create_durable_handle_reconnect_v2));
1423 inc_rfc1001_len(&req->hdr,
1424 sizeof(struct create_durable_handle_reconnect_v2));
1425 *num_iovec = num + 1;
1426 return 0;
1427}
1428
1429static int
1430add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1431 struct cifs_open_parms *oparms, bool use_persistent)
1432{
1433 struct smb2_create_req *req = iov[0].iov_base;
1434 unsigned int num = *num_iovec;
1435
1436 if (use_persistent) {
1437 if (oparms->reconnect)
1438 return add_durable_reconnect_v2_context(iov, num_iovec,
1439 oparms);
1440 else
1441 return add_durable_v2_context(iov, num_iovec, oparms);
1442 }
1443
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001444 if (oparms->reconnect) {
1445 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1446 /* indicate that we don't need to relock the file */
1447 oparms->reconnect = false;
1448 } else
1449 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001450 if (iov[num].iov_base == NULL)
1451 return -ENOMEM;
1452 iov[num].iov_len = sizeof(struct create_durable);
1453 if (!req->CreateContextsOffset)
1454 req->CreateContextsOffset =
1455 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1456 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001457 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001458 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1459 *num_iovec = num + 1;
1460 return 0;
1461}
1462
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001463int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001464SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001465 __u8 *oplock, struct smb2_file_all_info *buf,
1466 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001467{
1468 struct smb2_create_req *req;
1469 struct smb2_create_rsp *rsp;
1470 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001471 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001472 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001473 struct kvec iov[4];
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001474 int resp_buftype;
1475 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001476 __le16 *copy_path = NULL;
1477 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001478 int rc = 0;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001479 unsigned int num_iovecs = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001480 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001481 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001482
Joe Perchesf96637b2013-05-04 22:12:25 -05001483 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001484
1485 if (ses && (ses->server))
1486 server = ses->server;
1487 else
1488 return -EIO;
1489
1490 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1491 if (rc)
1492 return rc;
1493
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001494 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001495 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001496 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1497 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001498
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001499 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001500 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001501 /* File attributes ignored on open (used in create though) */
1502 req->FileAttributes = cpu_to_le32(file_attributes);
1503 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001504 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1505 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001506 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001507 /* do not count rfc1001 len field */
1508 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001509
1510 iov[0].iov_base = (char *)req;
1511 /* 4 for rfc1002 length field */
1512 iov[0].iov_len = get_rfc1002_length(req) + 4;
1513
1514 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001515 req->NameLength = cpu_to_le16(uni_path_len - 2);
1516 /* -1 since last byte is buf[0] which is sent below (path) */
1517 iov[0].iov_len--;
1518 if (uni_path_len % 8 != 0) {
1519 copy_size = uni_path_len / 8 * 8;
1520 if (copy_size < uni_path_len)
1521 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001522
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001523 copy_path = kzalloc(copy_size, GFP_KERNEL);
1524 if (!copy_path)
1525 return -ENOMEM;
1526 memcpy((char *)copy_path, (const char *)path,
1527 uni_path_len);
1528 uni_path_len = copy_size;
1529 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001530 }
1531
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001532 iov[1].iov_len = uni_path_len;
1533 iov[1].iov_base = path;
1534 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1535 inc_rfc1001_len(req, uni_path_len - 1);
1536
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001537 if (!server->oplocks)
1538 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1539
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001540 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001541 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1542 req->RequestedOplockLevel = *oplock;
1543 else {
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001544 rc = add_lease_context(server, iov, &num_iovecs, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001545 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001546 cifs_small_buf_release(req);
1547 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001548 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001549 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001550 lc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001551 }
1552
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001553 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1554 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001555 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001556 struct create_context *ccontext =
1557 (struct create_context *)iov[num_iovecs-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001558 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001559 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001560 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001561
1562 rc = add_durable_context(iov, &num_iovecs, oparms,
1563 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001564 if (rc) {
1565 cifs_small_buf_release(req);
1566 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001567 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001568 return rc;
1569 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001570 dhc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001571 }
1572
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001573 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1574 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1575
1576 if (rc != 0) {
1577 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001578 if (err_buf)
1579 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1580 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001581 goto creat_exit;
1582 }
1583
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001584 oparms->fid->persistent_fid = rsp->PersistentFileId;
1585 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001586
1587 if (buf) {
1588 memcpy(buf, &rsp->CreationTime, 32);
1589 buf->AllocationSize = rsp->AllocationSize;
1590 buf->EndOfFile = rsp->EndofFile;
1591 buf->Attributes = rsp->FileAttributes;
1592 buf->NumberOfLinks = cpu_to_le32(1);
1593 buf->DeletePending = 0;
1594 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001595
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001596 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001597 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001598 else
1599 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001600creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001601 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001602 kfree(lc_buf);
1603 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001604 free_rsp_buf(resp_buftype, rsp);
1605 return rc;
1606}
1607
Steve French4a72daf2013-06-25 00:20:49 -05001608/*
1609 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1610 */
1611int
1612SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1613 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1614 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1615{
1616 struct smb2_ioctl_req *req;
1617 struct smb2_ioctl_rsp *rsp;
1618 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001619 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001620 struct kvec iov[2];
1621 int resp_buftype;
1622 int num_iovecs;
1623 int rc = 0;
1624
1625 cifs_dbg(FYI, "SMB2 IOCTL\n");
1626
Steve French3d1a3742014-08-11 21:05:25 -05001627 if (out_data != NULL)
1628 *out_data = NULL;
1629
Steve French4a72daf2013-06-25 00:20:49 -05001630 /* zero out returned data len, in case of error */
1631 if (plen)
1632 *plen = 0;
1633
Steve French8e353102015-03-26 19:47:02 -05001634 if (tcon)
1635 ses = tcon->ses;
1636 else
1637 return -EIO;
1638
Steve French4a72daf2013-06-25 00:20:49 -05001639 if (ses && (ses->server))
1640 server = ses->server;
1641 else
1642 return -EIO;
1643
1644 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1645 if (rc)
1646 return rc;
1647
1648 req->CtlCode = cpu_to_le32(opcode);
1649 req->PersistentFileId = persistent_fid;
1650 req->VolatileFileId = volatile_fid;
1651
1652 if (indatalen) {
1653 req->InputCount = cpu_to_le32(indatalen);
1654 /* do not set InputOffset if no input data */
1655 req->InputOffset =
1656 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1657 iov[1].iov_base = in_data;
1658 iov[1].iov_len = indatalen;
1659 num_iovecs = 2;
1660 } else
1661 num_iovecs = 1;
1662
1663 req->OutputOffset = 0;
1664 req->OutputCount = 0; /* MBZ */
1665
1666 /*
1667 * Could increase MaxOutputResponse, but that would require more
1668 * than one credit. Windows typically sets this smaller, but for some
1669 * ioctls it may be useful to allow server to send more. No point
1670 * limiting what the server can send as long as fits in one credit
1671 */
1672 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1673
1674 if (is_fsctl)
1675 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1676 else
1677 req->Flags = 0;
1678
1679 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001680
Steve French7ff8d452013-10-14 00:44:19 -05001681 /*
1682 * If no input data, the size of ioctl struct in
1683 * protocol spec still includes a 1 byte data buffer,
1684 * but if input data passed to ioctl, we do not
1685 * want to double count this, so we do not send
1686 * the dummy one byte of data in iovec[0] if sending
1687 * input data (in iovec[1]). We also must add 4 bytes
1688 * in first iovec to allow for rfc1002 length field.
1689 */
1690
1691 if (indatalen) {
1692 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1693 inc_rfc1001_len(req, indatalen - 1);
1694 } else
1695 iov[0].iov_len = get_rfc1002_length(req) + 4;
1696
Steve French4a72daf2013-06-25 00:20:49 -05001697
1698 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1699 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1700
Steve French9bf0c9c2013-11-16 18:05:28 -06001701 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001702 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001703 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001704 } else if (rc == -EINVAL) {
1705 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1706 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001707 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001708 goto ioctl_exit;
1709 }
Steve French4a72daf2013-06-25 00:20:49 -05001710 }
1711
1712 /* check if caller wants to look at return data or just return rc */
1713 if ((plen == NULL) || (out_data == NULL))
1714 goto ioctl_exit;
1715
1716 *plen = le32_to_cpu(rsp->OutputCount);
1717
1718 /* We check for obvious errors in the output buffer length and offset */
1719 if (*plen == 0)
1720 goto ioctl_exit; /* server returned no data */
1721 else if (*plen > 0xFF00) {
1722 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1723 *plen = 0;
1724 rc = -EIO;
1725 goto ioctl_exit;
1726 }
1727
1728 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1729 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1730 le32_to_cpu(rsp->OutputOffset));
1731 *plen = 0;
1732 rc = -EIO;
1733 goto ioctl_exit;
1734 }
1735
1736 *out_data = kmalloc(*plen, GFP_KERNEL);
1737 if (*out_data == NULL) {
1738 rc = -ENOMEM;
1739 goto ioctl_exit;
1740 }
1741
Steve French373512e2015-12-18 13:05:30 -06001742 memcpy(*out_data,
1743 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
Steve French4a72daf2013-06-25 00:20:49 -05001744 *plen);
1745ioctl_exit:
1746 free_rsp_buf(resp_buftype, rsp);
1747 return rc;
1748}
1749
Steve French64a5cfa2013-10-14 15:31:32 -05001750/*
1751 * Individual callers to ioctl worker function follow
1752 */
1753
1754int
1755SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1756 u64 persistent_fid, u64 volatile_fid)
1757{
1758 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001759 struct compress_ioctl fsctl_input;
1760 char *ret_data = NULL;
1761
1762 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001763 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001764
1765 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1766 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1767 (char *)&fsctl_input /* data input */,
1768 2 /* in data len */, &ret_data /* out data */, NULL);
1769
1770 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001771
1772 return rc;
1773}
1774
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001775int
1776SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1777 u64 persistent_fid, u64 volatile_fid)
1778{
1779 struct smb2_close_req *req;
1780 struct smb2_close_rsp *rsp;
1781 struct TCP_Server_Info *server;
1782 struct cifs_ses *ses = tcon->ses;
1783 struct kvec iov[1];
1784 int resp_buftype;
1785 int rc = 0;
1786
Joe Perchesf96637b2013-05-04 22:12:25 -05001787 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001788
1789 if (ses && (ses->server))
1790 server = ses->server;
1791 else
1792 return -EIO;
1793
1794 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1795 if (rc)
1796 return rc;
1797
1798 req->PersistentFileId = persistent_fid;
1799 req->VolatileFileId = volatile_fid;
1800
1801 iov[0].iov_base = (char *)req;
1802 /* 4 for rfc1002 length field */
1803 iov[0].iov_len = get_rfc1002_length(req) + 4;
1804
1805 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1806 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1807
1808 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001809 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001810 goto close_exit;
1811 }
1812
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001813 /* BB FIXME - decode close response, update inode for caching */
1814
1815close_exit:
1816 free_rsp_buf(resp_buftype, rsp);
1817 return rc;
1818}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001819
1820static int
1821validate_buf(unsigned int offset, unsigned int buffer_length,
1822 struct smb2_hdr *hdr, unsigned int min_buf_size)
1823
1824{
1825 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1826 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1827 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1828 char *end_of_buf = begin_of_buf + buffer_length;
1829
1830
1831 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001832 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1833 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001834 return -EINVAL;
1835 }
1836
1837 /* check if beyond RFC1001 maximum length */
1838 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001839 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1840 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001841 return -EINVAL;
1842 }
1843
1844 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001845 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001846 return -EINVAL;
1847 }
1848
1849 return 0;
1850}
1851
1852/*
1853 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1854 * Caller must free buffer.
1855 */
1856static int
1857validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1858 struct smb2_hdr *hdr, unsigned int minbufsize,
1859 char *data)
1860
1861{
1862 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1863 int rc;
1864
1865 if (!data)
1866 return -EINVAL;
1867
1868 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1869 if (rc)
1870 return rc;
1871
1872 memcpy(data, begin_of_buf, buffer_length);
1873
1874 return 0;
1875}
1876
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001877static int
1878query_info(const unsigned int xid, struct cifs_tcon *tcon,
1879 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1880 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001881{
1882 struct smb2_query_info_req *req;
1883 struct smb2_query_info_rsp *rsp = NULL;
1884 struct kvec iov[2];
1885 int rc = 0;
1886 int resp_buftype;
1887 struct TCP_Server_Info *server;
1888 struct cifs_ses *ses = tcon->ses;
1889
Joe Perchesf96637b2013-05-04 22:12:25 -05001890 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001891
1892 if (ses && (ses->server))
1893 server = ses->server;
1894 else
1895 return -EIO;
1896
1897 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1898 if (rc)
1899 return rc;
1900
1901 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001902 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001903 req->PersistentFileId = persistent_fid;
1904 req->VolatileFileId = volatile_fid;
1905 /* 4 for rfc1002 length field and 1 for Buffer */
1906 req->InputBufferOffset =
1907 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001908 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001909
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);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001915 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1916
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001917 if (rc) {
1918 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1919 goto qinf_exit;
1920 }
1921
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001922 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1923 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001924 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001925
1926qinf_exit:
1927 free_rsp_buf(resp_buftype, rsp);
1928 return rc;
1929}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001930
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001931int
1932SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1933 u64 persistent_fid, u64 volatile_fid,
1934 struct smb2_file_all_info *data)
1935{
1936 return query_info(xid, tcon, persistent_fid, volatile_fid,
1937 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001938 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001939 sizeof(struct smb2_file_all_info), data);
1940}
1941
1942int
1943SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1944 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1945{
1946 return query_info(xid, tcon, persistent_fid, volatile_fid,
1947 FILE_INTERNAL_INFORMATION,
1948 sizeof(struct smb2_file_internal_info),
1949 sizeof(struct smb2_file_internal_info), uniqueid);
1950}
1951
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001952/*
1953 * This is a no-op for now. We're not really interested in the reply, but
1954 * rather in the fact that the server sent one and that server->lstrp
1955 * gets updated.
1956 *
1957 * FIXME: maybe we should consider checking that the reply matches request?
1958 */
1959static void
1960smb2_echo_callback(struct mid_q_entry *mid)
1961{
1962 struct TCP_Server_Info *server = mid->callback_data;
1963 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1964 unsigned int credits_received = 1;
1965
1966 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1967 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1968
Christopher Oo5fb4e282015-06-25 16:10:48 -07001969 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001970 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001971 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001972 add_credits(server, credits_received, CIFS_ECHO_OP);
1973}
1974
Pavel Shilovsky48f95262016-11-04 11:50:31 -07001975void smb2_reconnect_server(struct work_struct *work)
1976{
1977 struct TCP_Server_Info *server = container_of(work,
1978 struct TCP_Server_Info, reconnect.work);
1979 struct cifs_ses *ses;
1980 struct cifs_tcon *tcon, *tcon2;
1981 struct list_head tmp_list;
1982 int tcon_exist = false;
1983
1984 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
1985 mutex_lock(&server->reconnect_mutex);
1986
1987 INIT_LIST_HEAD(&tmp_list);
1988 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
1989
1990 spin_lock(&cifs_tcp_ses_lock);
1991 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1992 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1993 if (tcon->need_reconnect) {
1994 tcon->tc_count++;
1995 list_add_tail(&tcon->rlist, &tmp_list);
1996 tcon_exist = true;
1997 }
1998 }
1999 }
2000 /*
2001 * Get the reference to server struct to be sure that the last call of
2002 * cifs_put_tcon() in the loop below won't release the server pointer.
2003 */
2004 if (tcon_exist)
2005 server->srv_count++;
2006
2007 spin_unlock(&cifs_tcp_ses_lock);
2008
2009 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2010 smb2_reconnect(SMB2_ECHO, tcon);
2011 list_del_init(&tcon->rlist);
2012 cifs_put_tcon(tcon);
2013 }
2014
2015 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2016 mutex_unlock(&server->reconnect_mutex);
2017
2018 /* now we can safely release srv struct */
2019 if (tcon_exist)
2020 cifs_put_tcp_session(server, 1);
2021}
2022
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002023int
2024SMB2_echo(struct TCP_Server_Info *server)
2025{
2026 struct smb2_echo_req *req;
2027 int rc = 0;
2028 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002029 struct smb_rqst rqst = { .rq_iov = &iov,
2030 .rq_nvec = 1 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002031
Joe Perchesf96637b2013-05-04 22:12:25 -05002032 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002033
Steve French4fcd1812016-06-22 20:12:05 -05002034 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002035 /* No need to send echo on newly established connections */
2036 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2037 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002038 }
2039
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002040 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2041 if (rc)
2042 return rc;
2043
2044 req->hdr.CreditRequest = cpu_to_le16(1);
2045
2046 iov.iov_base = (char *)req;
2047 /* 4 for rfc1002 length field */
2048 iov.iov_len = get_rfc1002_length(req) + 4;
2049
Jeff Laytonfec344e2012-09-18 16:20:35 -07002050 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002051 CIFS_ECHO_OP);
2052 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002053 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002054
2055 cifs_small_buf_release(req);
2056 return rc;
2057}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002058
2059int
2060SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2061 u64 volatile_fid)
2062{
2063 struct smb2_flush_req *req;
2064 struct TCP_Server_Info *server;
2065 struct cifs_ses *ses = tcon->ses;
2066 struct kvec iov[1];
2067 int resp_buftype;
2068 int rc = 0;
2069
Joe Perchesf96637b2013-05-04 22:12:25 -05002070 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002071
2072 if (ses && (ses->server))
2073 server = ses->server;
2074 else
2075 return -EIO;
2076
2077 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2078 if (rc)
2079 return rc;
2080
2081 req->PersistentFileId = persistent_fid;
2082 req->VolatileFileId = volatile_fid;
2083
2084 iov[0].iov_base = (char *)req;
2085 /* 4 for rfc1002 length field */
2086 iov[0].iov_len = get_rfc1002_length(req) + 4;
2087
2088 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
2089
Steve Frenchdfebe402015-03-27 01:00:06 -05002090 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002091 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2092
2093 free_rsp_buf(resp_buftype, iov[0].iov_base);
2094 return rc;
2095}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002096
2097/*
2098 * To form a chain of read requests, any read requests after the first should
2099 * have the end_of_chain boolean set to true.
2100 */
2101static int
2102smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2103 unsigned int remaining_bytes, int request_type)
2104{
2105 int rc = -EACCES;
2106 struct smb2_read_req *req = NULL;
2107
2108 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2109 if (rc)
2110 return rc;
2111 if (io_parms->tcon->ses->server == NULL)
2112 return -ECONNABORTED;
2113
2114 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2115
2116 req->PersistentFileId = io_parms->persistent_fid;
2117 req->VolatileFileId = io_parms->volatile_fid;
2118 req->ReadChannelInfoOffset = 0; /* reserved */
2119 req->ReadChannelInfoLength = 0; /* reserved */
2120 req->Channel = 0; /* reserved */
2121 req->MinimumCount = 0;
2122 req->Length = cpu_to_le32(io_parms->length);
2123 req->Offset = cpu_to_le64(io_parms->offset);
2124
2125 if (request_type & CHAINED_REQUEST) {
2126 if (!(request_type & END_OF_CHAIN)) {
2127 /* 4 for rfc1002 length field */
2128 req->hdr.NextCommand =
2129 cpu_to_le32(get_rfc1002_length(req) + 4);
2130 } else /* END_OF_CHAIN */
2131 req->hdr.NextCommand = 0;
2132 if (request_type & RELATED_REQUEST) {
2133 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2134 /*
2135 * Related requests use info from previous read request
2136 * in chain.
2137 */
2138 req->hdr.SessionId = 0xFFFFFFFF;
2139 req->hdr.TreeId = 0xFFFFFFFF;
2140 req->PersistentFileId = 0xFFFFFFFF;
2141 req->VolatileFileId = 0xFFFFFFFF;
2142 }
2143 }
2144 if (remaining_bytes > io_parms->length)
2145 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2146 else
2147 req->RemainingBytes = 0;
2148
2149 iov[0].iov_base = (char *)req;
2150 /* 4 for rfc1002 length field */
2151 iov[0].iov_len = get_rfc1002_length(req) + 4;
2152 return rc;
2153}
2154
2155static void
2156smb2_readv_callback(struct mid_q_entry *mid)
2157{
2158 struct cifs_readdata *rdata = mid->callback_data;
2159 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2160 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07002161 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002162 unsigned int credits_received = 1;
Jeff Layton58195752012-09-19 06:22:34 -07002163 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Layton8321fec2012-09-19 06:22:32 -07002164 .rq_nvec = 1,
2165 .rq_pages = rdata->pages,
2166 .rq_npages = rdata->nr_pages,
2167 .rq_pagesz = rdata->pagesz,
2168 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002169
Joe Perchesf96637b2013-05-04 22:12:25 -05002170 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2171 __func__, mid->mid, mid->mid_state, rdata->result,
2172 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002173
2174 switch (mid->mid_state) {
2175 case MID_RESPONSE_RECEIVED:
2176 credits_received = le16_to_cpu(buf->CreditRequest);
2177 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04002178 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002179 int rc;
2180
Jeff Layton0b688cf2012-09-18 16:20:34 -07002181 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002182 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002183 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2184 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002185 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002186 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002187 task_io_account_read(rdata->got_bytes);
2188 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002189 break;
2190 case MID_REQUEST_SUBMITTED:
2191 case MID_RETRY_NEEDED:
2192 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002193 if (server->sign && rdata->got_bytes)
2194 /* reset bytes number since we can not check a sign */
2195 rdata->got_bytes = 0;
2196 /* FIXME: should this be counted toward the initiating task? */
2197 task_io_account_read(rdata->got_bytes);
2198 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002199 break;
2200 default:
2201 if (rdata->result != -ENODATA)
2202 rdata->result = -EIO;
2203 }
2204
2205 if (rdata->result)
2206 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2207
2208 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002209 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002210 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002211 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002212 add_credits(server, credits_received, 0);
2213}
2214
2215/* smb2_async_readv - send an async write, and set up mid to handle result */
2216int
2217smb2_async_readv(struct cifs_readdata *rdata)
2218{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002219 int rc, flags = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002220 struct smb2_hdr *buf;
2221 struct cifs_io_parms io_parms;
Jeff Layton58195752012-09-19 06:22:34 -07002222 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07002223 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002224 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002225
Joe Perchesf96637b2013-05-04 22:12:25 -05002226 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2227 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002228
2229 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2230 io_parms.offset = rdata->offset;
2231 io_parms.length = rdata->bytes;
2232 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2233 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2234 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002235
2236 server = io_parms.tcon->ses->server;
2237
Jeff Layton58195752012-09-19 06:22:34 -07002238 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002239 if (rc) {
2240 if (rc == -EAGAIN && rdata->credits) {
2241 /* credits was reset by reconnect */
2242 rdata->credits = 0;
2243 /* reduce in_flight value since we won't send the req */
2244 spin_lock(&server->req_lock);
2245 server->in_flight--;
2246 spin_unlock(&server->req_lock);
2247 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002248 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002249 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002250
Jeff Layton58195752012-09-19 06:22:34 -07002251 buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002252 /* 4 for rfc1002 length field */
Jeff Layton58195752012-09-19 06:22:34 -07002253 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002254
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002255 if (rdata->credits) {
2256 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2257 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002258 buf->CreditRequest = buf->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002259 spin_lock(&server->req_lock);
2260 server->credits += rdata->credits -
2261 le16_to_cpu(buf->CreditCharge);
2262 spin_unlock(&server->req_lock);
2263 wake_up(&server->request_q);
2264 flags = CIFS_HAS_CREDITS;
2265 }
2266
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002267 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002268 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002269 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002270 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002271 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002272 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002273 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2274 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002275
2276 cifs_small_buf_release(buf);
2277 return rc;
2278}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002279
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002280int
2281SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2282 unsigned int *nbytes, char **buf, int *buf_type)
2283{
2284 int resp_buftype, rc = -EACCES;
2285 struct smb2_read_rsp *rsp = NULL;
2286 struct kvec iov[1];
2287
2288 *nbytes = 0;
2289 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2290 if (rc)
2291 return rc;
2292
2293 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2294 &resp_buftype, CIFS_LOG_ERROR);
2295
2296 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2297
2298 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2299 free_rsp_buf(resp_buftype, iov[0].iov_base);
2300 return 0;
2301 }
2302
2303 if (rc) {
2304 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002305 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002306 } else {
2307 *nbytes = le32_to_cpu(rsp->DataLength);
2308 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2309 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002310 cifs_dbg(FYI, "bad length %d for count %d\n",
2311 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002312 rc = -EIO;
2313 *nbytes = 0;
2314 }
2315 }
2316
2317 if (*buf) {
Steve French373512e2015-12-18 13:05:30 -06002318 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002319 *nbytes);
2320 free_rsp_buf(resp_buftype, iov[0].iov_base);
2321 } else if (resp_buftype != CIFS_NO_BUFFER) {
2322 *buf = iov[0].iov_base;
2323 if (resp_buftype == CIFS_SMALL_BUFFER)
2324 *buf_type = CIFS_SMALL_BUFFER;
2325 else if (resp_buftype == CIFS_LARGE_BUFFER)
2326 *buf_type = CIFS_LARGE_BUFFER;
2327 }
2328 return rc;
2329}
2330
Pavel Shilovsky33319142012-09-18 16:20:29 -07002331/*
2332 * Check the mid_state and signature on received buffer (if any), and queue the
2333 * workqueue completion task.
2334 */
2335static void
2336smb2_writev_callback(struct mid_q_entry *mid)
2337{
2338 struct cifs_writedata *wdata = mid->callback_data;
2339 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002340 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002341 unsigned int written;
2342 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2343 unsigned int credits_received = 1;
2344
2345 switch (mid->mid_state) {
2346 case MID_RESPONSE_RECEIVED:
2347 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2348 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2349 if (wdata->result != 0)
2350 break;
2351
2352 written = le32_to_cpu(rsp->DataLength);
2353 /*
2354 * Mask off high 16 bits when bytes written as returned
2355 * by the server is greater than bytes requested by the
2356 * client. OS/2 servers are known to set incorrect
2357 * CountHigh values.
2358 */
2359 if (written > wdata->bytes)
2360 written &= 0xFFFF;
2361
2362 if (written < wdata->bytes)
2363 wdata->result = -ENOSPC;
2364 else
2365 wdata->bytes = written;
2366 break;
2367 case MID_REQUEST_SUBMITTED:
2368 case MID_RETRY_NEEDED:
2369 wdata->result = -EAGAIN;
2370 break;
2371 default:
2372 wdata->result = -EIO;
2373 break;
2374 }
2375
2376 if (wdata->result)
2377 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2378
2379 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002380 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002381 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002382 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002383 add_credits(tcon->ses->server, credits_received, 0);
2384}
2385
2386/* smb2_async_writev - send an async write, and set up mid to handle result */
2387int
Steve French4a5c80d2014-02-07 20:45:12 -06002388smb2_async_writev(struct cifs_writedata *wdata,
2389 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002390{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002391 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002392 struct smb2_write_req *req = NULL;
2393 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002394 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002395 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002396 struct smb_rqst rqst;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002397
2398 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002399 if (rc) {
2400 if (rc == -EAGAIN && wdata->credits) {
2401 /* credits was reset by reconnect */
2402 wdata->credits = 0;
2403 /* reduce in_flight value since we won't send the req */
2404 spin_lock(&server->req_lock);
2405 server->in_flight--;
2406 spin_unlock(&server->req_lock);
2407 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002408 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002409 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002410
Pavel Shilovsky33319142012-09-18 16:20:29 -07002411 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2412
2413 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2414 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2415 req->WriteChannelInfoOffset = 0;
2416 req->WriteChannelInfoLength = 0;
2417 req->Channel = 0;
2418 req->Offset = cpu_to_le64(wdata->offset);
2419 /* 4 for rfc1002 length field */
2420 req->DataOffset = cpu_to_le16(
2421 offsetof(struct smb2_write_req, Buffer) - 4);
2422 req->RemainingBytes = 0;
2423
2424 /* 4 for rfc1002 length field and 1 for Buffer */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002425 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2426 iov.iov_base = req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002427
Jeff Laytoneddb0792012-09-18 16:20:35 -07002428 rqst.rq_iov = &iov;
2429 rqst.rq_nvec = 1;
2430 rqst.rq_pages = wdata->pages;
2431 rqst.rq_npages = wdata->nr_pages;
2432 rqst.rq_pagesz = wdata->pagesz;
2433 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002434
Joe Perchesf96637b2013-05-04 22:12:25 -05002435 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2436 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002437
2438 req->Length = cpu_to_le32(wdata->bytes);
2439
2440 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2441
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002442 if (wdata->credits) {
2443 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2444 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002445 req->hdr.CreditRequest = req->hdr.CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002446 spin_lock(&server->req_lock);
2447 server->credits += wdata->credits -
2448 le16_to_cpu(req->hdr.CreditCharge);
2449 spin_unlock(&server->req_lock);
2450 wake_up(&server->request_q);
2451 flags = CIFS_HAS_CREDITS;
2452 }
2453
Pavel Shilovsky33319142012-09-18 16:20:29 -07002454 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002455 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2456 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002457
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002458 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002459 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002460 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2461 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002462
Pavel Shilovsky33319142012-09-18 16:20:29 -07002463async_writev_out:
2464 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002465 return rc;
2466}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002467
2468/*
2469 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2470 * The length field from io_parms must be at least 1 and indicates a number of
2471 * elements with data to write that begins with position 1 in iov array. All
2472 * data length is specified by count.
2473 */
2474int
2475SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2476 unsigned int *nbytes, struct kvec *iov, int n_vec)
2477{
2478 int rc = 0;
2479 struct smb2_write_req *req = NULL;
2480 struct smb2_write_rsp *rsp = NULL;
2481 int resp_buftype;
2482 *nbytes = 0;
2483
2484 if (n_vec < 1)
2485 return rc;
2486
2487 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2488 if (rc)
2489 return rc;
2490
2491 if (io_parms->tcon->ses->server == NULL)
2492 return -ECONNABORTED;
2493
2494 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2495
2496 req->PersistentFileId = io_parms->persistent_fid;
2497 req->VolatileFileId = io_parms->volatile_fid;
2498 req->WriteChannelInfoOffset = 0;
2499 req->WriteChannelInfoLength = 0;
2500 req->Channel = 0;
2501 req->Length = cpu_to_le32(io_parms->length);
2502 req->Offset = cpu_to_le64(io_parms->offset);
2503 /* 4 for rfc1002 length field */
2504 req->DataOffset = cpu_to_le16(
2505 offsetof(struct smb2_write_req, Buffer) - 4);
2506 req->RemainingBytes = 0;
2507
2508 iov[0].iov_base = (char *)req;
2509 /* 4 for rfc1002 length field and 1 for Buffer */
2510 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2511
2512 /* length of entire message including data to be written */
2513 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2514
2515 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2516 &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002517 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002518
2519 if (rc) {
2520 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002521 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002522 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002523 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002524
2525 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002526 return rc;
2527}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002528
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002529static unsigned int
2530num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2531{
2532 int len;
2533 unsigned int entrycount = 0;
2534 unsigned int next_offset = 0;
2535 FILE_DIRECTORY_INFO *entryptr;
2536
2537 if (bufstart == NULL)
2538 return 0;
2539
2540 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2541
2542 while (1) {
2543 entryptr = (FILE_DIRECTORY_INFO *)
2544 ((char *)entryptr + next_offset);
2545
2546 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002547 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002548 break;
2549 }
2550
2551 len = le32_to_cpu(entryptr->FileNameLength);
2552 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002553 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2554 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002555 break;
2556 }
2557
2558 *lastentry = (char *)entryptr;
2559 entrycount++;
2560
2561 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2562 if (!next_offset)
2563 break;
2564 }
2565
2566 return entrycount;
2567}
2568
2569/*
2570 * Readdir/FindFirst
2571 */
2572int
2573SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2574 u64 persistent_fid, u64 volatile_fid, int index,
2575 struct cifs_search_info *srch_inf)
2576{
2577 struct smb2_query_directory_req *req;
2578 struct smb2_query_directory_rsp *rsp = NULL;
2579 struct kvec iov[2];
2580 int rc = 0;
2581 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002582 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002583 unsigned char *bufptr;
2584 struct TCP_Server_Info *server;
2585 struct cifs_ses *ses = tcon->ses;
2586 __le16 asteriks = cpu_to_le16('*');
2587 char *end_of_smb;
2588 unsigned int output_size = CIFSMaxBufSize;
2589 size_t info_buf_size;
2590
2591 if (ses && (ses->server))
2592 server = ses->server;
2593 else
2594 return -EIO;
2595
2596 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2597 if (rc)
2598 return rc;
2599
2600 switch (srch_inf->info_level) {
2601 case SMB_FIND_FILE_DIRECTORY_INFO:
2602 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2603 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2604 break;
2605 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2606 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2607 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2608 break;
2609 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002610 cifs_dbg(VFS, "info level %u isn't supported\n",
2611 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002612 rc = -EINVAL;
2613 goto qdir_exit;
2614 }
2615
2616 req->FileIndex = cpu_to_le32(index);
2617 req->PersistentFileId = persistent_fid;
2618 req->VolatileFileId = volatile_fid;
2619
2620 len = 0x2;
2621 bufptr = req->Buffer;
2622 memcpy(bufptr, &asteriks, len);
2623
2624 req->FileNameOffset =
2625 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2626 req->FileNameLength = cpu_to_le16(len);
2627 /*
2628 * BB could be 30 bytes or so longer if we used SMB2 specific
2629 * buffer lengths, but this is safe and close enough.
2630 */
2631 output_size = min_t(unsigned int, output_size, server->maxBuf);
2632 output_size = min_t(unsigned int, output_size, 2 << 15);
2633 req->OutputBufferLength = cpu_to_le32(output_size);
2634
2635 iov[0].iov_base = (char *)req;
2636 /* 4 for RFC1001 length and 1 for Buffer */
2637 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2638
2639 iov[1].iov_base = (char *)(req->Buffer);
2640 iov[1].iov_len = len;
2641
2642 inc_rfc1001_len(req, len - 1 /* Buffer */);
2643
2644 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002645 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2646
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002647 if (rc) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002648 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2649 srch_inf->endOfSearch = true;
2650 rc = 0;
2651 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002652 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2653 goto qdir_exit;
2654 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002655
2656 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2657 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2658 info_buf_size);
2659 if (rc)
2660 goto qdir_exit;
2661
2662 srch_inf->unicode = true;
2663
2664 if (srch_inf->ntwrk_buf_start) {
2665 if (srch_inf->smallBuf)
2666 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2667 else
2668 cifs_buf_release(srch_inf->ntwrk_buf_start);
2669 }
2670 srch_inf->ntwrk_buf_start = (char *)rsp;
2671 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2672 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2673 /* 4 for rfc1002 length field */
2674 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2675 srch_inf->entries_in_buffer =
2676 num_entries(srch_inf->srch_entries_start, end_of_smb,
2677 &srch_inf->last_entry, info_buf_size);
2678 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002679 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2680 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2681 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002682 if (resp_buftype == CIFS_LARGE_BUFFER)
2683 srch_inf->smallBuf = false;
2684 else if (resp_buftype == CIFS_SMALL_BUFFER)
2685 srch_inf->smallBuf = true;
2686 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002687 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002688
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002689 return rc;
2690
2691qdir_exit:
2692 free_rsp_buf(resp_buftype, rsp);
2693 return rc;
2694}
2695
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002696static int
2697send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002698 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002699 unsigned int num, void **data, unsigned int *size)
2700{
2701 struct smb2_set_info_req *req;
2702 struct smb2_set_info_rsp *rsp = NULL;
2703 struct kvec *iov;
2704 int rc = 0;
2705 int resp_buftype;
2706 unsigned int i;
2707 struct TCP_Server_Info *server;
2708 struct cifs_ses *ses = tcon->ses;
2709
2710 if (ses && (ses->server))
2711 server = ses->server;
2712 else
2713 return -EIO;
2714
2715 if (!num)
2716 return -EINVAL;
2717
2718 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2719 if (!iov)
2720 return -ENOMEM;
2721
2722 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2723 if (rc) {
2724 kfree(iov);
2725 return rc;
2726 }
2727
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002728 req->hdr.ProcessId = cpu_to_le32(pid);
2729
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002730 req->InfoType = SMB2_O_INFO_FILE;
2731 req->FileInfoClass = info_class;
2732 req->PersistentFileId = persistent_fid;
2733 req->VolatileFileId = volatile_fid;
2734
2735 /* 4 for RFC1001 length and 1 for Buffer */
2736 req->BufferOffset =
2737 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2738 req->BufferLength = cpu_to_le32(*size);
2739
2740 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2741
2742 memcpy(req->Buffer, *data, *size);
2743
2744 iov[0].iov_base = (char *)req;
2745 /* 4 for RFC1001 length */
2746 iov[0].iov_len = get_rfc1002_length(req) + 4;
2747
2748 for (i = 1; i < num; i++) {
2749 inc_rfc1001_len(req, size[i]);
2750 le32_add_cpu(&req->BufferLength, size[i]);
2751 iov[i].iov_base = (char *)data[i];
2752 iov[i].iov_len = size[i];
2753 }
2754
2755 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2756 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2757
Steve French7d3fb242013-11-18 09:56:28 -06002758 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002759 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002760
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002761 free_rsp_buf(resp_buftype, rsp);
2762 kfree(iov);
2763 return rc;
2764}
2765
2766int
2767SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2768 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2769{
2770 struct smb2_file_rename_info info;
2771 void **data;
2772 unsigned int size[2];
2773 int rc;
2774 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2775
2776 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2777 if (!data)
2778 return -ENOMEM;
2779
2780 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2781 /* 0 = fail if target already exists */
2782 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2783 info.FileNameLength = cpu_to_le32(len);
2784
2785 data[0] = &info;
2786 size[0] = sizeof(struct smb2_file_rename_info);
2787
2788 data[1] = target_file;
2789 size[1] = len + 2 /* null */;
2790
2791 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002792 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2793 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002794 kfree(data);
2795 return rc;
2796}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002797
2798int
Steve French897fba12016-05-12 21:20:36 -05002799SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2800 u64 persistent_fid, u64 volatile_fid)
2801{
2802 __u8 delete_pending = 1;
2803 void *data;
2804 unsigned int size;
2805
2806 data = &delete_pending;
2807 size = 1; /* sizeof __u8 */
2808
2809 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2810 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2811 &size);
2812}
2813
2814int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002815SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2816 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2817{
2818 struct smb2_file_link_info info;
2819 void **data;
2820 unsigned int size[2];
2821 int rc;
2822 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2823
2824 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2825 if (!data)
2826 return -ENOMEM;
2827
2828 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2829 /* 0 = fail if link already exists */
2830 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2831 info.FileNameLength = cpu_to_le32(len);
2832
2833 data[0] = &info;
2834 size[0] = sizeof(struct smb2_file_link_info);
2835
2836 data[1] = target_file;
2837 size[1] = len + 2 /* null */;
2838
2839 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002840 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002841 kfree(data);
2842 return rc;
2843}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002844
2845int
2846SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002847 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002848{
2849 struct smb2_file_eof_info info;
2850 void *data;
2851 unsigned int size;
2852
2853 info.EndOfFile = *eof;
2854
2855 data = &info;
2856 size = sizeof(struct smb2_file_eof_info);
2857
Steve Frenchf29ebb42014-07-19 21:44:58 -05002858 if (is_falloc)
2859 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2860 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2861 else
2862 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2863 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002864}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002865
2866int
2867SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2868 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2869{
2870 unsigned int size;
2871 size = sizeof(FILE_BASIC_INFO);
2872 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2873 current->tgid, FILE_BASIC_INFORMATION, 1,
2874 (void **)&buf, &size);
2875}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002876
2877int
2878SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2879 const u64 persistent_fid, const u64 volatile_fid,
2880 __u8 oplock_level)
2881{
2882 int rc;
2883 struct smb2_oplock_break *req = NULL;
2884
Joe Perchesf96637b2013-05-04 22:12:25 -05002885 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002886 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2887
2888 if (rc)
2889 return rc;
2890
2891 req->VolatileFid = volatile_fid;
2892 req->PersistentFid = persistent_fid;
2893 req->OplockLevel = oplock_level;
2894 req->hdr.CreditRequest = cpu_to_le16(1);
2895
2896 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2897 /* SMB2 buffer freed by function above */
2898
2899 if (rc) {
2900 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002901 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002902 }
2903
2904 return rc;
2905}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002906
2907static void
2908copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2909 struct kstatfs *kst)
2910{
2911 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2912 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2913 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2914 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2915 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2916 return;
2917}
2918
2919static int
2920build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2921 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2922{
2923 int rc;
2924 struct smb2_query_info_req *req;
2925
Joe Perchesf96637b2013-05-04 22:12:25 -05002926 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002927
2928 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2929 return -EIO;
2930
2931 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2932 if (rc)
2933 return rc;
2934
2935 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2936 req->FileInfoClass = level;
2937 req->PersistentFileId = persistent_fid;
2938 req->VolatileFileId = volatile_fid;
2939 /* 4 for rfc1002 length field and 1 for pad */
2940 req->InputBufferOffset =
2941 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2942 req->OutputBufferLength = cpu_to_le32(
2943 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2944
2945 iov->iov_base = (char *)req;
2946 /* 4 for rfc1002 length field */
2947 iov->iov_len = get_rfc1002_length(req) + 4;
2948 return 0;
2949}
2950
2951int
2952SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2953 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2954{
2955 struct smb2_query_info_rsp *rsp = NULL;
2956 struct kvec iov;
2957 int rc = 0;
2958 int resp_buftype;
2959 struct cifs_ses *ses = tcon->ses;
2960 struct smb2_fs_full_size_info *info = NULL;
2961
2962 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2963 sizeof(struct smb2_fs_full_size_info),
2964 persistent_fid, volatile_fid);
2965 if (rc)
2966 return rc;
2967
2968 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2969 if (rc) {
2970 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05002971 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002972 }
2973 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2974
2975 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2976 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2977 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2978 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2979 sizeof(struct smb2_fs_full_size_info));
2980 if (!rc)
2981 copy_fs_info_to_kstatfs(info, fsdata);
2982
Steve French34f62642013-10-09 02:07:00 -05002983qfsinf_exit:
2984 free_rsp_buf(resp_buftype, iov.iov_base);
2985 return rc;
2986}
2987
2988int
2989SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05002990 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05002991{
2992 struct smb2_query_info_rsp *rsp = NULL;
2993 struct kvec iov;
2994 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05002995 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05002996 struct cifs_ses *ses = tcon->ses;
2997 unsigned int rsp_len, offset;
2998
Steven French21671142013-10-09 13:36:35 -05002999 if (level == FS_DEVICE_INFORMATION) {
3000 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3001 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3002 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3003 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3004 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003005 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3006 max_len = sizeof(struct smb3_fs_ss_info);
3007 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003008 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003009 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003010 return -EINVAL;
3011 }
3012
3013 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003014 persistent_fid, volatile_fid);
3015 if (rc)
3016 return rc;
3017
3018 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3019 if (rc) {
3020 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3021 goto qfsattr_exit;
3022 }
3023 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3024
3025 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3026 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003027 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3028 if (rc)
3029 goto qfsattr_exit;
3030
3031 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003032 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3033 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003034 rsp_len, max_len));
3035 else if (level == FS_DEVICE_INFORMATION)
3036 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3037 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003038 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3039 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3040 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3041 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3042 tcon->perf_sector_size =
3043 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3044 }
Steve French34f62642013-10-09 02:07:00 -05003045
3046qfsattr_exit:
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003047 free_rsp_buf(resp_buftype, iov.iov_base);
3048 return rc;
3049}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003050
3051int
3052smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3053 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3054 const __u32 num_lock, struct smb2_lock_element *buf)
3055{
3056 int rc = 0;
3057 struct smb2_lock_req *req = NULL;
3058 struct kvec iov[2];
3059 int resp_buf_type;
3060 unsigned int count;
3061
Joe Perchesf96637b2013-05-04 22:12:25 -05003062 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003063
3064 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3065 if (rc)
3066 return rc;
3067
3068 req->hdr.ProcessId = cpu_to_le32(pid);
3069 req->LockCount = cpu_to_le16(num_lock);
3070
3071 req->PersistentFileId = persist_fid;
3072 req->VolatileFileId = volatile_fid;
3073
3074 count = num_lock * sizeof(struct smb2_lock_element);
3075 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3076
3077 iov[0].iov_base = (char *)req;
3078 /* 4 for rfc1002 length field and count for all locks */
3079 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3080 iov[1].iov_base = (char *)buf;
3081 iov[1].iov_len = count;
3082
3083 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3084 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
3085 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003086 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003087 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3088 }
3089
3090 return rc;
3091}
3092
3093int
3094SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3095 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3096 const __u64 length, const __u64 offset, const __u32 lock_flags,
3097 const bool wait)
3098{
3099 struct smb2_lock_element lock;
3100
3101 lock.Offset = cpu_to_le64(offset);
3102 lock.Length = cpu_to_le64(length);
3103 lock.Flags = cpu_to_le32(lock_flags);
3104 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3105 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3106
3107 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3108}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003109
3110int
3111SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3112 __u8 *lease_key, const __le32 lease_state)
3113{
3114 int rc;
3115 struct smb2_lease_ack *req = NULL;
3116
Joe Perchesf96637b2013-05-04 22:12:25 -05003117 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003118 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3119
3120 if (rc)
3121 return rc;
3122
3123 req->hdr.CreditRequest = cpu_to_le16(1);
3124 req->StructureSize = cpu_to_le16(36);
3125 inc_rfc1001_len(req, 12);
3126
3127 memcpy(req->LeaseKey, lease_key, 16);
3128 req->LeaseState = lease_state;
3129
3130 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3131 /* SMB2 buffer freed by function above */
3132
3133 if (rc) {
3134 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003135 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003136 }
3137
3138 return rc;
3139}