blob: d1a90371b649ccf02f95f958fde55ea26372e91e [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
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070082smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040083 const struct cifs_tcon *tcon)
84{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070085 shdr->ProtocolId = SMB2_PROTO_NUMBER;
86 shdr->StructureSize = cpu_to_le16(64);
87 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +010088 if (tcon && tcon->ses && tcon->ses->server) {
89 struct TCP_Server_Info *server = tcon->ses->server;
90
91 spin_lock(&server->req_lock);
92 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -050093 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070094 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +010095 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070096 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -050097 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +010098 server->credits, 2));
99 spin_unlock(&server->req_lock);
100 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700101 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100102 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700103 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400104
105 if (!tcon)
106 goto out;
107
Steve French2b80d042013-06-23 18:43:37 -0500108 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
109 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500110 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500111 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700112 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500113 /* else CreditCharge MBZ */
114
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700115 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400116 /* Uid is not converted */
117 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700118 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500119
120 /*
121 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
122 * to pass the path on the Open SMB prefixed by \\server\share.
123 * Not sure when we would need to do the augmented path (if ever) and
124 * setting this flag breaks the SMB2 open operation since it is
125 * illegal to send an empty path name (without \\server\share prefix)
126 * when the DFS flag is set in the SMB open header. We could
127 * consider setting the flag on all operations other than open
128 * but it is safer to net set it for now.
129 */
130/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700131 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500132
Jeff Layton38d77c52013-05-26 07:01:00 -0400133 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700134 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400135out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400136 return;
137}
138
139static int
140smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
141{
142 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400143 struct nls_table *nls_codepage;
144 struct cifs_ses *ses;
145 struct TCP_Server_Info *server;
146
147 /*
148 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
149 * check for tcp and smb session status done differently
150 * for those three - in the calling routine.
151 */
152 if (tcon == NULL)
153 return rc;
154
155 if (smb2_command == SMB2_TREE_CONNECT)
156 return rc;
157
158 if (tcon->tidStatus == CifsExiting) {
159 /*
160 * only tree disconnect, open, and write,
161 * (and ulogoff which does not have tcon)
162 * are allowed as we start force umount.
163 */
164 if ((smb2_command != SMB2_WRITE) &&
165 (smb2_command != SMB2_CREATE) &&
166 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500167 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
168 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400169 return -ENODEV;
170 }
171 }
172 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
173 (!tcon->ses->server))
174 return -EIO;
175
176 ses = tcon->ses;
177 server = ses->server;
178
179 /*
180 * Give demultiplex thread up to 10 seconds to reconnect, should be
181 * greater than cifs socket timeout which is 7 seconds
182 */
183 while (server->tcpStatus == CifsNeedReconnect) {
184 /*
185 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
186 * here since they are implicitly done when session drops.
187 */
188 switch (smb2_command) {
189 /*
190 * BB Should we keep oplock break and add flush to exceptions?
191 */
192 case SMB2_TREE_DISCONNECT:
193 case SMB2_CANCEL:
194 case SMB2_CLOSE:
195 case SMB2_OPLOCK_BREAK:
196 return -EAGAIN;
197 }
198
199 wait_event_interruptible_timeout(server->response_q,
200 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
201
202 /* are we still trying to reconnect? */
203 if (server->tcpStatus != CifsNeedReconnect)
204 break;
205
206 /*
207 * on "soft" mounts we wait once. Hard mounts keep
208 * retrying until process is killed or server comes
209 * back on-line
210 */
211 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500212 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400213 return -EHOSTDOWN;
214 }
215 }
216
217 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
218 return rc;
219
220 nls_codepage = load_nls_default();
221
222 /*
223 * need to prevent multiple threads trying to simultaneously reconnect
224 * the same SMB session
225 */
226 mutex_lock(&tcon->ses->session_mutex);
227 rc = cifs_negotiate_protocol(0, tcon->ses);
228 if (!rc && tcon->ses->need_reconnect)
229 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
230
231 if (rc || !tcon->need_reconnect) {
232 mutex_unlock(&tcon->ses->session_mutex);
233 goto out;
234 }
235
236 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800237 if (tcon->use_persistent)
238 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500239
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400240 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
241 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500242
Joe Perchesf96637b2013-05-04 22:12:25 -0500243 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400244 if (rc)
245 goto out;
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800246
247 if (smb2_command != SMB2_INTERNAL_CMD)
248 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
249
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400250 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400251out:
252 /*
253 * Check if handle based operation so we know whether we can continue
254 * or not without returning to caller to reset file handle.
255 */
256 /*
257 * BB Is flush done by server on drop of tcp session? Should we special
258 * case it and skip above?
259 */
260 switch (smb2_command) {
261 case SMB2_FLUSH:
262 case SMB2_READ:
263 case SMB2_WRITE:
264 case SMB2_LOCK:
265 case SMB2_IOCTL:
266 case SMB2_QUERY_DIRECTORY:
267 case SMB2_CHANGE_NOTIFY:
268 case SMB2_QUERY_INFO:
269 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800270 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400271 }
272 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400273 return rc;
274}
275
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700276static void
277fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
278 unsigned int *total_len)
279{
280 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
281 /* lookup word count ie StructureSize from table */
282 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
283
284 /*
285 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
286 * largest operations (Create)
287 */
288 memset(buf, 0, 256);
289
290 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
291 spdu->StructureSize2 = cpu_to_le16(parmsize);
292
293 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
294}
295
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800296/* init request without RFC1001 length at the beginning */
297static int
298smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
299 void **request_buf, unsigned int *total_len)
300{
301 int rc;
302 struct smb2_sync_hdr *shdr;
303
304 rc = smb2_reconnect(smb2_command, tcon);
305 if (rc)
306 return rc;
307
308 /* BB eventually switch this to SMB2 specific small buf size */
309 *request_buf = cifs_small_buf_get();
310 if (*request_buf == NULL) {
311 /* BB should we add a retry in here if not a writepage? */
312 return -ENOMEM;
313 }
314
315 shdr = (struct smb2_sync_hdr *)(*request_buf);
316
317 fill_small_buf(smb2_command, tcon, shdr, total_len);
318
319 if (tcon != NULL) {
320#ifdef CONFIG_CIFS_STATS2
321 uint16_t com_code = le16_to_cpu(smb2_command);
322
323 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
324#endif
325 cifs_stats_inc(&tcon->num_smbs_sent);
326 }
327
328 return rc;
329}
330
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400331/*
332 * Allocate and return pointer to an SMB request hdr, and set basic
333 * SMB information in the SMB header. If the return code is zero, this
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800334 * function must have filled in request_buf pointer. The returned buffer
335 * has RFC1001 length at the beginning.
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400336 */
337static int
338small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
339 void **request_buf)
340{
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700341 int rc;
342 unsigned int total_len;
343 struct smb2_pdu *pdu;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400344
345 rc = smb2_reconnect(smb2_command, tcon);
346 if (rc)
347 return rc;
348
349 /* BB eventually switch this to SMB2 specific small buf size */
350 *request_buf = cifs_small_buf_get();
351 if (*request_buf == NULL) {
352 /* BB should we add a retry in here if not a writepage? */
353 return -ENOMEM;
354 }
355
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700356 pdu = (struct smb2_pdu *)(*request_buf);
357
358 fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len);
359
360 /* Note this is only network field converted to big endian */
361 pdu->hdr.smb2_buf_length = cpu_to_be32(total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400362
363 if (tcon != NULL) {
364#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400365 uint16_t com_code = le16_to_cpu(smb2_command);
366 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400367#endif
368 cifs_stats_inc(&tcon->num_smbs_sent);
369 }
370
371 return rc;
372}
373
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500374#ifdef CONFIG_CIFS_SMB311
375/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
376#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
377
378
379#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
380#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
381
382static void
383build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
384{
385 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
386 pneg_ctxt->DataLength = cpu_to_le16(38);
387 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
388 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
389 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
390 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
391}
392
393static void
394build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
395{
396 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
397 pneg_ctxt->DataLength = cpu_to_le16(6);
398 pneg_ctxt->CipherCount = cpu_to_le16(2);
399 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
400 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
401}
402
403static void
404assemble_neg_contexts(struct smb2_negotiate_req *req)
405{
406
407 /* +4 is to account for the RFC1001 len field */
408 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
409
410 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
411 /* Add 2 to size to round to 8 byte boundary */
412 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
413 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
414 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
415 req->NegotiateContextCount = cpu_to_le16(2);
416 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
417 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
418}
419#else
420static void assemble_neg_contexts(struct smb2_negotiate_req *req)
421{
422 return;
423}
424#endif /* SMB311 */
425
426
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400427/*
428 *
429 * SMB2 Worker functions follow:
430 *
431 * The general structure of the worker functions is:
432 * 1) Call smb2_init (assembles SMB2 header)
433 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
434 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
435 * 4) Decode SMB2 command specific fields in the fixed length area
436 * 5) Decode variable length data area (if any for this SMB2 command type)
437 * 6) Call free smb buffer
438 * 7) return
439 *
440 */
441
442int
443SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
444{
445 struct smb2_negotiate_req *req;
446 struct smb2_negotiate_rsp *rsp;
447 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700448 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400449 int rc = 0;
450 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400451 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400452 int blob_offset, blob_length;
453 char *security_blob;
454 int flags = CIFS_NEG_OP;
455
Joe Perchesf96637b2013-05-04 22:12:25 -0500456 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400457
Jeff Layton3534b852013-05-24 07:41:01 -0400458 if (!server) {
459 WARN(1, "%s: server is NULL!\n", __func__);
460 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400461 }
462
463 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
464 if (rc)
465 return rc;
466
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700467 req->hdr.sync_hdr.SessionId = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400468
Steve Frenche4aa25e2012-10-01 12:26:22 -0500469 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400470
Steve Frenche4aa25e2012-10-01 12:26:22 -0500471 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
472 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400473
474 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400475 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500476 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400477 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500478 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400479 else
480 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400481
Steve Frenche4aa25e2012-10-01 12:26:22 -0500482 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400483
Steve French3c5f9be12014-05-13 13:37:45 -0700484 /* ClientGUID must be zero for SMB2.02 dialect */
485 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
486 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500487 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700488 memcpy(req->ClientGUID, server->client_guid,
489 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500490 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
491 assemble_neg_contexts(req);
492 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400493 iov[0].iov_base = (char *)req;
494 /* 4 for rfc1002 length field */
495 iov[0].iov_len = get_rfc1002_length(req) + 4;
496
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700497 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
498 cifs_small_buf_release(req);
499 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400500 /*
501 * No tcon so can't do
502 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
503 */
504 if (rc != 0)
505 goto neg_exit;
506
Joe Perchesf96637b2013-05-04 22:12:25 -0500507 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400508
Steve Frenche4aa25e2012-10-01 12:26:22 -0500509 /* BB we may eventually want to match the negotiated vs. requested
510 dialect, even though we are only requesting one at a time */
511 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500512 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500513 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500514 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500515 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500516 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500517 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
518 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600519#ifdef CONFIG_CIFS_SMB311
520 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
521 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
522#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400523 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500524 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500525 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400526 rc = -EIO;
527 goto neg_exit;
528 }
529 server->dialect = le16_to_cpu(rsp->DialectRevision);
530
Jeff Laytone598d1d82013-05-26 07:00:59 -0400531 /* SMB2 only has an extended negflavor */
532 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400533 /* set it to the maximum buffer size value we can send with 1 credit */
534 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
535 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400536 server->max_read = le32_to_cpu(rsp->MaxReadSize);
537 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
538 /* BB Do we need to validate the SecurityMode? */
539 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
540 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400541 /* Internal types */
542 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400543
544 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
545 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500546 /*
547 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
548 * for us will be
549 * ses->sectype = RawNTLMSSP;
550 * but for time being this is our only auth choice so doesn't matter.
551 * We just found a server which sets blob length to zero expecting raw.
552 */
553 if (blob_length == 0)
554 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700555
Jeff Layton38d77c52013-05-26 07:01:00 -0400556 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400557 if (rc)
558 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500559 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500560 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500561 if (rc == 1)
562 rc = 0;
563 else if (rc == 0)
564 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400565 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400566neg_exit:
567 free_rsp_buf(resp_buftype, rsp);
568 return rc;
569}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400570
Steve Frenchff1c0382013-11-19 23:44:46 -0600571int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
572{
573 int rc = 0;
574 struct validate_negotiate_info_req vneg_inbuf;
575 struct validate_negotiate_info_rsp *pneg_rsp;
576 u32 rsplen;
577
578 cifs_dbg(FYI, "validate negotiate\n");
579
580 /*
581 * validation ioctl must be signed, so no point sending this if we
582 * can not sign it. We could eventually change this to selectively
583 * sign just this, the first and only signed request on a connection.
584 * This is good enough for now since a user who wants better security
585 * would also enable signing on the mount. Having validation of
586 * negotiate info for signed connections helps reduce attack vectors
587 */
588 if (tcon->ses->server->sign == false)
589 return 0; /* validation requires signing */
590
591 vneg_inbuf.Capabilities =
592 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100593 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
594 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600595
596 if (tcon->ses->sign)
597 vneg_inbuf.SecurityMode =
598 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
599 else if (global_secflags & CIFSSEC_MAY_SIGN)
600 vneg_inbuf.SecurityMode =
601 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
602 else
603 vneg_inbuf.SecurityMode = 0;
604
605 vneg_inbuf.DialectCount = cpu_to_le16(1);
606 vneg_inbuf.Dialects[0] =
607 cpu_to_le16(tcon->ses->server->vals->protocol_id);
608
609 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
610 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
611 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
612 (char **)&pneg_rsp, &rsplen);
613
614 if (rc != 0) {
615 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
616 return -EIO;
617 }
618
619 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
620 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
621 return -EIO;
622 }
623
624 /* check validate negotiate info response matches what we got earlier */
625 if (pneg_rsp->Dialect !=
626 cpu_to_le16(tcon->ses->server->vals->protocol_id))
627 goto vneg_out;
628
629 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
630 goto vneg_out;
631
632 /* do not validate server guid because not saved at negprot time yet */
633
634 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
635 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
636 goto vneg_out;
637
638 /* validate negotiate successful */
639 cifs_dbg(FYI, "validate negotiate info successful\n");
640 return 0;
641
642vneg_out:
643 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
644 return -EIO;
645}
646
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100647struct SMB2_sess_data {
648 unsigned int xid;
649 struct cifs_ses *ses;
650 struct nls_table *nls_cp;
651 void (*func)(struct SMB2_sess_data *);
652 int result;
653 u64 previous_session;
654
655 /* we will send the SMB in three pieces:
656 * a fixed length beginning part, an optional
657 * SPNEGO blob (which can be zero length), and a
658 * last part which will include the strings
659 * and rest of bcc area. This allows us to avoid
660 * a large buffer 17K allocation
661 */
662 int buf0_type;
663 struct kvec iov[2];
664};
665
666static int
667SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
668{
669 int rc;
670 struct cifs_ses *ses = sess_data->ses;
671 struct smb2_sess_setup_req *req;
672 struct TCP_Server_Info *server = ses->server;
673
674 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
675 if (rc)
676 return rc;
677
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700678 /* First session, not a reauthenticate */
679 req->hdr.sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100680
681 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
682 req->PreviousSessionId = sess_data->previous_session;
683
684 req->Flags = 0; /* MBZ */
685 /* to enable echos and oplocks */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700686 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100687
688 /* only one of SMB2 signing flags may be set in SMB2 request */
689 if (server->sign)
690 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
691 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
692 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
693 else
694 req->SecurityMode = 0;
695
696 req->Capabilities = 0;
697 req->Channel = 0; /* MBZ */
698
699 sess_data->iov[0].iov_base = (char *)req;
700 /* 4 for rfc1002 length field and 1 for pad */
701 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
702 /*
703 * This variable will be used to clear the buffer
704 * allocated above in case of any error in the calling function.
705 */
706 sess_data->buf0_type = CIFS_SMALL_BUFFER;
707
708 return 0;
709}
710
711static void
712SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
713{
714 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
715 sess_data->buf0_type = CIFS_NO_BUFFER;
716}
717
718static int
719SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
720{
721 int rc;
722 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700723 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100724
725 /* Testing shows that buffer offset must be at location of Buffer[0] */
726 req->SecurityBufferOffset =
727 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
728 1 /* pad */ - 4 /* rfc1001 len */);
729 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
730
731 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
732
733 /* BB add code to build os and lm fields */
734
735 rc = SendReceive2(sess_data->xid, sess_data->ses,
736 sess_data->iov, 2,
737 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700738 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
739 cifs_small_buf_release(sess_data->iov[0].iov_base);
740 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100741
742 return rc;
743}
744
745static int
746SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
747{
748 int rc = 0;
749 struct cifs_ses *ses = sess_data->ses;
750
751 mutex_lock(&ses->server->srv_mutex);
752 if (ses->server->sign && ses->server->ops->generate_signingkey) {
753 rc = ses->server->ops->generate_signingkey(ses);
754 kfree(ses->auth_key.response);
755 ses->auth_key.response = NULL;
756 if (rc) {
757 cifs_dbg(FYI,
758 "SMB3 session key generation failed\n");
759 mutex_unlock(&ses->server->srv_mutex);
760 goto keygen_exit;
761 }
762 }
763 if (!ses->server->session_estab) {
764 ses->server->sequence_number = 0x2;
765 ses->server->session_estab = true;
766 }
767 mutex_unlock(&ses->server->srv_mutex);
768
769 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
770 spin_lock(&GlobalMid_Lock);
771 ses->status = CifsGood;
772 ses->need_reconnect = false;
773 spin_unlock(&GlobalMid_Lock);
774
775keygen_exit:
776 if (!ses->server->sign) {
777 kfree(ses->auth_key.response);
778 ses->auth_key.response = NULL;
779 }
780 return rc;
781}
782
783#ifdef CONFIG_CIFS_UPCALL
784static void
785SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
786{
787 int rc;
788 struct cifs_ses *ses = sess_data->ses;
789 struct cifs_spnego_msg *msg;
790 struct key *spnego_key = NULL;
791 struct smb2_sess_setup_rsp *rsp = NULL;
792
793 rc = SMB2_sess_alloc_buffer(sess_data);
794 if (rc)
795 goto out;
796
797 spnego_key = cifs_get_spnego_key(ses);
798 if (IS_ERR(spnego_key)) {
799 rc = PTR_ERR(spnego_key);
800 spnego_key = NULL;
801 goto out;
802 }
803
804 msg = spnego_key->payload.data[0];
805 /*
806 * check version field to make sure that cifs.upcall is
807 * sending us a response in an expected form
808 */
809 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
810 cifs_dbg(VFS,
811 "bad cifs.upcall version. Expected %d got %d",
812 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
813 rc = -EKEYREJECTED;
814 goto out_put_spnego_key;
815 }
816
817 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
818 GFP_KERNEL);
819 if (!ses->auth_key.response) {
820 cifs_dbg(VFS,
821 "Kerberos can't allocate (%u bytes) memory",
822 msg->sesskey_len);
823 rc = -ENOMEM;
824 goto out_put_spnego_key;
825 }
826 ses->auth_key.len = msg->sesskey_len;
827
828 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
829 sess_data->iov[1].iov_len = msg->secblob_len;
830
831 rc = SMB2_sess_sendreceive(sess_data);
832 if (rc)
833 goto out_put_spnego_key;
834
835 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700836 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100837
838 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
839 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
840 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
841
842 rc = SMB2_sess_establish_session(sess_data);
843out_put_spnego_key:
844 key_invalidate(spnego_key);
845 key_put(spnego_key);
846out:
847 sess_data->result = rc;
848 sess_data->func = NULL;
849 SMB2_sess_free_buffer(sess_data);
850}
851#else
852static void
853SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
854{
855 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
856 sess_data->result = -EOPNOTSUPP;
857 sess_data->func = NULL;
858}
859#endif
860
Sachin Prabhu166cea42016-10-07 19:11:22 +0100861static void
862SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
863
864static void
865SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
866{
867 int rc;
868 struct cifs_ses *ses = sess_data->ses;
869 struct smb2_sess_setup_rsp *rsp = NULL;
870 char *ntlmssp_blob = NULL;
871 bool use_spnego = false; /* else use raw ntlmssp */
872 u16 blob_length = 0;
873
874 /*
875 * If memory allocation is successful, caller of this function
876 * frees it.
877 */
878 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
879 if (!ses->ntlmssp) {
880 rc = -ENOMEM;
881 goto out_err;
882 }
883 ses->ntlmssp->sesskey_per_smbsess = true;
884
885 rc = SMB2_sess_alloc_buffer(sess_data);
886 if (rc)
887 goto out_err;
888
889 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
890 GFP_KERNEL);
891 if (ntlmssp_blob == NULL) {
892 rc = -ENOMEM;
893 goto out;
894 }
895
896 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
897 if (use_spnego) {
898 /* BB eventually need to add this */
899 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
900 rc = -EOPNOTSUPP;
901 goto out;
902 } else {
903 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
904 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
905 }
906 sess_data->iov[1].iov_base = ntlmssp_blob;
907 sess_data->iov[1].iov_len = blob_length;
908
909 rc = SMB2_sess_sendreceive(sess_data);
910 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
911
912 /* If true, rc here is expected and not an error */
913 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700914 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +0100915 rc = 0;
916
917 if (rc)
918 goto out;
919
920 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
921 le16_to_cpu(rsp->SecurityBufferOffset)) {
922 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
923 le16_to_cpu(rsp->SecurityBufferOffset));
924 rc = -EIO;
925 goto out;
926 }
927 rc = decode_ntlmssp_challenge(rsp->Buffer,
928 le16_to_cpu(rsp->SecurityBufferLength), ses);
929 if (rc)
930 goto out;
931
932 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
933
934
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700935 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100936 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
937 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
938 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
939
940out:
941 kfree(ntlmssp_blob);
942 SMB2_sess_free_buffer(sess_data);
943 if (!rc) {
944 sess_data->result = 0;
945 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
946 return;
947 }
948out_err:
949 kfree(ses->ntlmssp);
950 ses->ntlmssp = NULL;
951 sess_data->result = rc;
952 sess_data->func = NULL;
953}
954
955static void
956SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
957{
958 int rc;
959 struct cifs_ses *ses = sess_data->ses;
960 struct smb2_sess_setup_req *req;
961 struct smb2_sess_setup_rsp *rsp = NULL;
962 unsigned char *ntlmssp_blob = NULL;
963 bool use_spnego = false; /* else use raw ntlmssp */
964 u16 blob_length = 0;
965
966 rc = SMB2_sess_alloc_buffer(sess_data);
967 if (rc)
968 goto out;
969
970 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700971 req->hdr.sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100972
973 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
974 sess_data->nls_cp);
975 if (rc) {
976 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
977 goto out;
978 }
979
980 if (use_spnego) {
981 /* BB eventually need to add this */
982 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
983 rc = -EOPNOTSUPP;
984 goto out;
985 }
986 sess_data->iov[1].iov_base = ntlmssp_blob;
987 sess_data->iov[1].iov_len = blob_length;
988
989 rc = SMB2_sess_sendreceive(sess_data);
990 if (rc)
991 goto out;
992
993 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
994
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700995 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100996 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
997 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
998 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
999
1000 rc = SMB2_sess_establish_session(sess_data);
1001out:
1002 kfree(ntlmssp_blob);
1003 SMB2_sess_free_buffer(sess_data);
1004 kfree(ses->ntlmssp);
1005 ses->ntlmssp = NULL;
1006 sess_data->result = rc;
1007 sess_data->func = NULL;
1008}
1009
1010static int
1011SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1012{
1013 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
1014 ses->sectype = RawNTLMSSP;
1015
1016 switch (ses->sectype) {
1017 case Kerberos:
1018 sess_data->func = SMB2_auth_kerberos;
1019 break;
1020 case RawNTLMSSP:
1021 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1022 break;
1023 default:
1024 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
1025 return -EOPNOTSUPP;
1026 }
1027
1028 return 0;
1029}
1030
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001031int
1032SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1033 const struct nls_table *nls_cp)
1034{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001035 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001036 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001037 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001038
Joe Perchesf96637b2013-05-04 22:12:25 -05001039 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001040
Jeff Layton3534b852013-05-24 07:41:01 -04001041 if (!server) {
1042 WARN(1, "%s: server is NULL!\n", __func__);
1043 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001044 }
1045
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001046 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1047 if (!sess_data)
1048 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001049
1050 rc = SMB2_select_sec(ses, sess_data);
1051 if (rc)
1052 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001053 sess_data->xid = xid;
1054 sess_data->ses = ses;
1055 sess_data->buf0_type = CIFS_NO_BUFFER;
1056 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001057
Sachin Prabhu166cea42016-10-07 19:11:22 +01001058 while (sess_data->func)
1059 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001060
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001061 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001062out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001063 kfree(sess_data);
1064 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001065}
1066
1067int
1068SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1069{
1070 struct smb2_logoff_req *req; /* response is also trivial struct */
1071 int rc = 0;
1072 struct TCP_Server_Info *server;
1073
Joe Perchesf96637b2013-05-04 22:12:25 -05001074 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001075
1076 if (ses && (ses->server))
1077 server = ses->server;
1078 else
1079 return -EIO;
1080
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001081 /* no need to send SMB logoff if uid already closed due to reconnect */
1082 if (ses->need_reconnect)
1083 goto smb2_session_already_dead;
1084
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001085 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1086 if (rc)
1087 return rc;
1088
1089 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001090 req->hdr.sync_hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -04001091 if (server->sign)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001092 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001093
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001094 rc = SendReceiveNoRsp(xid, ses, (char *) req, 0);
1095 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001096 /*
1097 * No tcon so can't do
1098 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1099 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001100
1101smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001102 return rc;
1103}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001104
1105static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1106{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001107 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001108}
1109
1110#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1111
Steve Frenchde9f68d2013-11-15 11:26:24 -06001112/* These are similar values to what Windows uses */
1113static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1114{
1115 tcon->max_chunks = 256;
1116 tcon->max_bytes_chunk = 1048576;
1117 tcon->max_bytes_copy = 16777216;
1118}
1119
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001120int
1121SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1122 struct cifs_tcon *tcon, const struct nls_table *cp)
1123{
1124 struct smb2_tree_connect_req *req;
1125 struct smb2_tree_connect_rsp *rsp = NULL;
1126 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001127 struct kvec rsp_iov;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001128 int rc = 0;
1129 int resp_buftype;
1130 int unc_path_len;
1131 struct TCP_Server_Info *server;
1132 __le16 *unc_path = NULL;
1133
Joe Perchesf96637b2013-05-04 22:12:25 -05001134 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001135
1136 if ((ses->server) && tree)
1137 server = ses->server;
1138 else
1139 return -EIO;
1140
1141 if (tcon && tcon->bad_network_name)
1142 return -ENOENT;
1143
Steve Frenchff9f84b2015-09-26 09:48:58 -05001144 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -05001145 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1146 cifs_dbg(VFS, "encryption requested but no server support");
1147 return -EOPNOTSUPP;
1148 }
1149
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001150 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1151 if (unc_path == NULL)
1152 return -ENOMEM;
1153
1154 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1155 unc_path_len *= 2;
1156 if (unc_path_len < 2) {
1157 kfree(unc_path);
1158 return -EINVAL;
1159 }
1160
1161 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1162 if (rc) {
1163 kfree(unc_path);
1164 return rc;
1165 }
1166
1167 if (tcon == NULL) {
1168 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001169 req->hdr.sync_hdr.SessionId = ses->Suid;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001170 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1171 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1172 }
1173
1174 iov[0].iov_base = (char *)req;
1175 /* 4 for rfc1002 length field and 1 for pad */
1176 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1177
1178 /* Testing shows that buffer offset must be at location of Buffer[0] */
1179 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1180 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1181 req->PathLength = cpu_to_le16(unc_path_len - 2);
1182 iov[1].iov_base = unc_path;
1183 iov[1].iov_len = unc_path_len;
1184
1185 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1186
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001187 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0, &rsp_iov);
1188 cifs_small_buf_release(req);
1189 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001190
1191 if (rc != 0) {
1192 if (tcon) {
1193 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1194 tcon->need_reconnect = true;
1195 }
1196 goto tcon_error_exit;
1197 }
1198
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001199 if (tcon == NULL) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001200 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001201 goto tcon_exit;
1202 }
1203
1204 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001205 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001206 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1207 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001208 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001209 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1210 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001211 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001212 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001213 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001214 rc = -EOPNOTSUPP;
1215 goto tcon_error_exit;
1216 }
1217
1218 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001219 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001220 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1221 tcon->tidStatus = CifsGood;
1222 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001223 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001224 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001225
1226 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1227 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001228 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001229 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001230 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1231 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001232 if (tcon->ses->server->ops->validate_negotiate)
1233 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001234tcon_exit:
1235 free_rsp_buf(resp_buftype, rsp);
1236 kfree(unc_path);
1237 return rc;
1238
1239tcon_error_exit:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001240 if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001241 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Steve French18f39e72014-08-17 00:22:24 -05001242 if (tcon)
1243 tcon->bad_network_name = true;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001244 }
1245 goto tcon_exit;
1246}
1247
1248int
1249SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1250{
1251 struct smb2_tree_disconnect_req *req; /* response is trivial */
1252 int rc = 0;
1253 struct TCP_Server_Info *server;
1254 struct cifs_ses *ses = tcon->ses;
1255
Joe Perchesf96637b2013-05-04 22:12:25 -05001256 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001257
1258 if (ses && (ses->server))
1259 server = ses->server;
1260 else
1261 return -EIO;
1262
1263 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1264 return 0;
1265
1266 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1267 if (rc)
1268 return rc;
1269
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001270 rc = SendReceiveNoRsp(xid, ses, (char *)req, 0);
1271 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001272 if (rc)
1273 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1274
1275 return rc;
1276}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001277
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001278
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001279static struct create_durable *
1280create_durable_buf(void)
1281{
1282 struct create_durable *buf;
1283
1284 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1285 if (!buf)
1286 return NULL;
1287
1288 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001289 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001290 buf->ccontext.DataLength = cpu_to_le32(16);
1291 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1292 (struct create_durable, Name));
1293 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001294 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001295 buf->Name[0] = 'D';
1296 buf->Name[1] = 'H';
1297 buf->Name[2] = 'n';
1298 buf->Name[3] = 'Q';
1299 return buf;
1300}
1301
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001302static struct create_durable *
1303create_reconnect_durable_buf(struct cifs_fid *fid)
1304{
1305 struct create_durable *buf;
1306
1307 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1308 if (!buf)
1309 return NULL;
1310
1311 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1312 (struct create_durable, Data));
1313 buf->ccontext.DataLength = cpu_to_le32(16);
1314 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1315 (struct create_durable, Name));
1316 buf->ccontext.NameLength = cpu_to_le16(4);
1317 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1318 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001319 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001320 buf->Name[0] = 'D';
1321 buf->Name[1] = 'H';
1322 buf->Name[2] = 'n';
1323 buf->Name[3] = 'C';
1324 return buf;
1325}
1326
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001327static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001328parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1329 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001330{
1331 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001332 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001333 unsigned int next;
1334 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001335 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001336
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001337 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001338 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001339 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001340 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001341 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001342 if (le16_to_cpu(cc->NameLength) == 4 &&
1343 strncmp(name, "RqLs", 4) == 0)
1344 return server->ops->parse_lease_buf(cc, epoch);
1345
1346 next = le32_to_cpu(cc->Next);
1347 if (!next)
1348 break;
1349 remaining -= next;
1350 cc = (struct create_context *)((char *)cc + next);
1351 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001352
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001353 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001354}
1355
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001356static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001357add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1358 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001359{
1360 struct smb2_create_req *req = iov[0].iov_base;
1361 unsigned int num = *num_iovec;
1362
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001363 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001364 if (iov[num].iov_base == NULL)
1365 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001366 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001367 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1368 if (!req->CreateContextsOffset)
1369 req->CreateContextsOffset = cpu_to_le32(
1370 sizeof(struct smb2_create_req) - 4 +
1371 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001372 le32_add_cpu(&req->CreateContextsLength,
1373 server->vals->create_lease_size);
1374 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001375 *num_iovec = num + 1;
1376 return 0;
1377}
1378
Steve Frenchb56eae42015-11-03 09:26:27 -06001379static struct create_durable_v2 *
1380create_durable_v2_buf(struct cifs_fid *pfid)
1381{
1382 struct create_durable_v2 *buf;
1383
1384 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1385 if (!buf)
1386 return NULL;
1387
1388 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1389 (struct create_durable_v2, dcontext));
1390 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1391 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1392 (struct create_durable_v2, Name));
1393 buf->ccontext.NameLength = cpu_to_le16(4);
1394
1395 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1396 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001397 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001398 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1399
1400 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1401 buf->Name[0] = 'D';
1402 buf->Name[1] = 'H';
1403 buf->Name[2] = '2';
1404 buf->Name[3] = 'Q';
1405 return buf;
1406}
1407
1408static struct create_durable_handle_reconnect_v2 *
1409create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1410{
1411 struct create_durable_handle_reconnect_v2 *buf;
1412
1413 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1414 GFP_KERNEL);
1415 if (!buf)
1416 return NULL;
1417
1418 buf->ccontext.DataOffset =
1419 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1420 dcontext));
1421 buf->ccontext.DataLength =
1422 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1423 buf->ccontext.NameOffset =
1424 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1425 Name));
1426 buf->ccontext.NameLength = cpu_to_le16(4);
1427
1428 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1429 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1430 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1431 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1432
1433 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1434 buf->Name[0] = 'D';
1435 buf->Name[1] = 'H';
1436 buf->Name[2] = '2';
1437 buf->Name[3] = 'C';
1438 return buf;
1439}
1440
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001441static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001442add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001443 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001444{
1445 struct smb2_create_req *req = iov[0].iov_base;
1446 unsigned int num = *num_iovec;
1447
Steve Frenchb56eae42015-11-03 09:26:27 -06001448 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1449 if (iov[num].iov_base == NULL)
1450 return -ENOMEM;
1451 iov[num].iov_len = sizeof(struct create_durable_v2);
1452 if (!req->CreateContextsOffset)
1453 req->CreateContextsOffset =
1454 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1455 iov[1].iov_len);
1456 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1457 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1458 *num_iovec = num + 1;
1459 return 0;
1460}
1461
1462static int
1463add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1464 struct cifs_open_parms *oparms)
1465{
1466 struct smb2_create_req *req = iov[0].iov_base;
1467 unsigned int num = *num_iovec;
1468
1469 /* indicate that we don't need to relock the file */
1470 oparms->reconnect = false;
1471
1472 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1473 if (iov[num].iov_base == NULL)
1474 return -ENOMEM;
1475 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1476 if (!req->CreateContextsOffset)
1477 req->CreateContextsOffset =
1478 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1479 iov[1].iov_len);
1480 le32_add_cpu(&req->CreateContextsLength,
1481 sizeof(struct create_durable_handle_reconnect_v2));
1482 inc_rfc1001_len(&req->hdr,
1483 sizeof(struct create_durable_handle_reconnect_v2));
1484 *num_iovec = num + 1;
1485 return 0;
1486}
1487
1488static int
1489add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1490 struct cifs_open_parms *oparms, bool use_persistent)
1491{
1492 struct smb2_create_req *req = iov[0].iov_base;
1493 unsigned int num = *num_iovec;
1494
1495 if (use_persistent) {
1496 if (oparms->reconnect)
1497 return add_durable_reconnect_v2_context(iov, num_iovec,
1498 oparms);
1499 else
1500 return add_durable_v2_context(iov, num_iovec, oparms);
1501 }
1502
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001503 if (oparms->reconnect) {
1504 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1505 /* indicate that we don't need to relock the file */
1506 oparms->reconnect = false;
1507 } else
1508 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001509 if (iov[num].iov_base == NULL)
1510 return -ENOMEM;
1511 iov[num].iov_len = sizeof(struct create_durable);
1512 if (!req->CreateContextsOffset)
1513 req->CreateContextsOffset =
1514 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1515 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001516 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001517 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1518 *num_iovec = num + 1;
1519 return 0;
1520}
1521
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001522int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001523SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001524 __u8 *oplock, struct smb2_file_all_info *buf,
1525 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001526{
1527 struct smb2_create_req *req;
1528 struct smb2_create_rsp *rsp;
1529 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001530 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001531 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001532 struct kvec iov[4];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001533 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001534 int resp_buftype;
1535 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001536 __le16 *copy_path = NULL;
1537 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001538 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001539 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001540 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001541 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001542
Joe Perchesf96637b2013-05-04 22:12:25 -05001543 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001544
1545 if (ses && (ses->server))
1546 server = ses->server;
1547 else
1548 return -EIO;
1549
1550 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1551 if (rc)
1552 return rc;
1553
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001554 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001555 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001556 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1557 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001558
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001559 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001560 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001561 /* File attributes ignored on open (used in create though) */
1562 req->FileAttributes = cpu_to_le32(file_attributes);
1563 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001564 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1565 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001566 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001567 /* do not count rfc1001 len field */
1568 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001569
1570 iov[0].iov_base = (char *)req;
1571 /* 4 for rfc1002 length field */
1572 iov[0].iov_len = get_rfc1002_length(req) + 4;
1573
1574 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001575 req->NameLength = cpu_to_le16(uni_path_len - 2);
1576 /* -1 since last byte is buf[0] which is sent below (path) */
1577 iov[0].iov_len--;
1578 if (uni_path_len % 8 != 0) {
1579 copy_size = uni_path_len / 8 * 8;
1580 if (copy_size < uni_path_len)
1581 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001582
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001583 copy_path = kzalloc(copy_size, GFP_KERNEL);
1584 if (!copy_path)
1585 return -ENOMEM;
1586 memcpy((char *)copy_path, (const char *)path,
1587 uni_path_len);
1588 uni_path_len = copy_size;
1589 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001590 }
1591
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001592 iov[1].iov_len = uni_path_len;
1593 iov[1].iov_base = path;
1594 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1595 inc_rfc1001_len(req, uni_path_len - 1);
1596
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001597 if (!server->oplocks)
1598 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1599
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001600 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001601 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1602 req->RequestedOplockLevel = *oplock;
1603 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001604 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001605 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001606 cifs_small_buf_release(req);
1607 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001608 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001609 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001610 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001611 }
1612
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001613 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1614 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001615 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001616 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001617 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001618 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001619 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001620 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001621
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001622 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001623 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001624 if (rc) {
1625 cifs_small_buf_release(req);
1626 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001627 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001628 return rc;
1629 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001630 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001631 }
1632
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001633 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, 0, &rsp_iov);
1634 cifs_small_buf_release(req);
1635 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001636
1637 if (rc != 0) {
1638 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001639 if (err_buf)
1640 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1641 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001642 goto creat_exit;
1643 }
1644
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001645 oparms->fid->persistent_fid = rsp->PersistentFileId;
1646 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001647
1648 if (buf) {
1649 memcpy(buf, &rsp->CreationTime, 32);
1650 buf->AllocationSize = rsp->AllocationSize;
1651 buf->EndOfFile = rsp->EndofFile;
1652 buf->Attributes = rsp->FileAttributes;
1653 buf->NumberOfLinks = cpu_to_le32(1);
1654 buf->DeletePending = 0;
1655 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001656
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001657 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001658 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001659 else
1660 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001661creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001662 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001663 kfree(lc_buf);
1664 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001665 free_rsp_buf(resp_buftype, rsp);
1666 return rc;
1667}
1668
Steve French4a72daf2013-06-25 00:20:49 -05001669/*
1670 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1671 */
1672int
1673SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1674 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1675 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1676{
1677 struct smb2_ioctl_req *req;
1678 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001679 struct smb2_sync_hdr *shdr;
Steve French4a72daf2013-06-25 00:20:49 -05001680 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001681 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001682 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001683 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001684 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001685 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001686 int rc = 0;
1687
1688 cifs_dbg(FYI, "SMB2 IOCTL\n");
1689
Steve French3d1a3742014-08-11 21:05:25 -05001690 if (out_data != NULL)
1691 *out_data = NULL;
1692
Steve French4a72daf2013-06-25 00:20:49 -05001693 /* zero out returned data len, in case of error */
1694 if (plen)
1695 *plen = 0;
1696
Steve French8e353102015-03-26 19:47:02 -05001697 if (tcon)
1698 ses = tcon->ses;
1699 else
1700 return -EIO;
1701
Steve French4a72daf2013-06-25 00:20:49 -05001702 if (ses && (ses->server))
1703 server = ses->server;
1704 else
1705 return -EIO;
1706
1707 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1708 if (rc)
1709 return rc;
1710
1711 req->CtlCode = cpu_to_le32(opcode);
1712 req->PersistentFileId = persistent_fid;
1713 req->VolatileFileId = volatile_fid;
1714
1715 if (indatalen) {
1716 req->InputCount = cpu_to_le32(indatalen);
1717 /* do not set InputOffset if no input data */
1718 req->InputOffset =
1719 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1720 iov[1].iov_base = in_data;
1721 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001722 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001723 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001724 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001725
1726 req->OutputOffset = 0;
1727 req->OutputCount = 0; /* MBZ */
1728
1729 /*
1730 * Could increase MaxOutputResponse, but that would require more
1731 * than one credit. Windows typically sets this smaller, but for some
1732 * ioctls it may be useful to allow server to send more. No point
1733 * limiting what the server can send as long as fits in one credit
1734 */
1735 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1736
1737 if (is_fsctl)
1738 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1739 else
1740 req->Flags = 0;
1741
1742 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001743
Steve French7ff8d452013-10-14 00:44:19 -05001744 /*
1745 * If no input data, the size of ioctl struct in
1746 * protocol spec still includes a 1 byte data buffer,
1747 * but if input data passed to ioctl, we do not
1748 * want to double count this, so we do not send
1749 * the dummy one byte of data in iovec[0] if sending
1750 * input data (in iovec[1]). We also must add 4 bytes
1751 * in first iovec to allow for rfc1002 length field.
1752 */
1753
1754 if (indatalen) {
1755 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1756 inc_rfc1001_len(req, indatalen - 1);
1757 } else
1758 iov[0].iov_len = get_rfc1002_length(req) + 4;
1759
Steve French4a72daf2013-06-25 00:20:49 -05001760
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001761 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, 0, &rsp_iov);
1762 cifs_small_buf_release(req);
1763 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05001764
Steve French9bf0c9c2013-11-16 18:05:28 -06001765 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001766 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001767 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001768 } else if (rc == -EINVAL) {
1769 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1770 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001771 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001772 goto ioctl_exit;
1773 }
Steve French4a72daf2013-06-25 00:20:49 -05001774 }
1775
1776 /* check if caller wants to look at return data or just return rc */
1777 if ((plen == NULL) || (out_data == NULL))
1778 goto ioctl_exit;
1779
1780 *plen = le32_to_cpu(rsp->OutputCount);
1781
1782 /* We check for obvious errors in the output buffer length and offset */
1783 if (*plen == 0)
1784 goto ioctl_exit; /* server returned no data */
1785 else if (*plen > 0xFF00) {
1786 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1787 *plen = 0;
1788 rc = -EIO;
1789 goto ioctl_exit;
1790 }
1791
1792 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1793 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1794 le32_to_cpu(rsp->OutputOffset));
1795 *plen = 0;
1796 rc = -EIO;
1797 goto ioctl_exit;
1798 }
1799
1800 *out_data = kmalloc(*plen, GFP_KERNEL);
1801 if (*out_data == NULL) {
1802 rc = -ENOMEM;
1803 goto ioctl_exit;
1804 }
1805
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001806 shdr = get_sync_hdr(rsp);
1807 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05001808ioctl_exit:
1809 free_rsp_buf(resp_buftype, rsp);
1810 return rc;
1811}
1812
Steve French64a5cfa2013-10-14 15:31:32 -05001813/*
1814 * Individual callers to ioctl worker function follow
1815 */
1816
1817int
1818SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1819 u64 persistent_fid, u64 volatile_fid)
1820{
1821 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001822 struct compress_ioctl fsctl_input;
1823 char *ret_data = NULL;
1824
1825 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001826 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001827
1828 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1829 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1830 (char *)&fsctl_input /* data input */,
1831 2 /* in data len */, &ret_data /* out data */, NULL);
1832
1833 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001834
1835 return rc;
1836}
1837
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001838int
1839SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1840 u64 persistent_fid, u64 volatile_fid)
1841{
1842 struct smb2_close_req *req;
1843 struct smb2_close_rsp *rsp;
1844 struct TCP_Server_Info *server;
1845 struct cifs_ses *ses = tcon->ses;
1846 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001847 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001848 int resp_buftype;
1849 int rc = 0;
1850
Joe Perchesf96637b2013-05-04 22:12:25 -05001851 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001852
1853 if (ses && (ses->server))
1854 server = ses->server;
1855 else
1856 return -EIO;
1857
1858 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1859 if (rc)
1860 return rc;
1861
1862 req->PersistentFileId = persistent_fid;
1863 req->VolatileFileId = volatile_fid;
1864
1865 iov[0].iov_base = (char *)req;
1866 /* 4 for rfc1002 length field */
1867 iov[0].iov_len = get_rfc1002_length(req) + 4;
1868
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001869 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0, &rsp_iov);
1870 cifs_small_buf_release(req);
1871 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001872
1873 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001874 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001875 goto close_exit;
1876 }
1877
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001878 /* BB FIXME - decode close response, update inode for caching */
1879
1880close_exit:
1881 free_rsp_buf(resp_buftype, rsp);
1882 return rc;
1883}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001884
1885static int
1886validate_buf(unsigned int offset, unsigned int buffer_length,
1887 struct smb2_hdr *hdr, unsigned int min_buf_size)
1888
1889{
1890 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1891 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1892 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1893 char *end_of_buf = begin_of_buf + buffer_length;
1894
1895
1896 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001897 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1898 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001899 return -EINVAL;
1900 }
1901
1902 /* check if beyond RFC1001 maximum length */
1903 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001904 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1905 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001906 return -EINVAL;
1907 }
1908
1909 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001910 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001911 return -EINVAL;
1912 }
1913
1914 return 0;
1915}
1916
1917/*
1918 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1919 * Caller must free buffer.
1920 */
1921static int
1922validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1923 struct smb2_hdr *hdr, unsigned int minbufsize,
1924 char *data)
1925
1926{
1927 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1928 int rc;
1929
1930 if (!data)
1931 return -EINVAL;
1932
1933 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1934 if (rc)
1935 return rc;
1936
1937 memcpy(data, begin_of_buf, buffer_length);
1938
1939 return 0;
1940}
1941
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001942static int
1943query_info(const unsigned int xid, struct cifs_tcon *tcon,
1944 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1945 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001946{
1947 struct smb2_query_info_req *req;
1948 struct smb2_query_info_rsp *rsp = NULL;
1949 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001950 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001951 int rc = 0;
1952 int resp_buftype;
1953 struct TCP_Server_Info *server;
1954 struct cifs_ses *ses = tcon->ses;
1955
Joe Perchesf96637b2013-05-04 22:12:25 -05001956 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001957
1958 if (ses && (ses->server))
1959 server = ses->server;
1960 else
1961 return -EIO;
1962
1963 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1964 if (rc)
1965 return rc;
1966
1967 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001968 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001969 req->PersistentFileId = persistent_fid;
1970 req->VolatileFileId = volatile_fid;
1971 /* 4 for rfc1002 length field and 1 for Buffer */
1972 req->InputBufferOffset =
1973 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001974 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001975
1976 iov[0].iov_base = (char *)req;
1977 /* 4 for rfc1002 length field */
1978 iov[0].iov_len = get_rfc1002_length(req) + 4;
1979
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001980 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0, &rsp_iov);
1981 cifs_small_buf_release(req);
1982 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001983
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001984 if (rc) {
1985 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1986 goto qinf_exit;
1987 }
1988
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001989 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1990 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001991 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001992
1993qinf_exit:
1994 free_rsp_buf(resp_buftype, rsp);
1995 return rc;
1996}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001997
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001998int
1999SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2000 u64 persistent_fid, u64 volatile_fid,
2001 struct smb2_file_all_info *data)
2002{
2003 return query_info(xid, tcon, persistent_fid, volatile_fid,
2004 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002005 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002006 sizeof(struct smb2_file_all_info), data);
2007}
2008
2009int
2010SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2011 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2012{
2013 return query_info(xid, tcon, persistent_fid, volatile_fid,
2014 FILE_INTERNAL_INFORMATION,
2015 sizeof(struct smb2_file_internal_info),
2016 sizeof(struct smb2_file_internal_info), uniqueid);
2017}
2018
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002019/*
2020 * This is a no-op for now. We're not really interested in the reply, but
2021 * rather in the fact that the server sent one and that server->lstrp
2022 * gets updated.
2023 *
2024 * FIXME: maybe we should consider checking that the reply matches request?
2025 */
2026static void
2027smb2_echo_callback(struct mid_q_entry *mid)
2028{
2029 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002030 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002031 unsigned int credits_received = 1;
2032
2033 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002034 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002035
Christopher Oo5fb4e282015-06-25 16:10:48 -07002036 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002037 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002038 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002039 add_credits(server, credits_received, CIFS_ECHO_OP);
2040}
2041
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002042void smb2_reconnect_server(struct work_struct *work)
2043{
2044 struct TCP_Server_Info *server = container_of(work,
2045 struct TCP_Server_Info, reconnect.work);
2046 struct cifs_ses *ses;
2047 struct cifs_tcon *tcon, *tcon2;
2048 struct list_head tmp_list;
2049 int tcon_exist = false;
2050
2051 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2052 mutex_lock(&server->reconnect_mutex);
2053
2054 INIT_LIST_HEAD(&tmp_list);
2055 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2056
2057 spin_lock(&cifs_tcp_ses_lock);
2058 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2059 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002060 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002061 tcon->tc_count++;
2062 list_add_tail(&tcon->rlist, &tmp_list);
2063 tcon_exist = true;
2064 }
2065 }
2066 }
2067 /*
2068 * Get the reference to server struct to be sure that the last call of
2069 * cifs_put_tcon() in the loop below won't release the server pointer.
2070 */
2071 if (tcon_exist)
2072 server->srv_count++;
2073
2074 spin_unlock(&cifs_tcp_ses_lock);
2075
2076 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002077 if (!smb2_reconnect(SMB2_INTERNAL_CMD, tcon))
2078 cifs_reopen_persistent_handles(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002079 list_del_init(&tcon->rlist);
2080 cifs_put_tcon(tcon);
2081 }
2082
2083 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2084 mutex_unlock(&server->reconnect_mutex);
2085
2086 /* now we can safely release srv struct */
2087 if (tcon_exist)
2088 cifs_put_tcp_session(server, 1);
2089}
2090
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002091int
2092SMB2_echo(struct TCP_Server_Info *server)
2093{
2094 struct smb2_echo_req *req;
2095 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002096 struct kvec iov[2];
2097 struct smb_rqst rqst = { .rq_iov = iov,
2098 .rq_nvec = 2 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002099
Joe Perchesf96637b2013-05-04 22:12:25 -05002100 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002101
Steve French4fcd1812016-06-22 20:12:05 -05002102 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002103 /* No need to send echo on newly established connections */
2104 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2105 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002106 }
2107
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002108 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2109 if (rc)
2110 return rc;
2111
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002112 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002113
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002114 /* 4 for rfc1002 length field */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002115 iov[0].iov_len = 4;
2116 iov[0].iov_base = (char *)req;
2117 iov[1].iov_len = get_rfc1002_length(req);
2118 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002119
Jeff Laytonfec344e2012-09-18 16:20:35 -07002120 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002121 CIFS_ECHO_OP);
2122 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002123 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002124
2125 cifs_small_buf_release(req);
2126 return rc;
2127}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002128
2129int
2130SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2131 u64 volatile_fid)
2132{
2133 struct smb2_flush_req *req;
2134 struct TCP_Server_Info *server;
2135 struct cifs_ses *ses = tcon->ses;
2136 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002137 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002138 int resp_buftype;
2139 int rc = 0;
2140
Joe Perchesf96637b2013-05-04 22:12:25 -05002141 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002142
2143 if (ses && (ses->server))
2144 server = ses->server;
2145 else
2146 return -EIO;
2147
2148 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2149 if (rc)
2150 return rc;
2151
2152 req->PersistentFileId = persistent_fid;
2153 req->VolatileFileId = volatile_fid;
2154
2155 iov[0].iov_base = (char *)req;
2156 /* 4 for rfc1002 length field */
2157 iov[0].iov_len = get_rfc1002_length(req) + 4;
2158
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002159 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0, &rsp_iov);
2160 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002161
Steve Frenchdfebe402015-03-27 01:00:06 -05002162 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002163 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2164
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002165 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002166 return rc;
2167}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002168
2169/*
2170 * To form a chain of read requests, any read requests after the first should
2171 * have the end_of_chain boolean set to true.
2172 */
2173static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002174smb2_new_read_req(void **buf, unsigned int *total_len,
2175 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2176 int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002177{
2178 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002179 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002180 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002181
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002182 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2183 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002184 if (rc)
2185 return rc;
2186 if (io_parms->tcon->ses->server == NULL)
2187 return -ECONNABORTED;
2188
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002189 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002190 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002191
2192 req->PersistentFileId = io_parms->persistent_fid;
2193 req->VolatileFileId = io_parms->volatile_fid;
2194 req->ReadChannelInfoOffset = 0; /* reserved */
2195 req->ReadChannelInfoLength = 0; /* reserved */
2196 req->Channel = 0; /* reserved */
2197 req->MinimumCount = 0;
2198 req->Length = cpu_to_le32(io_parms->length);
2199 req->Offset = cpu_to_le64(io_parms->offset);
2200
2201 if (request_type & CHAINED_REQUEST) {
2202 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002203 /* next 8-byte aligned request */
2204 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2205 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002206 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002207 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002208 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002209 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002210 /*
2211 * Related requests use info from previous read request
2212 * in chain.
2213 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002214 shdr->SessionId = 0xFFFFFFFF;
2215 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002216 req->PersistentFileId = 0xFFFFFFFF;
2217 req->VolatileFileId = 0xFFFFFFFF;
2218 }
2219 }
2220 if (remaining_bytes > io_parms->length)
2221 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2222 else
2223 req->RemainingBytes = 0;
2224
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002225 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002226 return rc;
2227}
2228
2229static void
2230smb2_readv_callback(struct mid_q_entry *mid)
2231{
2232 struct cifs_readdata *rdata = mid->callback_data;
2233 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2234 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002235 struct smb2_sync_hdr *shdr =
2236 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002237 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002238 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2239 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002240 .rq_pages = rdata->pages,
2241 .rq_npages = rdata->nr_pages,
2242 .rq_pagesz = rdata->pagesz,
2243 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002244
Joe Perchesf96637b2013-05-04 22:12:25 -05002245 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2246 __func__, mid->mid, mid->mid_state, rdata->result,
2247 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002248
2249 switch (mid->mid_state) {
2250 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002251 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002252 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04002253 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002254 int rc;
2255
Jeff Layton0b688cf2012-09-18 16:20:34 -07002256 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002257 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002258 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2259 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002260 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002261 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002262 task_io_account_read(rdata->got_bytes);
2263 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002264 break;
2265 case MID_REQUEST_SUBMITTED:
2266 case MID_RETRY_NEEDED:
2267 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002268 if (server->sign && rdata->got_bytes)
2269 /* reset bytes number since we can not check a sign */
2270 rdata->got_bytes = 0;
2271 /* FIXME: should this be counted toward the initiating task? */
2272 task_io_account_read(rdata->got_bytes);
2273 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002274 break;
2275 default:
2276 if (rdata->result != -ENODATA)
2277 rdata->result = -EIO;
2278 }
2279
2280 if (rdata->result)
2281 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2282
2283 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002284 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002285 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002286 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002287 add_credits(server, credits_received, 0);
2288}
2289
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002290/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002291int
2292smb2_async_readv(struct cifs_readdata *rdata)
2293{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002294 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002295 char *buf;
2296 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002297 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002298 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2299 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002300 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002301 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002302 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002303
Joe Perchesf96637b2013-05-04 22:12:25 -05002304 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2305 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002306
2307 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2308 io_parms.offset = rdata->offset;
2309 io_parms.length = rdata->bytes;
2310 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2311 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2312 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002313
2314 server = io_parms.tcon->ses->server;
2315
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002316 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002317 if (rc) {
2318 if (rc == -EAGAIN && rdata->credits) {
2319 /* credits was reset by reconnect */
2320 rdata->credits = 0;
2321 /* reduce in_flight value since we won't send the req */
2322 spin_lock(&server->req_lock);
2323 server->in_flight--;
2324 spin_unlock(&server->req_lock);
2325 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002326 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002327 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002328
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002329 req_len = cpu_to_be32(total_len);
2330
2331 rdata->iov[0].iov_base = &req_len;
2332 rdata->iov[0].iov_len = sizeof(__be32);
2333 rdata->iov[1].iov_base = buf;
2334 rdata->iov[1].iov_len = total_len;
2335
2336 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002337
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002338 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002339 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002340 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002341 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002342 spin_lock(&server->req_lock);
2343 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002344 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002345 spin_unlock(&server->req_lock);
2346 wake_up(&server->request_q);
2347 flags = CIFS_HAS_CREDITS;
2348 }
2349
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002350 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002351 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002352 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002353 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002354 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002355 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002356 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2357 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002358
2359 cifs_small_buf_release(buf);
2360 return rc;
2361}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002362
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002363int
2364SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2365 unsigned int *nbytes, char **buf, int *buf_type)
2366{
2367 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002368 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002369 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002370 struct smb2_sync_hdr *shdr;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002371 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002372 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002373 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002374 __be32 req_len;
2375 struct smb_rqst rqst = { .rq_iov = iov,
2376 .rq_nvec = 2 };
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002377
2378 *nbytes = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002379 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002380 if (rc)
2381 return rc;
2382
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002383 req_len = cpu_to_be32(total_len);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002384
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002385 iov[0].iov_base = &req_len;
2386 iov[0].iov_len = sizeof(__be32);
2387 iov[1].iov_base = req;
2388 iov[1].iov_len = total_len;
2389
2390 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst, &resp_buftype,
2391 CIFS_LOG_ERROR, &rsp_iov);
2392 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002393
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002394 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002395 shdr = get_sync_hdr(rsp);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002396
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002397 if (shdr->Status == STATUS_END_OF_FILE) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002398 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002399 return 0;
2400 }
2401
2402 if (rc) {
2403 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002404 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002405 } else {
2406 *nbytes = le32_to_cpu(rsp->DataLength);
2407 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2408 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002409 cifs_dbg(FYI, "bad length %d for count %d\n",
2410 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002411 rc = -EIO;
2412 *nbytes = 0;
2413 }
2414 }
2415
2416 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002417 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002418 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002419 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002420 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002421 if (resp_buftype == CIFS_SMALL_BUFFER)
2422 *buf_type = CIFS_SMALL_BUFFER;
2423 else if (resp_buftype == CIFS_LARGE_BUFFER)
2424 *buf_type = CIFS_LARGE_BUFFER;
2425 }
2426 return rc;
2427}
2428
Pavel Shilovsky33319142012-09-18 16:20:29 -07002429/*
2430 * Check the mid_state and signature on received buffer (if any), and queue the
2431 * workqueue completion task.
2432 */
2433static void
2434smb2_writev_callback(struct mid_q_entry *mid)
2435{
2436 struct cifs_writedata *wdata = mid->callback_data;
2437 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002438 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002439 unsigned int written;
2440 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2441 unsigned int credits_received = 1;
2442
2443 switch (mid->mid_state) {
2444 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002445 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002446 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2447 if (wdata->result != 0)
2448 break;
2449
2450 written = le32_to_cpu(rsp->DataLength);
2451 /*
2452 * Mask off high 16 bits when bytes written as returned
2453 * by the server is greater than bytes requested by the
2454 * client. OS/2 servers are known to set incorrect
2455 * CountHigh values.
2456 */
2457 if (written > wdata->bytes)
2458 written &= 0xFFFF;
2459
2460 if (written < wdata->bytes)
2461 wdata->result = -ENOSPC;
2462 else
2463 wdata->bytes = written;
2464 break;
2465 case MID_REQUEST_SUBMITTED:
2466 case MID_RETRY_NEEDED:
2467 wdata->result = -EAGAIN;
2468 break;
2469 default:
2470 wdata->result = -EIO;
2471 break;
2472 }
2473
2474 if (wdata->result)
2475 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2476
2477 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002478 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002479 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002480 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002481 add_credits(tcon->ses->server, credits_received, 0);
2482}
2483
2484/* smb2_async_writev - send an async write, and set up mid to handle result */
2485int
Steve French4a5c80d2014-02-07 20:45:12 -06002486smb2_async_writev(struct cifs_writedata *wdata,
2487 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002488{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002489 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002490 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002491 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002492 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002493 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002494 struct kvec iov[2];
2495 struct smb_rqst rqst = { };
Pavel Shilovsky33319142012-09-18 16:20:29 -07002496
2497 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002498 if (rc) {
2499 if (rc == -EAGAIN && wdata->credits) {
2500 /* credits was reset by reconnect */
2501 wdata->credits = 0;
2502 /* reduce in_flight value since we won't send the req */
2503 spin_lock(&server->req_lock);
2504 server->in_flight--;
2505 spin_unlock(&server->req_lock);
2506 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002507 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002508 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002509
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002510 shdr = get_sync_hdr(req);
2511 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002512
2513 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2514 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2515 req->WriteChannelInfoOffset = 0;
2516 req->WriteChannelInfoLength = 0;
2517 req->Channel = 0;
2518 req->Offset = cpu_to_le64(wdata->offset);
2519 /* 4 for rfc1002 length field */
2520 req->DataOffset = cpu_to_le16(
2521 offsetof(struct smb2_write_req, Buffer) - 4);
2522 req->RemainingBytes = 0;
2523
2524 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002525 iov[0].iov_len = 4;
2526 iov[0].iov_base = req;
2527 iov[1].iov_len = get_rfc1002_length(req) - 1;
2528 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002529
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002530 rqst.rq_iov = iov;
2531 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002532 rqst.rq_pages = wdata->pages;
2533 rqst.rq_npages = wdata->nr_pages;
2534 rqst.rq_pagesz = wdata->pagesz;
2535 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002536
Joe Perchesf96637b2013-05-04 22:12:25 -05002537 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2538 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002539
2540 req->Length = cpu_to_le32(wdata->bytes);
2541
2542 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2543
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002544 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002545 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002546 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002547 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002548 spin_lock(&server->req_lock);
2549 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002550 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002551 spin_unlock(&server->req_lock);
2552 wake_up(&server->request_q);
2553 flags = CIFS_HAS_CREDITS;
2554 }
2555
Pavel Shilovsky33319142012-09-18 16:20:29 -07002556 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002557 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2558 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002559
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002560 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002561 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002562 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2563 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002564
Pavel Shilovsky33319142012-09-18 16:20:29 -07002565async_writev_out:
2566 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002567 return rc;
2568}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002569
2570/*
2571 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2572 * The length field from io_parms must be at least 1 and indicates a number of
2573 * elements with data to write that begins with position 1 in iov array. All
2574 * data length is specified by count.
2575 */
2576int
2577SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2578 unsigned int *nbytes, struct kvec *iov, int n_vec)
2579{
2580 int rc = 0;
2581 struct smb2_write_req *req = NULL;
2582 struct smb2_write_rsp *rsp = NULL;
2583 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002584 struct kvec rsp_iov;
2585
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002586 *nbytes = 0;
2587
2588 if (n_vec < 1)
2589 return rc;
2590
2591 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2592 if (rc)
2593 return rc;
2594
2595 if (io_parms->tcon->ses->server == NULL)
2596 return -ECONNABORTED;
2597
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002598 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002599
2600 req->PersistentFileId = io_parms->persistent_fid;
2601 req->VolatileFileId = io_parms->volatile_fid;
2602 req->WriteChannelInfoOffset = 0;
2603 req->WriteChannelInfoLength = 0;
2604 req->Channel = 0;
2605 req->Length = cpu_to_le32(io_parms->length);
2606 req->Offset = cpu_to_le64(io_parms->offset);
2607 /* 4 for rfc1002 length field */
2608 req->DataOffset = cpu_to_le16(
2609 offsetof(struct smb2_write_req, Buffer) - 4);
2610 req->RemainingBytes = 0;
2611
2612 iov[0].iov_base = (char *)req;
2613 /* 4 for rfc1002 length field and 1 for Buffer */
2614 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2615
2616 /* length of entire message including data to be written */
2617 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2618
2619 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002620 &resp_buftype, 0, &rsp_iov);
2621 cifs_small_buf_release(req);
2622 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002623
2624 if (rc) {
2625 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002626 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002627 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002628 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002629
2630 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002631 return rc;
2632}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002633
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002634static unsigned int
2635num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2636{
2637 int len;
2638 unsigned int entrycount = 0;
2639 unsigned int next_offset = 0;
2640 FILE_DIRECTORY_INFO *entryptr;
2641
2642 if (bufstart == NULL)
2643 return 0;
2644
2645 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2646
2647 while (1) {
2648 entryptr = (FILE_DIRECTORY_INFO *)
2649 ((char *)entryptr + next_offset);
2650
2651 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002652 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002653 break;
2654 }
2655
2656 len = le32_to_cpu(entryptr->FileNameLength);
2657 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002658 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2659 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002660 break;
2661 }
2662
2663 *lastentry = (char *)entryptr;
2664 entrycount++;
2665
2666 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2667 if (!next_offset)
2668 break;
2669 }
2670
2671 return entrycount;
2672}
2673
2674/*
2675 * Readdir/FindFirst
2676 */
2677int
2678SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2679 u64 persistent_fid, u64 volatile_fid, int index,
2680 struct cifs_search_info *srch_inf)
2681{
2682 struct smb2_query_directory_req *req;
2683 struct smb2_query_directory_rsp *rsp = NULL;
2684 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002685 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002686 int rc = 0;
2687 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002688 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002689 unsigned char *bufptr;
2690 struct TCP_Server_Info *server;
2691 struct cifs_ses *ses = tcon->ses;
2692 __le16 asteriks = cpu_to_le16('*');
2693 char *end_of_smb;
2694 unsigned int output_size = CIFSMaxBufSize;
2695 size_t info_buf_size;
2696
2697 if (ses && (ses->server))
2698 server = ses->server;
2699 else
2700 return -EIO;
2701
2702 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2703 if (rc)
2704 return rc;
2705
2706 switch (srch_inf->info_level) {
2707 case SMB_FIND_FILE_DIRECTORY_INFO:
2708 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2709 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2710 break;
2711 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2712 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2713 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2714 break;
2715 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002716 cifs_dbg(VFS, "info level %u isn't supported\n",
2717 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002718 rc = -EINVAL;
2719 goto qdir_exit;
2720 }
2721
2722 req->FileIndex = cpu_to_le32(index);
2723 req->PersistentFileId = persistent_fid;
2724 req->VolatileFileId = volatile_fid;
2725
2726 len = 0x2;
2727 bufptr = req->Buffer;
2728 memcpy(bufptr, &asteriks, len);
2729
2730 req->FileNameOffset =
2731 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2732 req->FileNameLength = cpu_to_le16(len);
2733 /*
2734 * BB could be 30 bytes or so longer if we used SMB2 specific
2735 * buffer lengths, but this is safe and close enough.
2736 */
2737 output_size = min_t(unsigned int, output_size, server->maxBuf);
2738 output_size = min_t(unsigned int, output_size, 2 << 15);
2739 req->OutputBufferLength = cpu_to_le32(output_size);
2740
2741 iov[0].iov_base = (char *)req;
2742 /* 4 for RFC1001 length and 1 for Buffer */
2743 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2744
2745 iov[1].iov_base = (char *)(req->Buffer);
2746 iov[1].iov_len = len;
2747
2748 inc_rfc1001_len(req, len - 1 /* Buffer */);
2749
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002750 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0, &rsp_iov);
2751 cifs_small_buf_release(req);
2752 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002753
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002754 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002755 if (rc == -ENODATA &&
2756 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002757 srch_inf->endOfSearch = true;
2758 rc = 0;
2759 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002760 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2761 goto qdir_exit;
2762 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002763
2764 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2765 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2766 info_buf_size);
2767 if (rc)
2768 goto qdir_exit;
2769
2770 srch_inf->unicode = true;
2771
2772 if (srch_inf->ntwrk_buf_start) {
2773 if (srch_inf->smallBuf)
2774 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2775 else
2776 cifs_buf_release(srch_inf->ntwrk_buf_start);
2777 }
2778 srch_inf->ntwrk_buf_start = (char *)rsp;
2779 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2780 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2781 /* 4 for rfc1002 length field */
2782 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2783 srch_inf->entries_in_buffer =
2784 num_entries(srch_inf->srch_entries_start, end_of_smb,
2785 &srch_inf->last_entry, info_buf_size);
2786 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002787 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2788 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2789 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002790 if (resp_buftype == CIFS_LARGE_BUFFER)
2791 srch_inf->smallBuf = false;
2792 else if (resp_buftype == CIFS_SMALL_BUFFER)
2793 srch_inf->smallBuf = true;
2794 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002795 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002796
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002797 return rc;
2798
2799qdir_exit:
2800 free_rsp_buf(resp_buftype, rsp);
2801 return rc;
2802}
2803
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002804static int
2805send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002806 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002807 unsigned int num, void **data, unsigned int *size)
2808{
2809 struct smb2_set_info_req *req;
2810 struct smb2_set_info_rsp *rsp = NULL;
2811 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002812 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002813 int rc = 0;
2814 int resp_buftype;
2815 unsigned int i;
2816 struct TCP_Server_Info *server;
2817 struct cifs_ses *ses = tcon->ses;
2818
2819 if (ses && (ses->server))
2820 server = ses->server;
2821 else
2822 return -EIO;
2823
2824 if (!num)
2825 return -EINVAL;
2826
2827 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2828 if (!iov)
2829 return -ENOMEM;
2830
2831 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2832 if (rc) {
2833 kfree(iov);
2834 return rc;
2835 }
2836
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002837 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002838
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002839 req->InfoType = SMB2_O_INFO_FILE;
2840 req->FileInfoClass = info_class;
2841 req->PersistentFileId = persistent_fid;
2842 req->VolatileFileId = volatile_fid;
2843
2844 /* 4 for RFC1001 length and 1 for Buffer */
2845 req->BufferOffset =
2846 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2847 req->BufferLength = cpu_to_le32(*size);
2848
2849 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2850
2851 memcpy(req->Buffer, *data, *size);
2852
2853 iov[0].iov_base = (char *)req;
2854 /* 4 for RFC1001 length */
2855 iov[0].iov_len = get_rfc1002_length(req) + 4;
2856
2857 for (i = 1; i < num; i++) {
2858 inc_rfc1001_len(req, size[i]);
2859 le32_add_cpu(&req->BufferLength, size[i]);
2860 iov[i].iov_base = (char *)data[i];
2861 iov[i].iov_len = size[i];
2862 }
2863
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002864 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0, &rsp_iov);
2865 cifs_small_buf_release(req);
2866 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002867
Steve French7d3fb242013-11-18 09:56:28 -06002868 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002869 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002870
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002871 free_rsp_buf(resp_buftype, rsp);
2872 kfree(iov);
2873 return rc;
2874}
2875
2876int
2877SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2878 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2879{
2880 struct smb2_file_rename_info info;
2881 void **data;
2882 unsigned int size[2];
2883 int rc;
2884 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2885
2886 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2887 if (!data)
2888 return -ENOMEM;
2889
2890 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2891 /* 0 = fail if target already exists */
2892 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2893 info.FileNameLength = cpu_to_le32(len);
2894
2895 data[0] = &info;
2896 size[0] = sizeof(struct smb2_file_rename_info);
2897
2898 data[1] = target_file;
2899 size[1] = len + 2 /* null */;
2900
2901 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002902 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2903 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002904 kfree(data);
2905 return rc;
2906}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002907
2908int
Steve French897fba12016-05-12 21:20:36 -05002909SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2910 u64 persistent_fid, u64 volatile_fid)
2911{
2912 __u8 delete_pending = 1;
2913 void *data;
2914 unsigned int size;
2915
2916 data = &delete_pending;
2917 size = 1; /* sizeof __u8 */
2918
2919 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2920 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2921 &size);
2922}
2923
2924int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002925SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2926 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2927{
2928 struct smb2_file_link_info info;
2929 void **data;
2930 unsigned int size[2];
2931 int rc;
2932 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2933
2934 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2935 if (!data)
2936 return -ENOMEM;
2937
2938 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2939 /* 0 = fail if link already exists */
2940 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2941 info.FileNameLength = cpu_to_le32(len);
2942
2943 data[0] = &info;
2944 size[0] = sizeof(struct smb2_file_link_info);
2945
2946 data[1] = target_file;
2947 size[1] = len + 2 /* null */;
2948
2949 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002950 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002951 kfree(data);
2952 return rc;
2953}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002954
2955int
2956SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002957 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002958{
2959 struct smb2_file_eof_info info;
2960 void *data;
2961 unsigned int size;
2962
2963 info.EndOfFile = *eof;
2964
2965 data = &info;
2966 size = sizeof(struct smb2_file_eof_info);
2967
Steve Frenchf29ebb42014-07-19 21:44:58 -05002968 if (is_falloc)
2969 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2970 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2971 else
2972 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2973 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002974}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002975
2976int
2977SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2978 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2979{
2980 unsigned int size;
2981 size = sizeof(FILE_BASIC_INFO);
2982 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2983 current->tgid, FILE_BASIC_INFORMATION, 1,
2984 (void **)&buf, &size);
2985}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002986
2987int
2988SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2989 const u64 persistent_fid, const u64 volatile_fid,
2990 __u8 oplock_level)
2991{
2992 int rc;
2993 struct smb2_oplock_break *req = NULL;
2994
Joe Perchesf96637b2013-05-04 22:12:25 -05002995 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002996 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2997
2998 if (rc)
2999 return rc;
3000
3001 req->VolatileFid = volatile_fid;
3002 req->PersistentFid = persistent_fid;
3003 req->OplockLevel = oplock_level;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003004 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003005
3006 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003007 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003008
3009 if (rc) {
3010 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003011 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003012 }
3013
3014 return rc;
3015}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003016
3017static void
3018copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3019 struct kstatfs *kst)
3020{
3021 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3022 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3023 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3024 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
3025 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3026 return;
3027}
3028
3029static int
3030build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3031 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3032{
3033 int rc;
3034 struct smb2_query_info_req *req;
3035
Joe Perchesf96637b2013-05-04 22:12:25 -05003036 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003037
3038 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3039 return -EIO;
3040
3041 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3042 if (rc)
3043 return rc;
3044
3045 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3046 req->FileInfoClass = level;
3047 req->PersistentFileId = persistent_fid;
3048 req->VolatileFileId = volatile_fid;
3049 /* 4 for rfc1002 length field and 1 for pad */
3050 req->InputBufferOffset =
3051 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3052 req->OutputBufferLength = cpu_to_le32(
3053 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3054
3055 iov->iov_base = (char *)req;
3056 /* 4 for rfc1002 length field */
3057 iov->iov_len = get_rfc1002_length(req) + 4;
3058 return 0;
3059}
3060
3061int
3062SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3063 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3064{
3065 struct smb2_query_info_rsp *rsp = NULL;
3066 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003067 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003068 int rc = 0;
3069 int resp_buftype;
3070 struct cifs_ses *ses = tcon->ses;
3071 struct smb2_fs_full_size_info *info = NULL;
3072
3073 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3074 sizeof(struct smb2_fs_full_size_info),
3075 persistent_fid, volatile_fid);
3076 if (rc)
3077 return rc;
3078
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003079 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0, &rsp_iov);
3080 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003081 if (rc) {
3082 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003083 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003084 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003085 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003086
3087 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3088 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3089 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3090 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3091 sizeof(struct smb2_fs_full_size_info));
3092 if (!rc)
3093 copy_fs_info_to_kstatfs(info, fsdata);
3094
Steve French34f62642013-10-09 02:07:00 -05003095qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003096 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003097 return rc;
3098}
3099
3100int
3101SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003102 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003103{
3104 struct smb2_query_info_rsp *rsp = NULL;
3105 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003106 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003107 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003108 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003109 struct cifs_ses *ses = tcon->ses;
3110 unsigned int rsp_len, offset;
3111
Steven French21671142013-10-09 13:36:35 -05003112 if (level == FS_DEVICE_INFORMATION) {
3113 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3114 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3115 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3116 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3117 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003118 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3119 max_len = sizeof(struct smb3_fs_ss_info);
3120 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003121 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003122 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003123 return -EINVAL;
3124 }
3125
3126 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003127 persistent_fid, volatile_fid);
3128 if (rc)
3129 return rc;
3130
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003131 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0, &rsp_iov);
3132 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003133 if (rc) {
3134 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3135 goto qfsattr_exit;
3136 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003137 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003138
3139 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3140 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003141 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3142 if (rc)
3143 goto qfsattr_exit;
3144
3145 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003146 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3147 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003148 rsp_len, max_len));
3149 else if (level == FS_DEVICE_INFORMATION)
3150 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3151 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003152 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3153 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3154 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3155 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3156 tcon->perf_sector_size =
3157 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3158 }
Steve French34f62642013-10-09 02:07:00 -05003159
3160qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003161 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003162 return rc;
3163}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003164
3165int
3166smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3167 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3168 const __u32 num_lock, struct smb2_lock_element *buf)
3169{
3170 int rc = 0;
3171 struct smb2_lock_req *req = NULL;
3172 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003173 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003174 int resp_buf_type;
3175 unsigned int count;
3176
Joe Perchesf96637b2013-05-04 22:12:25 -05003177 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003178
3179 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3180 if (rc)
3181 return rc;
3182
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003183 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003184 req->LockCount = cpu_to_le16(num_lock);
3185
3186 req->PersistentFileId = persist_fid;
3187 req->VolatileFileId = volatile_fid;
3188
3189 count = num_lock * sizeof(struct smb2_lock_element);
3190 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3191
3192 iov[0].iov_base = (char *)req;
3193 /* 4 for rfc1002 length field and count for all locks */
3194 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3195 iov[1].iov_base = (char *)buf;
3196 iov[1].iov_len = count;
3197
3198 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003199 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
3200 &rsp_iov);
3201 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003202 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003203 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003204 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3205 }
3206
3207 return rc;
3208}
3209
3210int
3211SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3212 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3213 const __u64 length, const __u64 offset, const __u32 lock_flags,
3214 const bool wait)
3215{
3216 struct smb2_lock_element lock;
3217
3218 lock.Offset = cpu_to_le64(offset);
3219 lock.Length = cpu_to_le64(length);
3220 lock.Flags = cpu_to_le32(lock_flags);
3221 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3222 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3223
3224 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3225}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003226
3227int
3228SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3229 __u8 *lease_key, const __le32 lease_state)
3230{
3231 int rc;
3232 struct smb2_lease_ack *req = NULL;
3233
Joe Perchesf96637b2013-05-04 22:12:25 -05003234 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003235 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3236
3237 if (rc)
3238 return rc;
3239
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003240 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003241 req->StructureSize = cpu_to_le16(36);
3242 inc_rfc1001_len(req, 12);
3243
3244 memcpy(req->LeaseKey, lease_key, 16);
3245 req->LeaseState = lease_state;
3246
3247 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003248 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003249
3250 if (rc) {
3251 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003252 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003253 }
3254
3255 return rc;
3256}