blob: 2069431b32e368aa7db4dd801aa83e773b93c6c7 [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
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070080static int encryption_required(const struct cifs_tcon *tcon)
81{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080082 if (!tcon)
83 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070084 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
85 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
86 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080087 if (tcon->seal &&
88 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
89 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070090 return 0;
91}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040092
93static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070094smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040095 const struct cifs_tcon *tcon)
96{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070097 shdr->ProtocolId = SMB2_PROTO_NUMBER;
98 shdr->StructureSize = cpu_to_le16(64);
99 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100100 if (tcon && tcon->ses && tcon->ses->server) {
101 struct TCP_Server_Info *server = tcon->ses->server;
102
103 spin_lock(&server->req_lock);
104 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500105 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700106 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100107 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700108 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500109 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100110 server->credits, 2));
111 spin_unlock(&server->req_lock);
112 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700113 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100114 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700115 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400116
117 if (!tcon)
118 goto out;
119
Steve French2b80d042013-06-23 18:43:37 -0500120 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
121 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500122 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500123 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700124 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500125 /* else CreditCharge MBZ */
126
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700127 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400128 /* Uid is not converted */
129 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700130 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500131
132 /*
133 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
134 * to pass the path on the Open SMB prefixed by \\server\share.
135 * Not sure when we would need to do the augmented path (if ever) and
136 * setting this flag breaks the SMB2 open operation since it is
137 * illegal to send an empty path name (without \\server\share prefix)
138 * when the DFS flag is set in the SMB open header. We could
139 * consider setting the flag on all operations other than open
140 * but it is safer to net set it for now.
141 */
142/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700143 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500144
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700145 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
146 !encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700147 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400148out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400149 return;
150}
151
152static int
153smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
154{
155 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400156 struct nls_table *nls_codepage;
157 struct cifs_ses *ses;
158 struct TCP_Server_Info *server;
159
160 /*
161 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
162 * check for tcp and smb session status done differently
163 * for those three - in the calling routine.
164 */
165 if (tcon == NULL)
166 return rc;
167
168 if (smb2_command == SMB2_TREE_CONNECT)
169 return rc;
170
171 if (tcon->tidStatus == CifsExiting) {
172 /*
173 * only tree disconnect, open, and write,
174 * (and ulogoff which does not have tcon)
175 * are allowed as we start force umount.
176 */
177 if ((smb2_command != SMB2_WRITE) &&
178 (smb2_command != SMB2_CREATE) &&
179 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500180 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
181 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400182 return -ENODEV;
183 }
184 }
185 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
186 (!tcon->ses->server))
187 return -EIO;
188
189 ses = tcon->ses;
190 server = ses->server;
191
192 /*
193 * Give demultiplex thread up to 10 seconds to reconnect, should be
194 * greater than cifs socket timeout which is 7 seconds
195 */
196 while (server->tcpStatus == CifsNeedReconnect) {
197 /*
198 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
199 * here since they are implicitly done when session drops.
200 */
201 switch (smb2_command) {
202 /*
203 * BB Should we keep oplock break and add flush to exceptions?
204 */
205 case SMB2_TREE_DISCONNECT:
206 case SMB2_CANCEL:
207 case SMB2_CLOSE:
208 case SMB2_OPLOCK_BREAK:
209 return -EAGAIN;
210 }
211
212 wait_event_interruptible_timeout(server->response_q,
213 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
214
215 /* are we still trying to reconnect? */
216 if (server->tcpStatus != CifsNeedReconnect)
217 break;
218
219 /*
220 * on "soft" mounts we wait once. Hard mounts keep
221 * retrying until process is killed or server comes
222 * back on-line
223 */
224 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500225 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400226 return -EHOSTDOWN;
227 }
228 }
229
230 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
231 return rc;
232
233 nls_codepage = load_nls_default();
234
235 /*
236 * need to prevent multiple threads trying to simultaneously reconnect
237 * the same SMB session
238 */
239 mutex_lock(&tcon->ses->session_mutex);
240 rc = cifs_negotiate_protocol(0, tcon->ses);
241 if (!rc && tcon->ses->need_reconnect)
242 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
243
244 if (rc || !tcon->need_reconnect) {
245 mutex_unlock(&tcon->ses->session_mutex);
246 goto out;
247 }
248
249 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800250 if (tcon->use_persistent)
251 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500252
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400253 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
254 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500255
Joe Perchesf96637b2013-05-04 22:12:25 -0500256 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400257 if (rc)
258 goto out;
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800259
260 if (smb2_command != SMB2_INTERNAL_CMD)
261 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
262
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400263 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400264out:
265 /*
266 * Check if handle based operation so we know whether we can continue
267 * or not without returning to caller to reset file handle.
268 */
269 /*
270 * BB Is flush done by server on drop of tcp session? Should we special
271 * case it and skip above?
272 */
273 switch (smb2_command) {
274 case SMB2_FLUSH:
275 case SMB2_READ:
276 case SMB2_WRITE:
277 case SMB2_LOCK:
278 case SMB2_IOCTL:
279 case SMB2_QUERY_DIRECTORY:
280 case SMB2_CHANGE_NOTIFY:
281 case SMB2_QUERY_INFO:
282 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800283 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400284 }
285 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400286 return rc;
287}
288
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700289static void
290fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
291 unsigned int *total_len)
292{
293 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
294 /* lookup word count ie StructureSize from table */
295 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
296
297 /*
298 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
299 * largest operations (Create)
300 */
301 memset(buf, 0, 256);
302
303 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
304 spdu->StructureSize2 = cpu_to_le16(parmsize);
305
306 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
307}
308
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800309/* init request without RFC1001 length at the beginning */
310static int
311smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
312 void **request_buf, unsigned int *total_len)
313{
314 int rc;
315 struct smb2_sync_hdr *shdr;
316
317 rc = smb2_reconnect(smb2_command, tcon);
318 if (rc)
319 return rc;
320
321 /* BB eventually switch this to SMB2 specific small buf size */
322 *request_buf = cifs_small_buf_get();
323 if (*request_buf == NULL) {
324 /* BB should we add a retry in here if not a writepage? */
325 return -ENOMEM;
326 }
327
328 shdr = (struct smb2_sync_hdr *)(*request_buf);
329
330 fill_small_buf(smb2_command, tcon, shdr, total_len);
331
332 if (tcon != NULL) {
333#ifdef CONFIG_CIFS_STATS2
334 uint16_t com_code = le16_to_cpu(smb2_command);
335
336 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
337#endif
338 cifs_stats_inc(&tcon->num_smbs_sent);
339 }
340
341 return rc;
342}
343
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400344/*
345 * Allocate and return pointer to an SMB request hdr, and set basic
346 * SMB information in the SMB header. If the return code is zero, this
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800347 * function must have filled in request_buf pointer. The returned buffer
348 * has RFC1001 length at the beginning.
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400349 */
350static int
351small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
352 void **request_buf)
353{
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700354 int rc;
355 unsigned int total_len;
356 struct smb2_pdu *pdu;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400357
358 rc = smb2_reconnect(smb2_command, tcon);
359 if (rc)
360 return rc;
361
362 /* BB eventually switch this to SMB2 specific small buf size */
363 *request_buf = cifs_small_buf_get();
364 if (*request_buf == NULL) {
365 /* BB should we add a retry in here if not a writepage? */
366 return -ENOMEM;
367 }
368
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700369 pdu = (struct smb2_pdu *)(*request_buf);
370
371 fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len);
372
373 /* Note this is only network field converted to big endian */
374 pdu->hdr.smb2_buf_length = cpu_to_be32(total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400375
376 if (tcon != NULL) {
377#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400378 uint16_t com_code = le16_to_cpu(smb2_command);
379 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400380#endif
381 cifs_stats_inc(&tcon->num_smbs_sent);
382 }
383
384 return rc;
385}
386
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500387#ifdef CONFIG_CIFS_SMB311
388/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
389#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
390
391
392#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
393#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
394
395static void
396build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
397{
398 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
399 pneg_ctxt->DataLength = cpu_to_le16(38);
400 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
401 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
402 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
403 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
404}
405
406static void
407build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
408{
409 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
410 pneg_ctxt->DataLength = cpu_to_le16(6);
411 pneg_ctxt->CipherCount = cpu_to_le16(2);
412 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
413 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
414}
415
416static void
417assemble_neg_contexts(struct smb2_negotiate_req *req)
418{
419
420 /* +4 is to account for the RFC1001 len field */
421 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
422
423 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
424 /* Add 2 to size to round to 8 byte boundary */
425 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
426 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
427 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
428 req->NegotiateContextCount = cpu_to_le16(2);
429 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
430 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
431}
432#else
433static void assemble_neg_contexts(struct smb2_negotiate_req *req)
434{
435 return;
436}
437#endif /* SMB311 */
438
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400439/*
440 *
441 * SMB2 Worker functions follow:
442 *
443 * The general structure of the worker functions is:
444 * 1) Call smb2_init (assembles SMB2 header)
445 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
446 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
447 * 4) Decode SMB2 command specific fields in the fixed length area
448 * 5) Decode variable length data area (if any for this SMB2 command type)
449 * 6) Call free smb buffer
450 * 7) return
451 *
452 */
453
454int
455SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
456{
457 struct smb2_negotiate_req *req;
458 struct smb2_negotiate_rsp *rsp;
459 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700460 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400461 int rc = 0;
462 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400463 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400464 int blob_offset, blob_length;
465 char *security_blob;
466 int flags = CIFS_NEG_OP;
467
Joe Perchesf96637b2013-05-04 22:12:25 -0500468 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400469
Jeff Layton3534b852013-05-24 07:41:01 -0400470 if (!server) {
471 WARN(1, "%s: server is NULL!\n", __func__);
472 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400473 }
474
475 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
476 if (rc)
477 return rc;
478
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700479 req->hdr.sync_hdr.SessionId = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400480
Steve Frenche4aa25e2012-10-01 12:26:22 -0500481 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400482
Steve Frenche4aa25e2012-10-01 12:26:22 -0500483 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
484 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400485
486 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400487 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500488 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400489 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500490 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400491 else
492 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400493
Steve Frenche4aa25e2012-10-01 12:26:22 -0500494 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400495
Steve French3c5f9be12014-05-13 13:37:45 -0700496 /* ClientGUID must be zero for SMB2.02 dialect */
497 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
498 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500499 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700500 memcpy(req->ClientGUID, server->client_guid,
501 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500502 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
503 assemble_neg_contexts(req);
504 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400505 iov[0].iov_base = (char *)req;
506 /* 4 for rfc1002 length field */
507 iov[0].iov_len = get_rfc1002_length(req) + 4;
508
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700509 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
510 cifs_small_buf_release(req);
511 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400512 /*
513 * No tcon so can't do
514 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
515 */
516 if (rc != 0)
517 goto neg_exit;
518
Joe Perchesf96637b2013-05-04 22:12:25 -0500519 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400520
Steve Frenche4aa25e2012-10-01 12:26:22 -0500521 /* BB we may eventually want to match the negotiated vs. requested
522 dialect, even though we are only requesting one at a time */
523 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500524 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500525 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500526 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500527 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500528 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500529 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
530 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600531#ifdef CONFIG_CIFS_SMB311
532 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
533 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
534#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400535 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500536 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500537 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400538 rc = -EIO;
539 goto neg_exit;
540 }
541 server->dialect = le16_to_cpu(rsp->DialectRevision);
542
Jeff Laytone598d1d82013-05-26 07:00:59 -0400543 /* SMB2 only has an extended negflavor */
544 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400545 /* set it to the maximum buffer size value we can send with 1 credit */
546 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
547 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400548 server->max_read = le32_to_cpu(rsp->MaxReadSize);
549 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
550 /* BB Do we need to validate the SecurityMode? */
551 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
552 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400553 /* Internal types */
554 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400555
556 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
557 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500558 /*
559 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
560 * for us will be
561 * ses->sectype = RawNTLMSSP;
562 * but for time being this is our only auth choice so doesn't matter.
563 * We just found a server which sets blob length to zero expecting raw.
564 */
565 if (blob_length == 0)
566 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700567
Jeff Layton38d77c52013-05-26 07:01:00 -0400568 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400569 if (rc)
570 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500571 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500572 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500573 if (rc == 1)
574 rc = 0;
575 else if (rc == 0)
576 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400577 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400578neg_exit:
579 free_rsp_buf(resp_buftype, rsp);
580 return rc;
581}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400582
Steve Frenchff1c0382013-11-19 23:44:46 -0600583int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
584{
585 int rc = 0;
586 struct validate_negotiate_info_req vneg_inbuf;
587 struct validate_negotiate_info_rsp *pneg_rsp;
588 u32 rsplen;
589
590 cifs_dbg(FYI, "validate negotiate\n");
591
592 /*
593 * validation ioctl must be signed, so no point sending this if we
594 * can not sign it. We could eventually change this to selectively
595 * sign just this, the first and only signed request on a connection.
596 * This is good enough for now since a user who wants better security
597 * would also enable signing on the mount. Having validation of
598 * negotiate info for signed connections helps reduce attack vectors
599 */
600 if (tcon->ses->server->sign == false)
601 return 0; /* validation requires signing */
602
603 vneg_inbuf.Capabilities =
604 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100605 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
606 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600607
608 if (tcon->ses->sign)
609 vneg_inbuf.SecurityMode =
610 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
611 else if (global_secflags & CIFSSEC_MAY_SIGN)
612 vneg_inbuf.SecurityMode =
613 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
614 else
615 vneg_inbuf.SecurityMode = 0;
616
617 vneg_inbuf.DialectCount = cpu_to_le16(1);
618 vneg_inbuf.Dialects[0] =
619 cpu_to_le16(tcon->ses->server->vals->protocol_id);
620
621 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
622 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100623 false /* use_ipc */,
Steve Frenchff1c0382013-11-19 23:44:46 -0600624 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
625 (char **)&pneg_rsp, &rsplen);
626
627 if (rc != 0) {
628 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
629 return -EIO;
630 }
631
632 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
633 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
634 return -EIO;
635 }
636
637 /* check validate negotiate info response matches what we got earlier */
638 if (pneg_rsp->Dialect !=
639 cpu_to_le16(tcon->ses->server->vals->protocol_id))
640 goto vneg_out;
641
642 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
643 goto vneg_out;
644
645 /* do not validate server guid because not saved at negprot time yet */
646
647 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
648 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
649 goto vneg_out;
650
651 /* validate negotiate successful */
652 cifs_dbg(FYI, "validate negotiate info successful\n");
653 return 0;
654
655vneg_out:
656 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
657 return -EIO;
658}
659
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100660struct SMB2_sess_data {
661 unsigned int xid;
662 struct cifs_ses *ses;
663 struct nls_table *nls_cp;
664 void (*func)(struct SMB2_sess_data *);
665 int result;
666 u64 previous_session;
667
668 /* we will send the SMB in three pieces:
669 * a fixed length beginning part, an optional
670 * SPNEGO blob (which can be zero length), and a
671 * last part which will include the strings
672 * and rest of bcc area. This allows us to avoid
673 * a large buffer 17K allocation
674 */
675 int buf0_type;
676 struct kvec iov[2];
677};
678
679static int
680SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
681{
682 int rc;
683 struct cifs_ses *ses = sess_data->ses;
684 struct smb2_sess_setup_req *req;
685 struct TCP_Server_Info *server = ses->server;
686
687 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
688 if (rc)
689 return rc;
690
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700691 /* First session, not a reauthenticate */
692 req->hdr.sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100693
694 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
695 req->PreviousSessionId = sess_data->previous_session;
696
697 req->Flags = 0; /* MBZ */
698 /* to enable echos and oplocks */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700699 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100700
701 /* only one of SMB2 signing flags may be set in SMB2 request */
702 if (server->sign)
703 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
704 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
705 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
706 else
707 req->SecurityMode = 0;
708
709 req->Capabilities = 0;
710 req->Channel = 0; /* MBZ */
711
712 sess_data->iov[0].iov_base = (char *)req;
713 /* 4 for rfc1002 length field and 1 for pad */
714 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
715 /*
716 * This variable will be used to clear the buffer
717 * allocated above in case of any error in the calling function.
718 */
719 sess_data->buf0_type = CIFS_SMALL_BUFFER;
720
721 return 0;
722}
723
724static void
725SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
726{
727 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
728 sess_data->buf0_type = CIFS_NO_BUFFER;
729}
730
731static int
732SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
733{
734 int rc;
735 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700736 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100737
738 /* Testing shows that buffer offset must be at location of Buffer[0] */
739 req->SecurityBufferOffset =
740 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
741 1 /* pad */ - 4 /* rfc1001 len */);
742 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
743
744 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
745
746 /* BB add code to build os and lm fields */
747
748 rc = SendReceive2(sess_data->xid, sess_data->ses,
749 sess_data->iov, 2,
750 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700751 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
752 cifs_small_buf_release(sess_data->iov[0].iov_base);
753 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100754
755 return rc;
756}
757
758static int
759SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
760{
761 int rc = 0;
762 struct cifs_ses *ses = sess_data->ses;
763
764 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800765 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100766 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100767 if (rc) {
768 cifs_dbg(FYI,
769 "SMB3 session key generation failed\n");
770 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800771 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100772 }
773 }
774 if (!ses->server->session_estab) {
775 ses->server->sequence_number = 0x2;
776 ses->server->session_estab = true;
777 }
778 mutex_unlock(&ses->server->srv_mutex);
779
780 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
781 spin_lock(&GlobalMid_Lock);
782 ses->status = CifsGood;
783 ses->need_reconnect = false;
784 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100785 return rc;
786}
787
788#ifdef CONFIG_CIFS_UPCALL
789static void
790SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
791{
792 int rc;
793 struct cifs_ses *ses = sess_data->ses;
794 struct cifs_spnego_msg *msg;
795 struct key *spnego_key = NULL;
796 struct smb2_sess_setup_rsp *rsp = NULL;
797
798 rc = SMB2_sess_alloc_buffer(sess_data);
799 if (rc)
800 goto out;
801
802 spnego_key = cifs_get_spnego_key(ses);
803 if (IS_ERR(spnego_key)) {
804 rc = PTR_ERR(spnego_key);
805 spnego_key = NULL;
806 goto out;
807 }
808
809 msg = spnego_key->payload.data[0];
810 /*
811 * check version field to make sure that cifs.upcall is
812 * sending us a response in an expected form
813 */
814 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
815 cifs_dbg(VFS,
816 "bad cifs.upcall version. Expected %d got %d",
817 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
818 rc = -EKEYREJECTED;
819 goto out_put_spnego_key;
820 }
821
822 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
823 GFP_KERNEL);
824 if (!ses->auth_key.response) {
825 cifs_dbg(VFS,
826 "Kerberos can't allocate (%u bytes) memory",
827 msg->sesskey_len);
828 rc = -ENOMEM;
829 goto out_put_spnego_key;
830 }
831 ses->auth_key.len = msg->sesskey_len;
832
833 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
834 sess_data->iov[1].iov_len = msg->secblob_len;
835
836 rc = SMB2_sess_sendreceive(sess_data);
837 if (rc)
838 goto out_put_spnego_key;
839
840 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700841 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100842
843 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100844
845 rc = SMB2_sess_establish_session(sess_data);
846out_put_spnego_key:
847 key_invalidate(spnego_key);
848 key_put(spnego_key);
849out:
850 sess_data->result = rc;
851 sess_data->func = NULL;
852 SMB2_sess_free_buffer(sess_data);
853}
854#else
855static void
856SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
857{
858 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
859 sess_data->result = -EOPNOTSUPP;
860 sess_data->func = NULL;
861}
862#endif
863
Sachin Prabhu166cea42016-10-07 19:11:22 +0100864static void
865SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
866
867static void
868SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
869{
870 int rc;
871 struct cifs_ses *ses = sess_data->ses;
872 struct smb2_sess_setup_rsp *rsp = NULL;
873 char *ntlmssp_blob = NULL;
874 bool use_spnego = false; /* else use raw ntlmssp */
875 u16 blob_length = 0;
876
877 /*
878 * If memory allocation is successful, caller of this function
879 * frees it.
880 */
881 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
882 if (!ses->ntlmssp) {
883 rc = -ENOMEM;
884 goto out_err;
885 }
886 ses->ntlmssp->sesskey_per_smbsess = true;
887
888 rc = SMB2_sess_alloc_buffer(sess_data);
889 if (rc)
890 goto out_err;
891
892 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
893 GFP_KERNEL);
894 if (ntlmssp_blob == NULL) {
895 rc = -ENOMEM;
896 goto out;
897 }
898
899 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
900 if (use_spnego) {
901 /* BB eventually need to add this */
902 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
903 rc = -EOPNOTSUPP;
904 goto out;
905 } else {
906 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
907 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
908 }
909 sess_data->iov[1].iov_base = ntlmssp_blob;
910 sess_data->iov[1].iov_len = blob_length;
911
912 rc = SMB2_sess_sendreceive(sess_data);
913 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
914
915 /* If true, rc here is expected and not an error */
916 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700917 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +0100918 rc = 0;
919
920 if (rc)
921 goto out;
922
923 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
924 le16_to_cpu(rsp->SecurityBufferOffset)) {
925 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
926 le16_to_cpu(rsp->SecurityBufferOffset));
927 rc = -EIO;
928 goto out;
929 }
930 rc = decode_ntlmssp_challenge(rsp->Buffer,
931 le16_to_cpu(rsp->SecurityBufferLength), ses);
932 if (rc)
933 goto out;
934
935 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
936
937
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700938 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100939 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +0100940
941out:
942 kfree(ntlmssp_blob);
943 SMB2_sess_free_buffer(sess_data);
944 if (!rc) {
945 sess_data->result = 0;
946 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
947 return;
948 }
949out_err:
950 kfree(ses->ntlmssp);
951 ses->ntlmssp = NULL;
952 sess_data->result = rc;
953 sess_data->func = NULL;
954}
955
956static void
957SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
958{
959 int rc;
960 struct cifs_ses *ses = sess_data->ses;
961 struct smb2_sess_setup_req *req;
962 struct smb2_sess_setup_rsp *rsp = NULL;
963 unsigned char *ntlmssp_blob = NULL;
964 bool use_spnego = false; /* else use raw ntlmssp */
965 u16 blob_length = 0;
966
967 rc = SMB2_sess_alloc_buffer(sess_data);
968 if (rc)
969 goto out;
970
971 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700972 req->hdr.sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100973
974 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
975 sess_data->nls_cp);
976 if (rc) {
977 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
978 goto out;
979 }
980
981 if (use_spnego) {
982 /* BB eventually need to add this */
983 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
984 rc = -EOPNOTSUPP;
985 goto out;
986 }
987 sess_data->iov[1].iov_base = ntlmssp_blob;
988 sess_data->iov[1].iov_len = blob_length;
989
990 rc = SMB2_sess_sendreceive(sess_data);
991 if (rc)
992 goto out;
993
994 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
995
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700996 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100997 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +0100998
999 rc = SMB2_sess_establish_session(sess_data);
1000out:
1001 kfree(ntlmssp_blob);
1002 SMB2_sess_free_buffer(sess_data);
1003 kfree(ses->ntlmssp);
1004 ses->ntlmssp = NULL;
1005 sess_data->result = rc;
1006 sess_data->func = NULL;
1007}
1008
1009static int
1010SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1011{
1012 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
1013 ses->sectype = RawNTLMSSP;
1014
1015 switch (ses->sectype) {
1016 case Kerberos:
1017 sess_data->func = SMB2_auth_kerberos;
1018 break;
1019 case RawNTLMSSP:
1020 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1021 break;
1022 default:
1023 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
1024 return -EOPNOTSUPP;
1025 }
1026
1027 return 0;
1028}
1029
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001030int
1031SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1032 const struct nls_table *nls_cp)
1033{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001034 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001035 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001036 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001037
Joe Perchesf96637b2013-05-04 22:12:25 -05001038 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001039
Jeff Layton3534b852013-05-24 07:41:01 -04001040 if (!server) {
1041 WARN(1, "%s: server is NULL!\n", __func__);
1042 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001043 }
1044
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001045 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1046 if (!sess_data)
1047 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001048
1049 rc = SMB2_select_sec(ses, sess_data);
1050 if (rc)
1051 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001052 sess_data->xid = xid;
1053 sess_data->ses = ses;
1054 sess_data->buf0_type = CIFS_NO_BUFFER;
1055 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001056
Sachin Prabhu166cea42016-10-07 19:11:22 +01001057 while (sess_data->func)
1058 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001059
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001060 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001061out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001062 kfree(sess_data);
1063 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001064}
1065
1066int
1067SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1068{
1069 struct smb2_logoff_req *req; /* response is also trivial struct */
1070 int rc = 0;
1071 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001072 int flags = 0;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001073
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;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001091
1092 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1093 flags |= CIFS_TRANSFORM_REQ;
1094 else if (server->sign)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001095 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001096
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001097 rc = SendReceiveNoRsp(xid, ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001098 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001099 /*
1100 * No tcon so can't do
1101 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1102 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001103
1104smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001105 return rc;
1106}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001107
1108static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1109{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001110 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001111}
1112
1113#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1114
Steve Frenchde9f68d2013-11-15 11:26:24 -06001115/* These are similar values to what Windows uses */
1116static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1117{
1118 tcon->max_chunks = 256;
1119 tcon->max_bytes_chunk = 1048576;
1120 tcon->max_bytes_copy = 16777216;
1121}
1122
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001123int
1124SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1125 struct cifs_tcon *tcon, const struct nls_table *cp)
1126{
1127 struct smb2_tree_connect_req *req;
1128 struct smb2_tree_connect_rsp *rsp = NULL;
1129 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001130 struct kvec rsp_iov;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001131 int rc = 0;
1132 int resp_buftype;
1133 int unc_path_len;
1134 struct TCP_Server_Info *server;
1135 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001136 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001137
Joe Perchesf96637b2013-05-04 22:12:25 -05001138 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001139
1140 if ((ses->server) && tree)
1141 server = ses->server;
1142 else
1143 return -EIO;
1144
1145 if (tcon && tcon->bad_network_name)
1146 return -ENOENT;
1147
1148 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1149 if (unc_path == NULL)
1150 return -ENOMEM;
1151
1152 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1153 unc_path_len *= 2;
1154 if (unc_path_len < 2) {
1155 kfree(unc_path);
1156 return -EINVAL;
1157 }
1158
1159 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1160 if (rc) {
1161 kfree(unc_path);
1162 return rc;
1163 }
1164
1165 if (tcon == NULL) {
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001166 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1167 flags |= CIFS_TRANSFORM_REQ;
1168
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001169 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001170 req->hdr.sync_hdr.SessionId = ses->Suid;
Aurelien Aptelb9043cc2017-02-13 16:19:04 +01001171 if (ses->server->sign)
1172 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001173 } else if (encryption_required(tcon))
1174 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001175
1176 iov[0].iov_base = (char *)req;
1177 /* 4 for rfc1002 length field and 1 for pad */
1178 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1179
1180 /* Testing shows that buffer offset must be at location of Buffer[0] */
1181 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1182 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1183 req->PathLength = cpu_to_le16(unc_path_len - 2);
1184 iov[1].iov_base = unc_path;
1185 iov[1].iov_len = unc_path_len;
1186
1187 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1188
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001189 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001190 cifs_small_buf_release(req);
1191 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001192
1193 if (rc != 0) {
1194 if (tcon) {
1195 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1196 tcon->need_reconnect = true;
1197 }
1198 goto tcon_error_exit;
1199 }
1200
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001201 if (tcon == NULL) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001202 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001203 goto tcon_exit;
1204 }
1205
1206 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001207 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001208 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1209 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001210 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001211 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1212 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001213 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001214 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001215 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001216 rc = -EOPNOTSUPP;
1217 goto tcon_error_exit;
1218 }
1219
1220 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001221 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001222 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1223 tcon->tidStatus = CifsGood;
1224 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001225 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001226 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001227
1228 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1229 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001230 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001231
1232 if (tcon->seal &&
1233 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1234 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1235
Steve Frenchde9f68d2013-11-15 11:26:24 -06001236 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001237 if (tcon->ses->server->ops->validate_negotiate)
1238 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001239tcon_exit:
1240 free_rsp_buf(resp_buftype, rsp);
1241 kfree(unc_path);
1242 return rc;
1243
1244tcon_error_exit:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001245 if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001246 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Steve French18f39e72014-08-17 00:22:24 -05001247 if (tcon)
1248 tcon->bad_network_name = true;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001249 }
1250 goto tcon_exit;
1251}
1252
1253int
1254SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1255{
1256 struct smb2_tree_disconnect_req *req; /* response is trivial */
1257 int rc = 0;
1258 struct TCP_Server_Info *server;
1259 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001260 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001261
Joe Perchesf96637b2013-05-04 22:12:25 -05001262 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001263
1264 if (ses && (ses->server))
1265 server = ses->server;
1266 else
1267 return -EIO;
1268
1269 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1270 return 0;
1271
1272 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1273 if (rc)
1274 return rc;
1275
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001276 if (encryption_required(tcon))
1277 flags |= CIFS_TRANSFORM_REQ;
1278
1279 rc = SendReceiveNoRsp(xid, ses, (char *)req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001280 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001281 if (rc)
1282 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1283
1284 return rc;
1285}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001286
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001287
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001288static struct create_durable *
1289create_durable_buf(void)
1290{
1291 struct create_durable *buf;
1292
1293 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1294 if (!buf)
1295 return NULL;
1296
1297 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001298 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001299 buf->ccontext.DataLength = cpu_to_le32(16);
1300 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1301 (struct create_durable, Name));
1302 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001303 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001304 buf->Name[0] = 'D';
1305 buf->Name[1] = 'H';
1306 buf->Name[2] = 'n';
1307 buf->Name[3] = 'Q';
1308 return buf;
1309}
1310
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001311static struct create_durable *
1312create_reconnect_durable_buf(struct cifs_fid *fid)
1313{
1314 struct create_durable *buf;
1315
1316 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1317 if (!buf)
1318 return NULL;
1319
1320 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1321 (struct create_durable, Data));
1322 buf->ccontext.DataLength = cpu_to_le32(16);
1323 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1324 (struct create_durable, Name));
1325 buf->ccontext.NameLength = cpu_to_le16(4);
1326 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1327 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001328 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001329 buf->Name[0] = 'D';
1330 buf->Name[1] = 'H';
1331 buf->Name[2] = 'n';
1332 buf->Name[3] = 'C';
1333 return buf;
1334}
1335
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001336static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001337parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1338 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001339{
1340 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001341 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001342 unsigned int next;
1343 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001344 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001345
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001346 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001347 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001348 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001349 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001350 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001351 if (le16_to_cpu(cc->NameLength) == 4 &&
1352 strncmp(name, "RqLs", 4) == 0)
1353 return server->ops->parse_lease_buf(cc, epoch);
1354
1355 next = le32_to_cpu(cc->Next);
1356 if (!next)
1357 break;
1358 remaining -= next;
1359 cc = (struct create_context *)((char *)cc + next);
1360 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001361
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001362 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001363}
1364
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001365static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001366add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1367 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001368{
1369 struct smb2_create_req *req = iov[0].iov_base;
1370 unsigned int num = *num_iovec;
1371
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001372 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001373 if (iov[num].iov_base == NULL)
1374 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001375 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001376 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1377 if (!req->CreateContextsOffset)
1378 req->CreateContextsOffset = cpu_to_le32(
1379 sizeof(struct smb2_create_req) - 4 +
1380 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001381 le32_add_cpu(&req->CreateContextsLength,
1382 server->vals->create_lease_size);
1383 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001384 *num_iovec = num + 1;
1385 return 0;
1386}
1387
Steve Frenchb56eae42015-11-03 09:26:27 -06001388static struct create_durable_v2 *
1389create_durable_v2_buf(struct cifs_fid *pfid)
1390{
1391 struct create_durable_v2 *buf;
1392
1393 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1394 if (!buf)
1395 return NULL;
1396
1397 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1398 (struct create_durable_v2, dcontext));
1399 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1400 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1401 (struct create_durable_v2, Name));
1402 buf->ccontext.NameLength = cpu_to_le16(4);
1403
1404 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1405 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001406 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001407 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1408
1409 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1410 buf->Name[0] = 'D';
1411 buf->Name[1] = 'H';
1412 buf->Name[2] = '2';
1413 buf->Name[3] = 'Q';
1414 return buf;
1415}
1416
1417static struct create_durable_handle_reconnect_v2 *
1418create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1419{
1420 struct create_durable_handle_reconnect_v2 *buf;
1421
1422 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1423 GFP_KERNEL);
1424 if (!buf)
1425 return NULL;
1426
1427 buf->ccontext.DataOffset =
1428 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1429 dcontext));
1430 buf->ccontext.DataLength =
1431 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1432 buf->ccontext.NameOffset =
1433 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1434 Name));
1435 buf->ccontext.NameLength = cpu_to_le16(4);
1436
1437 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1438 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1439 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1440 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1441
1442 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1443 buf->Name[0] = 'D';
1444 buf->Name[1] = 'H';
1445 buf->Name[2] = '2';
1446 buf->Name[3] = 'C';
1447 return buf;
1448}
1449
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001450static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001451add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001452 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001453{
1454 struct smb2_create_req *req = iov[0].iov_base;
1455 unsigned int num = *num_iovec;
1456
Steve Frenchb56eae42015-11-03 09:26:27 -06001457 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1458 if (iov[num].iov_base == NULL)
1459 return -ENOMEM;
1460 iov[num].iov_len = sizeof(struct create_durable_v2);
1461 if (!req->CreateContextsOffset)
1462 req->CreateContextsOffset =
1463 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1464 iov[1].iov_len);
1465 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1466 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1467 *num_iovec = num + 1;
1468 return 0;
1469}
1470
1471static int
1472add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1473 struct cifs_open_parms *oparms)
1474{
1475 struct smb2_create_req *req = iov[0].iov_base;
1476 unsigned int num = *num_iovec;
1477
1478 /* indicate that we don't need to relock the file */
1479 oparms->reconnect = false;
1480
1481 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1482 if (iov[num].iov_base == NULL)
1483 return -ENOMEM;
1484 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1485 if (!req->CreateContextsOffset)
1486 req->CreateContextsOffset =
1487 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1488 iov[1].iov_len);
1489 le32_add_cpu(&req->CreateContextsLength,
1490 sizeof(struct create_durable_handle_reconnect_v2));
1491 inc_rfc1001_len(&req->hdr,
1492 sizeof(struct create_durable_handle_reconnect_v2));
1493 *num_iovec = num + 1;
1494 return 0;
1495}
1496
1497static int
1498add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1499 struct cifs_open_parms *oparms, bool use_persistent)
1500{
1501 struct smb2_create_req *req = iov[0].iov_base;
1502 unsigned int num = *num_iovec;
1503
1504 if (use_persistent) {
1505 if (oparms->reconnect)
1506 return add_durable_reconnect_v2_context(iov, num_iovec,
1507 oparms);
1508 else
1509 return add_durable_v2_context(iov, num_iovec, oparms);
1510 }
1511
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001512 if (oparms->reconnect) {
1513 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1514 /* indicate that we don't need to relock the file */
1515 oparms->reconnect = false;
1516 } else
1517 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001518 if (iov[num].iov_base == NULL)
1519 return -ENOMEM;
1520 iov[num].iov_len = sizeof(struct create_durable);
1521 if (!req->CreateContextsOffset)
1522 req->CreateContextsOffset =
1523 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1524 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001525 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001526 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1527 *num_iovec = num + 1;
1528 return 0;
1529}
1530
Aurelien Aptelf0712922017-02-22 14:47:17 +01001531static int
1532alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1533 const char *treename, const __le16 *path)
1534{
1535 int treename_len, path_len;
1536 struct nls_table *cp;
1537 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1538
1539 /*
1540 * skip leading "\\"
1541 */
1542 treename_len = strlen(treename);
1543 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1544 return -EINVAL;
1545
1546 treename += 2;
1547 treename_len -= 2;
1548
1549 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1550
1551 /*
1552 * make room for one path separator between the treename and
1553 * path
1554 */
1555 *out_len = treename_len + 1 + path_len;
1556
1557 /*
1558 * final path needs to be null-terminated UTF16 with a
1559 * size aligned to 8
1560 */
1561
1562 *out_size = roundup((*out_len+1)*2, 8);
1563 *out_path = kzalloc(*out_size, GFP_KERNEL);
1564 if (!*out_path)
1565 return -ENOMEM;
1566
1567 cp = load_nls_default();
1568 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1569 UniStrcat(*out_path, sep);
1570 UniStrcat(*out_path, path);
1571 unload_nls(cp);
1572
1573 return 0;
1574}
1575
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001576int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001577SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001578 __u8 *oplock, struct smb2_file_all_info *buf,
1579 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001580{
1581 struct smb2_create_req *req;
1582 struct smb2_create_rsp *rsp;
1583 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001584 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001585 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001586 struct kvec iov[4];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001587 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001588 int resp_buftype;
1589 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001590 __le16 *copy_path = NULL;
1591 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001592 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001593 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001594 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001595 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001596 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001597
Joe Perchesf96637b2013-05-04 22:12:25 -05001598 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001599
1600 if (ses && (ses->server))
1601 server = ses->server;
1602 else
1603 return -EIO;
1604
1605 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1606 if (rc)
1607 return rc;
1608
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001609 if (encryption_required(tcon))
1610 flags |= CIFS_TRANSFORM_REQ;
1611
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001612 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001613 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001614 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1615 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001616
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001617 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001618 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001619 /* File attributes ignored on open (used in create though) */
1620 req->FileAttributes = cpu_to_le32(file_attributes);
1621 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001622 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1623 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001624
1625 iov[0].iov_base = (char *)req;
1626 /* 4 for rfc1002 length field */
1627 iov[0].iov_len = get_rfc1002_length(req) + 4;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001628 /* -1 since last byte is buf[0] which is sent below (path) */
1629 iov[0].iov_len--;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001630
Aurelien Aptelf0712922017-02-22 14:47:17 +01001631 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1632
1633 /* [MS-SMB2] 2.2.13 NameOffset:
1634 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1635 * the SMB2 header, the file name includes a prefix that will
1636 * be processed during DFS name normalization as specified in
1637 * section 3.3.5.9. Otherwise, the file name is relative to
1638 * the share that is identified by the TreeId in the SMB2
1639 * header.
1640 */
1641 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1642 int name_len;
1643
1644 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1645 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1646 &name_len,
1647 tcon->treeName, path);
1648 if (rc)
1649 return rc;
1650 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001651 uni_path_len = copy_size;
1652 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001653 } else {
1654 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1655 /* MUST set path len (NameLength) to 0 opening root of share */
1656 req->NameLength = cpu_to_le16(uni_path_len - 2);
1657 if (uni_path_len % 8 != 0) {
1658 copy_size = roundup(uni_path_len, 8);
1659 copy_path = kzalloc(copy_size, GFP_KERNEL);
1660 if (!copy_path)
1661 return -ENOMEM;
1662 memcpy((char *)copy_path, (const char *)path,
1663 uni_path_len);
1664 uni_path_len = copy_size;
1665 path = copy_path;
1666 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001667 }
1668
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001669 iov[1].iov_len = uni_path_len;
1670 iov[1].iov_base = path;
1671 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1672 inc_rfc1001_len(req, uni_path_len - 1);
1673
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001674 if (!server->oplocks)
1675 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1676
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001677 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001678 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1679 req->RequestedOplockLevel = *oplock;
1680 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001681 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001682 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001683 cifs_small_buf_release(req);
1684 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001685 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001686 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001687 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001688 }
1689
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001690 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1691 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001692 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001693 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001694 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001695 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001696 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001697 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001698
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001699 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001700 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001701 if (rc) {
1702 cifs_small_buf_release(req);
1703 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001704 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001705 return rc;
1706 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001707 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001708 }
1709
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001710 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001711 cifs_small_buf_release(req);
1712 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001713
1714 if (rc != 0) {
1715 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001716 if (err_buf)
1717 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1718 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001719 goto creat_exit;
1720 }
1721
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001722 oparms->fid->persistent_fid = rsp->PersistentFileId;
1723 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001724
1725 if (buf) {
1726 memcpy(buf, &rsp->CreationTime, 32);
1727 buf->AllocationSize = rsp->AllocationSize;
1728 buf->EndOfFile = rsp->EndofFile;
1729 buf->Attributes = rsp->FileAttributes;
1730 buf->NumberOfLinks = cpu_to_le32(1);
1731 buf->DeletePending = 0;
1732 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001733
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001734 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001735 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001736 else
1737 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001738creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001739 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001740 kfree(lc_buf);
1741 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001742 free_rsp_buf(resp_buftype, rsp);
1743 return rc;
1744}
1745
Steve French4a72daf2013-06-25 00:20:49 -05001746/*
1747 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1748 */
1749int
1750SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +01001751 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1752 char *in_data, u32 indatalen,
1753 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001754{
1755 struct smb2_ioctl_req *req;
1756 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001757 struct smb2_sync_hdr *shdr;
Steve French4a72daf2013-06-25 00:20:49 -05001758 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001759 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001760 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001761 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001762 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001763 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001764 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001765 int flags = 0;
Steve French4a72daf2013-06-25 00:20:49 -05001766
1767 cifs_dbg(FYI, "SMB2 IOCTL\n");
1768
Steve French3d1a3742014-08-11 21:05:25 -05001769 if (out_data != NULL)
1770 *out_data = NULL;
1771
Steve French4a72daf2013-06-25 00:20:49 -05001772 /* zero out returned data len, in case of error */
1773 if (plen)
1774 *plen = 0;
1775
Steve French8e353102015-03-26 19:47:02 -05001776 if (tcon)
1777 ses = tcon->ses;
1778 else
1779 return -EIO;
1780
Steve French4a72daf2013-06-25 00:20:49 -05001781 if (ses && (ses->server))
1782 server = ses->server;
1783 else
1784 return -EIO;
1785
1786 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1787 if (rc)
1788 return rc;
1789
Aurelien Aptel51146622017-02-28 15:08:41 +01001790 if (use_ipc) {
1791 if (ses->ipc_tid == 0) {
1792 cifs_small_buf_release(req);
1793 return -ENOTCONN;
1794 }
1795
1796 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
1797 req->hdr.sync_hdr.TreeId, ses->ipc_tid);
1798 req->hdr.sync_hdr.TreeId = ses->ipc_tid;
1799 }
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001800 if (encryption_required(tcon))
1801 flags |= CIFS_TRANSFORM_REQ;
1802
Steve French4a72daf2013-06-25 00:20:49 -05001803 req->CtlCode = cpu_to_le32(opcode);
1804 req->PersistentFileId = persistent_fid;
1805 req->VolatileFileId = volatile_fid;
1806
1807 if (indatalen) {
1808 req->InputCount = cpu_to_le32(indatalen);
1809 /* do not set InputOffset if no input data */
1810 req->InputOffset =
1811 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1812 iov[1].iov_base = in_data;
1813 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001814 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001815 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001816 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001817
1818 req->OutputOffset = 0;
1819 req->OutputCount = 0; /* MBZ */
1820
1821 /*
1822 * Could increase MaxOutputResponse, but that would require more
1823 * than one credit. Windows typically sets this smaller, but for some
1824 * ioctls it may be useful to allow server to send more. No point
1825 * limiting what the server can send as long as fits in one credit
1826 */
1827 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1828
1829 if (is_fsctl)
1830 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1831 else
1832 req->Flags = 0;
1833
1834 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001835
Steve French7ff8d452013-10-14 00:44:19 -05001836 /*
1837 * If no input data, the size of ioctl struct in
1838 * protocol spec still includes a 1 byte data buffer,
1839 * but if input data passed to ioctl, we do not
1840 * want to double count this, so we do not send
1841 * the dummy one byte of data in iovec[0] if sending
1842 * input data (in iovec[1]). We also must add 4 bytes
1843 * in first iovec to allow for rfc1002 length field.
1844 */
1845
1846 if (indatalen) {
1847 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1848 inc_rfc1001_len(req, indatalen - 1);
1849 } else
1850 iov[0].iov_len = get_rfc1002_length(req) + 4;
1851
Steve French4a72daf2013-06-25 00:20:49 -05001852
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001853 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001854 cifs_small_buf_release(req);
1855 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05001856
Steve French9bf0c9c2013-11-16 18:05:28 -06001857 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001858 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001859 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001860 } else if (rc == -EINVAL) {
1861 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1862 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001863 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001864 goto ioctl_exit;
1865 }
Steve French4a72daf2013-06-25 00:20:49 -05001866 }
1867
1868 /* check if caller wants to look at return data or just return rc */
1869 if ((plen == NULL) || (out_data == NULL))
1870 goto ioctl_exit;
1871
1872 *plen = le32_to_cpu(rsp->OutputCount);
1873
1874 /* We check for obvious errors in the output buffer length and offset */
1875 if (*plen == 0)
1876 goto ioctl_exit; /* server returned no data */
1877 else if (*plen > 0xFF00) {
1878 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1879 *plen = 0;
1880 rc = -EIO;
1881 goto ioctl_exit;
1882 }
1883
1884 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1885 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1886 le32_to_cpu(rsp->OutputOffset));
1887 *plen = 0;
1888 rc = -EIO;
1889 goto ioctl_exit;
1890 }
1891
1892 *out_data = kmalloc(*plen, GFP_KERNEL);
1893 if (*out_data == NULL) {
1894 rc = -ENOMEM;
1895 goto ioctl_exit;
1896 }
1897
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001898 shdr = get_sync_hdr(rsp);
1899 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05001900ioctl_exit:
1901 free_rsp_buf(resp_buftype, rsp);
1902 return rc;
1903}
1904
Steve French64a5cfa2013-10-14 15:31:32 -05001905/*
1906 * Individual callers to ioctl worker function follow
1907 */
1908
1909int
1910SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1911 u64 persistent_fid, u64 volatile_fid)
1912{
1913 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001914 struct compress_ioctl fsctl_input;
1915 char *ret_data = NULL;
1916
1917 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001918 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001919
1920 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1921 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001922 false /* use_ipc */,
Steve French64a5cfa2013-10-14 15:31:32 -05001923 (char *)&fsctl_input /* data input */,
1924 2 /* in data len */, &ret_data /* out data */, NULL);
1925
1926 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001927
1928 return rc;
1929}
1930
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001931int
1932SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1933 u64 persistent_fid, u64 volatile_fid)
1934{
1935 struct smb2_close_req *req;
1936 struct smb2_close_rsp *rsp;
1937 struct TCP_Server_Info *server;
1938 struct cifs_ses *ses = tcon->ses;
1939 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001940 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001941 int resp_buftype;
1942 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001943 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001944
Joe Perchesf96637b2013-05-04 22:12:25 -05001945 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001946
1947 if (ses && (ses->server))
1948 server = ses->server;
1949 else
1950 return -EIO;
1951
1952 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1953 if (rc)
1954 return rc;
1955
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001956 if (encryption_required(tcon))
1957 flags |= CIFS_TRANSFORM_REQ;
1958
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001959 req->PersistentFileId = persistent_fid;
1960 req->VolatileFileId = volatile_fid;
1961
1962 iov[0].iov_base = (char *)req;
1963 /* 4 for rfc1002 length field */
1964 iov[0].iov_len = get_rfc1002_length(req) + 4;
1965
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001966 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001967 cifs_small_buf_release(req);
1968 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001969
1970 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001971 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001972 goto close_exit;
1973 }
1974
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001975 /* BB FIXME - decode close response, update inode for caching */
1976
1977close_exit:
1978 free_rsp_buf(resp_buftype, rsp);
1979 return rc;
1980}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001981
1982static int
1983validate_buf(unsigned int offset, unsigned int buffer_length,
1984 struct smb2_hdr *hdr, unsigned int min_buf_size)
1985
1986{
1987 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1988 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1989 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1990 char *end_of_buf = begin_of_buf + buffer_length;
1991
1992
1993 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001994 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1995 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001996 return -EINVAL;
1997 }
1998
1999 /* check if beyond RFC1001 maximum length */
2000 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002001 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2002 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002003 return -EINVAL;
2004 }
2005
2006 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002007 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002008 return -EINVAL;
2009 }
2010
2011 return 0;
2012}
2013
2014/*
2015 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2016 * Caller must free buffer.
2017 */
2018static int
2019validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2020 struct smb2_hdr *hdr, unsigned int minbufsize,
2021 char *data)
2022
2023{
2024 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2025 int rc;
2026
2027 if (!data)
2028 return -EINVAL;
2029
2030 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2031 if (rc)
2032 return rc;
2033
2034 memcpy(data, begin_of_buf, buffer_length);
2035
2036 return 0;
2037}
2038
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002039static int
2040query_info(const unsigned int xid, struct cifs_tcon *tcon,
2041 u64 persistent_fid, u64 volatile_fid, u8 info_class,
2042 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002043{
2044 struct smb2_query_info_req *req;
2045 struct smb2_query_info_rsp *rsp = NULL;
2046 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002047 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002048 int rc = 0;
2049 int resp_buftype;
2050 struct TCP_Server_Info *server;
2051 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002052 int flags = 0;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002053
Joe Perchesf96637b2013-05-04 22:12:25 -05002054 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002055
2056 if (ses && (ses->server))
2057 server = ses->server;
2058 else
2059 return -EIO;
2060
2061 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2062 if (rc)
2063 return rc;
2064
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002065 if (encryption_required(tcon))
2066 flags |= CIFS_TRANSFORM_REQ;
2067
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002068 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002069 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002070 req->PersistentFileId = persistent_fid;
2071 req->VolatileFileId = volatile_fid;
2072 /* 4 for rfc1002 length field and 1 for Buffer */
2073 req->InputBufferOffset =
2074 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002075 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002076
2077 iov[0].iov_base = (char *)req;
2078 /* 4 for rfc1002 length field */
2079 iov[0].iov_len = get_rfc1002_length(req) + 4;
2080
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002081 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002082 cifs_small_buf_release(req);
2083 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002084
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002085 if (rc) {
2086 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2087 goto qinf_exit;
2088 }
2089
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002090 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2091 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002092 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002093
2094qinf_exit:
2095 free_rsp_buf(resp_buftype, rsp);
2096 return rc;
2097}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002098
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002099int
2100SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2101 u64 persistent_fid, u64 volatile_fid,
2102 struct smb2_file_all_info *data)
2103{
2104 return query_info(xid, tcon, persistent_fid, volatile_fid,
2105 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002106 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002107 sizeof(struct smb2_file_all_info), data);
2108}
2109
2110int
2111SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2112 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2113{
2114 return query_info(xid, tcon, persistent_fid, volatile_fid,
2115 FILE_INTERNAL_INFORMATION,
2116 sizeof(struct smb2_file_internal_info),
2117 sizeof(struct smb2_file_internal_info), uniqueid);
2118}
2119
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002120/*
2121 * This is a no-op for now. We're not really interested in the reply, but
2122 * rather in the fact that the server sent one and that server->lstrp
2123 * gets updated.
2124 *
2125 * FIXME: maybe we should consider checking that the reply matches request?
2126 */
2127static void
2128smb2_echo_callback(struct mid_q_entry *mid)
2129{
2130 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002131 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002132 unsigned int credits_received = 1;
2133
2134 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002135 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002136
Christopher Oo5fb4e282015-06-25 16:10:48 -07002137 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002138 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002139 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002140 add_credits(server, credits_received, CIFS_ECHO_OP);
2141}
2142
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002143void smb2_reconnect_server(struct work_struct *work)
2144{
2145 struct TCP_Server_Info *server = container_of(work,
2146 struct TCP_Server_Info, reconnect.work);
2147 struct cifs_ses *ses;
2148 struct cifs_tcon *tcon, *tcon2;
2149 struct list_head tmp_list;
2150 int tcon_exist = false;
2151
2152 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2153 mutex_lock(&server->reconnect_mutex);
2154
2155 INIT_LIST_HEAD(&tmp_list);
2156 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2157
2158 spin_lock(&cifs_tcp_ses_lock);
2159 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2160 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002161 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002162 tcon->tc_count++;
2163 list_add_tail(&tcon->rlist, &tmp_list);
2164 tcon_exist = true;
2165 }
2166 }
2167 }
2168 /*
2169 * Get the reference to server struct to be sure that the last call of
2170 * cifs_put_tcon() in the loop below won't release the server pointer.
2171 */
2172 if (tcon_exist)
2173 server->srv_count++;
2174
2175 spin_unlock(&cifs_tcp_ses_lock);
2176
2177 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002178 if (!smb2_reconnect(SMB2_INTERNAL_CMD, tcon))
2179 cifs_reopen_persistent_handles(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002180 list_del_init(&tcon->rlist);
2181 cifs_put_tcon(tcon);
2182 }
2183
2184 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2185 mutex_unlock(&server->reconnect_mutex);
2186
2187 /* now we can safely release srv struct */
2188 if (tcon_exist)
2189 cifs_put_tcp_session(server, 1);
2190}
2191
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002192int
2193SMB2_echo(struct TCP_Server_Info *server)
2194{
2195 struct smb2_echo_req *req;
2196 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002197 struct kvec iov[2];
2198 struct smb_rqst rqst = { .rq_iov = iov,
2199 .rq_nvec = 2 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002200
Joe Perchesf96637b2013-05-04 22:12:25 -05002201 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002202
Steve French4fcd1812016-06-22 20:12:05 -05002203 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002204 /* No need to send echo on newly established connections */
2205 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2206 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002207 }
2208
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002209 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2210 if (rc)
2211 return rc;
2212
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002213 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002214
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002215 /* 4 for rfc1002 length field */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002216 iov[0].iov_len = 4;
2217 iov[0].iov_base = (char *)req;
2218 iov[1].iov_len = get_rfc1002_length(req);
2219 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002220
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002221 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2222 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002223 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002224 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002225
2226 cifs_small_buf_release(req);
2227 return rc;
2228}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002229
2230int
2231SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2232 u64 volatile_fid)
2233{
2234 struct smb2_flush_req *req;
2235 struct TCP_Server_Info *server;
2236 struct cifs_ses *ses = tcon->ses;
2237 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002238 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002239 int resp_buftype;
2240 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002241 int flags = 0;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002242
Joe Perchesf96637b2013-05-04 22:12:25 -05002243 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002244
2245 if (ses && (ses->server))
2246 server = ses->server;
2247 else
2248 return -EIO;
2249
2250 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2251 if (rc)
2252 return rc;
2253
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002254 if (encryption_required(tcon))
2255 flags |= CIFS_TRANSFORM_REQ;
2256
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002257 req->PersistentFileId = persistent_fid;
2258 req->VolatileFileId = volatile_fid;
2259
2260 iov[0].iov_base = (char *)req;
2261 /* 4 for rfc1002 length field */
2262 iov[0].iov_len = get_rfc1002_length(req) + 4;
2263
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002264 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002265 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002266
Steve Frenchdfebe402015-03-27 01:00:06 -05002267 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002268 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2269
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002270 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002271 return rc;
2272}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002273
2274/*
2275 * To form a chain of read requests, any read requests after the first should
2276 * have the end_of_chain boolean set to true.
2277 */
2278static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002279smb2_new_read_req(void **buf, unsigned int *total_len,
2280 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2281 int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002282{
2283 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002284 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002285 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002286
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002287 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2288 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002289 if (rc)
2290 return rc;
2291 if (io_parms->tcon->ses->server == NULL)
2292 return -ECONNABORTED;
2293
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002294 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002295 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002296
2297 req->PersistentFileId = io_parms->persistent_fid;
2298 req->VolatileFileId = io_parms->volatile_fid;
2299 req->ReadChannelInfoOffset = 0; /* reserved */
2300 req->ReadChannelInfoLength = 0; /* reserved */
2301 req->Channel = 0; /* reserved */
2302 req->MinimumCount = 0;
2303 req->Length = cpu_to_le32(io_parms->length);
2304 req->Offset = cpu_to_le64(io_parms->offset);
2305
2306 if (request_type & CHAINED_REQUEST) {
2307 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002308 /* next 8-byte aligned request */
2309 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2310 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002311 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002312 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002313 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002314 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002315 /*
2316 * Related requests use info from previous read request
2317 * in chain.
2318 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002319 shdr->SessionId = 0xFFFFFFFF;
2320 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002321 req->PersistentFileId = 0xFFFFFFFF;
2322 req->VolatileFileId = 0xFFFFFFFF;
2323 }
2324 }
2325 if (remaining_bytes > io_parms->length)
2326 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2327 else
2328 req->RemainingBytes = 0;
2329
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002330 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002331 return rc;
2332}
2333
2334static void
2335smb2_readv_callback(struct mid_q_entry *mid)
2336{
2337 struct cifs_readdata *rdata = mid->callback_data;
2338 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2339 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002340 struct smb2_sync_hdr *shdr =
2341 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002342 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002343 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2344 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002345 .rq_pages = rdata->pages,
2346 .rq_npages = rdata->nr_pages,
2347 .rq_pagesz = rdata->pagesz,
2348 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002349
Joe Perchesf96637b2013-05-04 22:12:25 -05002350 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2351 __func__, mid->mid, mid->mid_state, rdata->result,
2352 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002353
2354 switch (mid->mid_state) {
2355 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002356 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002357 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002358 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002359 int rc;
2360
Jeff Layton0b688cf2012-09-18 16:20:34 -07002361 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002362 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002363 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2364 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002365 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002366 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002367 task_io_account_read(rdata->got_bytes);
2368 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002369 break;
2370 case MID_REQUEST_SUBMITTED:
2371 case MID_RETRY_NEEDED:
2372 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002373 if (server->sign && rdata->got_bytes)
2374 /* reset bytes number since we can not check a sign */
2375 rdata->got_bytes = 0;
2376 /* FIXME: should this be counted toward the initiating task? */
2377 task_io_account_read(rdata->got_bytes);
2378 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002379 break;
2380 default:
2381 if (rdata->result != -ENODATA)
2382 rdata->result = -EIO;
2383 }
2384
2385 if (rdata->result)
2386 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2387
2388 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002389 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002390 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002391 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002392 add_credits(server, credits_received, 0);
2393}
2394
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002395/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002396int
2397smb2_async_readv(struct cifs_readdata *rdata)
2398{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002399 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002400 char *buf;
2401 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002402 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002403 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2404 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002405 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002406 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002407 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002408
Joe Perchesf96637b2013-05-04 22:12:25 -05002409 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2410 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002411
2412 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2413 io_parms.offset = rdata->offset;
2414 io_parms.length = rdata->bytes;
2415 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2416 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2417 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002418
2419 server = io_parms.tcon->ses->server;
2420
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002421 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002422 if (rc) {
2423 if (rc == -EAGAIN && rdata->credits) {
2424 /* credits was reset by reconnect */
2425 rdata->credits = 0;
2426 /* reduce in_flight value since we won't send the req */
2427 spin_lock(&server->req_lock);
2428 server->in_flight--;
2429 spin_unlock(&server->req_lock);
2430 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002431 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002432 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002433
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002434 if (encryption_required(io_parms.tcon))
2435 flags |= CIFS_TRANSFORM_REQ;
2436
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002437 req_len = cpu_to_be32(total_len);
2438
2439 rdata->iov[0].iov_base = &req_len;
2440 rdata->iov[0].iov_len = sizeof(__be32);
2441 rdata->iov[1].iov_base = buf;
2442 rdata->iov[1].iov_len = total_len;
2443
2444 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002445
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002446 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002447 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002448 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002449 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002450 spin_lock(&server->req_lock);
2451 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002452 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002453 spin_unlock(&server->req_lock);
2454 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002455 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002456 }
2457
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002458 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002459 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002460 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002461 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002462 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002463 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002464 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2465 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002466
2467 cifs_small_buf_release(buf);
2468 return rc;
2469}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002470
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002471int
2472SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2473 unsigned int *nbytes, char **buf, int *buf_type)
2474{
2475 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002476 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002477 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002478 struct smb2_sync_hdr *shdr;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002479 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002480 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002481 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002482 __be32 req_len;
2483 struct smb_rqst rqst = { .rq_iov = iov,
2484 .rq_nvec = 2 };
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002485 int flags = CIFS_LOG_ERROR;
2486 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002487
2488 *nbytes = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002489 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002490 if (rc)
2491 return rc;
2492
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002493 if (encryption_required(io_parms->tcon))
2494 flags |= CIFS_TRANSFORM_REQ;
2495
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002496 req_len = cpu_to_be32(total_len);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002497
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002498 iov[0].iov_base = &req_len;
2499 iov[0].iov_len = sizeof(__be32);
2500 iov[1].iov_base = req;
2501 iov[1].iov_len = total_len;
2502
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002503 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002504 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002505
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002506 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002507 shdr = get_sync_hdr(rsp);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002508
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002509 if (shdr->Status == STATUS_END_OF_FILE) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002510 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002511 return 0;
2512 }
2513
2514 if (rc) {
2515 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002516 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002517 } else {
2518 *nbytes = le32_to_cpu(rsp->DataLength);
2519 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2520 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002521 cifs_dbg(FYI, "bad length %d for count %d\n",
2522 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002523 rc = -EIO;
2524 *nbytes = 0;
2525 }
2526 }
2527
2528 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002529 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002530 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002531 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002532 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002533 if (resp_buftype == CIFS_SMALL_BUFFER)
2534 *buf_type = CIFS_SMALL_BUFFER;
2535 else if (resp_buftype == CIFS_LARGE_BUFFER)
2536 *buf_type = CIFS_LARGE_BUFFER;
2537 }
2538 return rc;
2539}
2540
Pavel Shilovsky33319142012-09-18 16:20:29 -07002541/*
2542 * Check the mid_state and signature on received buffer (if any), and queue the
2543 * workqueue completion task.
2544 */
2545static void
2546smb2_writev_callback(struct mid_q_entry *mid)
2547{
2548 struct cifs_writedata *wdata = mid->callback_data;
2549 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002550 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002551 unsigned int written;
2552 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2553 unsigned int credits_received = 1;
2554
2555 switch (mid->mid_state) {
2556 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002557 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002558 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2559 if (wdata->result != 0)
2560 break;
2561
2562 written = le32_to_cpu(rsp->DataLength);
2563 /*
2564 * Mask off high 16 bits when bytes written as returned
2565 * by the server is greater than bytes requested by the
2566 * client. OS/2 servers are known to set incorrect
2567 * CountHigh values.
2568 */
2569 if (written > wdata->bytes)
2570 written &= 0xFFFF;
2571
2572 if (written < wdata->bytes)
2573 wdata->result = -ENOSPC;
2574 else
2575 wdata->bytes = written;
2576 break;
2577 case MID_REQUEST_SUBMITTED:
2578 case MID_RETRY_NEEDED:
2579 wdata->result = -EAGAIN;
2580 break;
2581 default:
2582 wdata->result = -EIO;
2583 break;
2584 }
2585
2586 if (wdata->result)
2587 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2588
2589 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002590 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002591 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002592 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002593 add_credits(tcon->ses->server, credits_received, 0);
2594}
2595
2596/* smb2_async_writev - send an async write, and set up mid to handle result */
2597int
Steve French4a5c80d2014-02-07 20:45:12 -06002598smb2_async_writev(struct cifs_writedata *wdata,
2599 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002600{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002601 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002602 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002603 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002604 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002605 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002606 struct kvec iov[2];
2607 struct smb_rqst rqst = { };
Pavel Shilovsky33319142012-09-18 16:20:29 -07002608
2609 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002610 if (rc) {
2611 if (rc == -EAGAIN && wdata->credits) {
2612 /* credits was reset by reconnect */
2613 wdata->credits = 0;
2614 /* reduce in_flight value since we won't send the req */
2615 spin_lock(&server->req_lock);
2616 server->in_flight--;
2617 spin_unlock(&server->req_lock);
2618 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002619 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002620 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002621
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002622 if (encryption_required(tcon))
2623 flags |= CIFS_TRANSFORM_REQ;
2624
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002625 shdr = get_sync_hdr(req);
2626 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002627
2628 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2629 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2630 req->WriteChannelInfoOffset = 0;
2631 req->WriteChannelInfoLength = 0;
2632 req->Channel = 0;
2633 req->Offset = cpu_to_le64(wdata->offset);
2634 /* 4 for rfc1002 length field */
2635 req->DataOffset = cpu_to_le16(
2636 offsetof(struct smb2_write_req, Buffer) - 4);
2637 req->RemainingBytes = 0;
2638
2639 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002640 iov[0].iov_len = 4;
2641 iov[0].iov_base = req;
2642 iov[1].iov_len = get_rfc1002_length(req) - 1;
2643 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002644
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002645 rqst.rq_iov = iov;
2646 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002647 rqst.rq_pages = wdata->pages;
2648 rqst.rq_npages = wdata->nr_pages;
2649 rqst.rq_pagesz = wdata->pagesz;
2650 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002651
Joe Perchesf96637b2013-05-04 22:12:25 -05002652 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2653 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002654
2655 req->Length = cpu_to_le32(wdata->bytes);
2656
2657 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2658
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002659 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002660 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002661 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002662 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002663 spin_lock(&server->req_lock);
2664 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002665 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002666 spin_unlock(&server->req_lock);
2667 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002668 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002669 }
2670
Pavel Shilovsky33319142012-09-18 16:20:29 -07002671 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002672 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2673 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002674
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002675 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002676 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002677 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2678 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002679
Pavel Shilovsky33319142012-09-18 16:20:29 -07002680async_writev_out:
2681 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002682 return rc;
2683}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002684
2685/*
2686 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2687 * The length field from io_parms must be at least 1 and indicates a number of
2688 * elements with data to write that begins with position 1 in iov array. All
2689 * data length is specified by count.
2690 */
2691int
2692SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2693 unsigned int *nbytes, struct kvec *iov, int n_vec)
2694{
2695 int rc = 0;
2696 struct smb2_write_req *req = NULL;
2697 struct smb2_write_rsp *rsp = NULL;
2698 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002699 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002700 int flags = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002701
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002702 *nbytes = 0;
2703
2704 if (n_vec < 1)
2705 return rc;
2706
2707 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2708 if (rc)
2709 return rc;
2710
2711 if (io_parms->tcon->ses->server == NULL)
2712 return -ECONNABORTED;
2713
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002714 if (encryption_required(io_parms->tcon))
2715 flags |= CIFS_TRANSFORM_REQ;
2716
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002717 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002718
2719 req->PersistentFileId = io_parms->persistent_fid;
2720 req->VolatileFileId = io_parms->volatile_fid;
2721 req->WriteChannelInfoOffset = 0;
2722 req->WriteChannelInfoLength = 0;
2723 req->Channel = 0;
2724 req->Length = cpu_to_le32(io_parms->length);
2725 req->Offset = cpu_to_le64(io_parms->offset);
2726 /* 4 for rfc1002 length field */
2727 req->DataOffset = cpu_to_le16(
2728 offsetof(struct smb2_write_req, Buffer) - 4);
2729 req->RemainingBytes = 0;
2730
2731 iov[0].iov_base = (char *)req;
2732 /* 4 for rfc1002 length field and 1 for Buffer */
2733 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2734
2735 /* length of entire message including data to be written */
2736 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2737
2738 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002739 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002740 cifs_small_buf_release(req);
2741 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002742
2743 if (rc) {
2744 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002745 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002746 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002747 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002748
2749 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002750 return rc;
2751}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002752
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002753static unsigned int
2754num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2755{
2756 int len;
2757 unsigned int entrycount = 0;
2758 unsigned int next_offset = 0;
2759 FILE_DIRECTORY_INFO *entryptr;
2760
2761 if (bufstart == NULL)
2762 return 0;
2763
2764 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2765
2766 while (1) {
2767 entryptr = (FILE_DIRECTORY_INFO *)
2768 ((char *)entryptr + next_offset);
2769
2770 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002771 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002772 break;
2773 }
2774
2775 len = le32_to_cpu(entryptr->FileNameLength);
2776 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002777 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2778 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002779 break;
2780 }
2781
2782 *lastentry = (char *)entryptr;
2783 entrycount++;
2784
2785 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2786 if (!next_offset)
2787 break;
2788 }
2789
2790 return entrycount;
2791}
2792
2793/*
2794 * Readdir/FindFirst
2795 */
2796int
2797SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2798 u64 persistent_fid, u64 volatile_fid, int index,
2799 struct cifs_search_info *srch_inf)
2800{
2801 struct smb2_query_directory_req *req;
2802 struct smb2_query_directory_rsp *rsp = NULL;
2803 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002804 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002805 int rc = 0;
2806 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002807 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002808 unsigned char *bufptr;
2809 struct TCP_Server_Info *server;
2810 struct cifs_ses *ses = tcon->ses;
2811 __le16 asteriks = cpu_to_le16('*');
2812 char *end_of_smb;
2813 unsigned int output_size = CIFSMaxBufSize;
2814 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002815 int flags = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002816
2817 if (ses && (ses->server))
2818 server = ses->server;
2819 else
2820 return -EIO;
2821
2822 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2823 if (rc)
2824 return rc;
2825
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002826 if (encryption_required(tcon))
2827 flags |= CIFS_TRANSFORM_REQ;
2828
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002829 switch (srch_inf->info_level) {
2830 case SMB_FIND_FILE_DIRECTORY_INFO:
2831 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2832 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2833 break;
2834 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2835 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2836 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2837 break;
2838 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002839 cifs_dbg(VFS, "info level %u isn't supported\n",
2840 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002841 rc = -EINVAL;
2842 goto qdir_exit;
2843 }
2844
2845 req->FileIndex = cpu_to_le32(index);
2846 req->PersistentFileId = persistent_fid;
2847 req->VolatileFileId = volatile_fid;
2848
2849 len = 0x2;
2850 bufptr = req->Buffer;
2851 memcpy(bufptr, &asteriks, len);
2852
2853 req->FileNameOffset =
2854 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2855 req->FileNameLength = cpu_to_le16(len);
2856 /*
2857 * BB could be 30 bytes or so longer if we used SMB2 specific
2858 * buffer lengths, but this is safe and close enough.
2859 */
2860 output_size = min_t(unsigned int, output_size, server->maxBuf);
2861 output_size = min_t(unsigned int, output_size, 2 << 15);
2862 req->OutputBufferLength = cpu_to_le32(output_size);
2863
2864 iov[0].iov_base = (char *)req;
2865 /* 4 for RFC1001 length and 1 for Buffer */
2866 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2867
2868 iov[1].iov_base = (char *)(req->Buffer);
2869 iov[1].iov_len = len;
2870
2871 inc_rfc1001_len(req, len - 1 /* Buffer */);
2872
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002873 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002874 cifs_small_buf_release(req);
2875 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002876
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002877 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002878 if (rc == -ENODATA &&
2879 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002880 srch_inf->endOfSearch = true;
2881 rc = 0;
2882 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002883 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2884 goto qdir_exit;
2885 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002886
2887 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2888 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2889 info_buf_size);
2890 if (rc)
2891 goto qdir_exit;
2892
2893 srch_inf->unicode = true;
2894
2895 if (srch_inf->ntwrk_buf_start) {
2896 if (srch_inf->smallBuf)
2897 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2898 else
2899 cifs_buf_release(srch_inf->ntwrk_buf_start);
2900 }
2901 srch_inf->ntwrk_buf_start = (char *)rsp;
2902 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2903 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2904 /* 4 for rfc1002 length field */
2905 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2906 srch_inf->entries_in_buffer =
2907 num_entries(srch_inf->srch_entries_start, end_of_smb,
2908 &srch_inf->last_entry, info_buf_size);
2909 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002910 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2911 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2912 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002913 if (resp_buftype == CIFS_LARGE_BUFFER)
2914 srch_inf->smallBuf = false;
2915 else if (resp_buftype == CIFS_SMALL_BUFFER)
2916 srch_inf->smallBuf = true;
2917 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002918 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002919
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002920 return rc;
2921
2922qdir_exit:
2923 free_rsp_buf(resp_buftype, rsp);
2924 return rc;
2925}
2926
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002927static int
2928send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002929 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002930 unsigned int num, void **data, unsigned int *size)
2931{
2932 struct smb2_set_info_req *req;
2933 struct smb2_set_info_rsp *rsp = NULL;
2934 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002935 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002936 int rc = 0;
2937 int resp_buftype;
2938 unsigned int i;
2939 struct TCP_Server_Info *server;
2940 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002941 int flags = 0;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002942
2943 if (ses && (ses->server))
2944 server = ses->server;
2945 else
2946 return -EIO;
2947
2948 if (!num)
2949 return -EINVAL;
2950
2951 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2952 if (!iov)
2953 return -ENOMEM;
2954
2955 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2956 if (rc) {
2957 kfree(iov);
2958 return rc;
2959 }
2960
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002961 if (encryption_required(tcon))
2962 flags |= CIFS_TRANSFORM_REQ;
2963
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002964 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002965
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002966 req->InfoType = SMB2_O_INFO_FILE;
2967 req->FileInfoClass = info_class;
2968 req->PersistentFileId = persistent_fid;
2969 req->VolatileFileId = volatile_fid;
2970
2971 /* 4 for RFC1001 length and 1 for Buffer */
2972 req->BufferOffset =
2973 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2974 req->BufferLength = cpu_to_le32(*size);
2975
2976 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2977
2978 memcpy(req->Buffer, *data, *size);
2979
2980 iov[0].iov_base = (char *)req;
2981 /* 4 for RFC1001 length */
2982 iov[0].iov_len = get_rfc1002_length(req) + 4;
2983
2984 for (i = 1; i < num; i++) {
2985 inc_rfc1001_len(req, size[i]);
2986 le32_add_cpu(&req->BufferLength, size[i]);
2987 iov[i].iov_base = (char *)data[i];
2988 iov[i].iov_len = size[i];
2989 }
2990
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002991 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002992 cifs_small_buf_release(req);
2993 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002994
Steve French7d3fb242013-11-18 09:56:28 -06002995 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002996 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002997
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002998 free_rsp_buf(resp_buftype, rsp);
2999 kfree(iov);
3000 return rc;
3001}
3002
3003int
3004SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3005 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3006{
3007 struct smb2_file_rename_info info;
3008 void **data;
3009 unsigned int size[2];
3010 int rc;
3011 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3012
3013 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3014 if (!data)
3015 return -ENOMEM;
3016
3017 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3018 /* 0 = fail if target already exists */
3019 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3020 info.FileNameLength = cpu_to_le32(len);
3021
3022 data[0] = &info;
3023 size[0] = sizeof(struct smb2_file_rename_info);
3024
3025 data[1] = target_file;
3026 size[1] = len + 2 /* null */;
3027
3028 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003029 current->tgid, FILE_RENAME_INFORMATION, 2, data,
3030 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003031 kfree(data);
3032 return rc;
3033}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003034
3035int
Steve French897fba12016-05-12 21:20:36 -05003036SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3037 u64 persistent_fid, u64 volatile_fid)
3038{
3039 __u8 delete_pending = 1;
3040 void *data;
3041 unsigned int size;
3042
3043 data = &delete_pending;
3044 size = 1; /* sizeof __u8 */
3045
3046 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3047 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
3048 &size);
3049}
3050
3051int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003052SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3053 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3054{
3055 struct smb2_file_link_info info;
3056 void **data;
3057 unsigned int size[2];
3058 int rc;
3059 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3060
3061 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3062 if (!data)
3063 return -ENOMEM;
3064
3065 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3066 /* 0 = fail if link already exists */
3067 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3068 info.FileNameLength = cpu_to_le32(len);
3069
3070 data[0] = &info;
3071 size[0] = sizeof(struct smb2_file_link_info);
3072
3073 data[1] = target_file;
3074 size[1] = len + 2 /* null */;
3075
3076 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003077 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003078 kfree(data);
3079 return rc;
3080}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003081
3082int
3083SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003084 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003085{
3086 struct smb2_file_eof_info info;
3087 void *data;
3088 unsigned int size;
3089
3090 info.EndOfFile = *eof;
3091
3092 data = &info;
3093 size = sizeof(struct smb2_file_eof_info);
3094
Steve Frenchf29ebb42014-07-19 21:44:58 -05003095 if (is_falloc)
3096 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3097 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
3098 else
3099 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3100 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003101}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003102
3103int
3104SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3105 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3106{
3107 unsigned int size;
3108 size = sizeof(FILE_BASIC_INFO);
3109 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3110 current->tgid, FILE_BASIC_INFORMATION, 1,
3111 (void **)&buf, &size);
3112}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003113
3114int
3115SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3116 const u64 persistent_fid, const u64 volatile_fid,
3117 __u8 oplock_level)
3118{
3119 int rc;
3120 struct smb2_oplock_break *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003121 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003122
Joe Perchesf96637b2013-05-04 22:12:25 -05003123 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003124 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003125 if (rc)
3126 return rc;
3127
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003128 if (encryption_required(tcon))
3129 flags |= CIFS_TRANSFORM_REQ;
3130
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003131 req->VolatileFid = volatile_fid;
3132 req->PersistentFid = persistent_fid;
3133 req->OplockLevel = oplock_level;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003134 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003135
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003136 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003137 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003138
3139 if (rc) {
3140 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003141 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003142 }
3143
3144 return rc;
3145}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003146
3147static void
3148copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3149 struct kstatfs *kst)
3150{
3151 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3152 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3153 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3154 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
3155 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3156 return;
3157}
3158
3159static int
3160build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3161 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3162{
3163 int rc;
3164 struct smb2_query_info_req *req;
3165
Joe Perchesf96637b2013-05-04 22:12:25 -05003166 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003167
3168 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3169 return -EIO;
3170
3171 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3172 if (rc)
3173 return rc;
3174
3175 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3176 req->FileInfoClass = level;
3177 req->PersistentFileId = persistent_fid;
3178 req->VolatileFileId = volatile_fid;
3179 /* 4 for rfc1002 length field and 1 for pad */
3180 req->InputBufferOffset =
3181 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3182 req->OutputBufferLength = cpu_to_le32(
3183 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3184
3185 iov->iov_base = (char *)req;
3186 /* 4 for rfc1002 length field */
3187 iov->iov_len = get_rfc1002_length(req) + 4;
3188 return 0;
3189}
3190
3191int
3192SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3193 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3194{
3195 struct smb2_query_info_rsp *rsp = NULL;
3196 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003197 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003198 int rc = 0;
3199 int resp_buftype;
3200 struct cifs_ses *ses = tcon->ses;
3201 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003202 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003203
3204 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3205 sizeof(struct smb2_fs_full_size_info),
3206 persistent_fid, volatile_fid);
3207 if (rc)
3208 return rc;
3209
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003210 if (encryption_required(tcon))
3211 flags |= CIFS_TRANSFORM_REQ;
3212
3213 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003214 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003215 if (rc) {
3216 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003217 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003218 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003219 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003220
3221 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3222 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3223 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3224 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3225 sizeof(struct smb2_fs_full_size_info));
3226 if (!rc)
3227 copy_fs_info_to_kstatfs(info, fsdata);
3228
Steve French34f62642013-10-09 02:07:00 -05003229qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003230 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003231 return rc;
3232}
3233
3234int
3235SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003236 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003237{
3238 struct smb2_query_info_rsp *rsp = NULL;
3239 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003240 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003241 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003242 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003243 struct cifs_ses *ses = tcon->ses;
3244 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003245 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003246
Steven French21671142013-10-09 13:36:35 -05003247 if (level == FS_DEVICE_INFORMATION) {
3248 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3249 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3250 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3251 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3252 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003253 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3254 max_len = sizeof(struct smb3_fs_ss_info);
3255 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003256 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003257 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003258 return -EINVAL;
3259 }
3260
3261 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003262 persistent_fid, volatile_fid);
3263 if (rc)
3264 return rc;
3265
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003266 if (encryption_required(tcon))
3267 flags |= CIFS_TRANSFORM_REQ;
3268
3269 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003270 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003271 if (rc) {
3272 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3273 goto qfsattr_exit;
3274 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003275 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003276
3277 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3278 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003279 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3280 if (rc)
3281 goto qfsattr_exit;
3282
3283 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003284 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3285 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003286 rsp_len, max_len));
3287 else if (level == FS_DEVICE_INFORMATION)
3288 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3289 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003290 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3291 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3292 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3293 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3294 tcon->perf_sector_size =
3295 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3296 }
Steve French34f62642013-10-09 02:07:00 -05003297
3298qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003299 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003300 return rc;
3301}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003302
3303int
3304smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3305 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3306 const __u32 num_lock, struct smb2_lock_element *buf)
3307{
3308 int rc = 0;
3309 struct smb2_lock_req *req = NULL;
3310 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003311 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003312 int resp_buf_type;
3313 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003314 int flags = CIFS_NO_RESP;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003315
Joe Perchesf96637b2013-05-04 22:12:25 -05003316 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003317
3318 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3319 if (rc)
3320 return rc;
3321
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003322 if (encryption_required(tcon))
3323 flags |= CIFS_TRANSFORM_REQ;
3324
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003325 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003326 req->LockCount = cpu_to_le16(num_lock);
3327
3328 req->PersistentFileId = persist_fid;
3329 req->VolatileFileId = volatile_fid;
3330
3331 count = num_lock * sizeof(struct smb2_lock_element);
3332 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3333
3334 iov[0].iov_base = (char *)req;
3335 /* 4 for rfc1002 length field and count for all locks */
3336 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3337 iov[1].iov_base = (char *)buf;
3338 iov[1].iov_len = count;
3339
3340 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003341 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003342 &rsp_iov);
3343 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003344 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003345 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003346 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3347 }
3348
3349 return rc;
3350}
3351
3352int
3353SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3354 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3355 const __u64 length, const __u64 offset, const __u32 lock_flags,
3356 const bool wait)
3357{
3358 struct smb2_lock_element lock;
3359
3360 lock.Offset = cpu_to_le64(offset);
3361 lock.Length = cpu_to_le64(length);
3362 lock.Flags = cpu_to_le32(lock_flags);
3363 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3364 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3365
3366 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3367}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003368
3369int
3370SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3371 __u8 *lease_key, const __le32 lease_state)
3372{
3373 int rc;
3374 struct smb2_lease_ack *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003375 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003376
Joe Perchesf96637b2013-05-04 22:12:25 -05003377 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003378 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003379 if (rc)
3380 return rc;
3381
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003382 if (encryption_required(tcon))
3383 flags |= CIFS_TRANSFORM_REQ;
3384
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003385 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003386 req->StructureSize = cpu_to_le16(36);
3387 inc_rfc1001_len(req, 12);
3388
3389 memcpy(req->LeaseKey, lease_key, 16);
3390 req->LeaseState = lease_state;
3391
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003392 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003393 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003394
3395 if (rc) {
3396 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003397 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003398 }
3399
3400 return rc;
3401}