blob: 0f044c4a2dc9b5b625e9cae21d34344692b76d23 [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>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020036#include <linux/uuid.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070037#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040038#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070048#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070049#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050050#include "cifs_spnego.h"
Long Lidb223a52017-11-22 17:38:45 -070051#include "smbdirect.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040052
53/*
54 * The following table defines the expected "StructureSize" of SMB2 requests
55 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
56 *
57 * Note that commands are defined in smb2pdu.h in le16 but the array below is
58 * indexed by command in host byte order.
59 */
60static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
61 /* SMB2_NEGOTIATE */ 36,
62 /* SMB2_SESSION_SETUP */ 25,
63 /* SMB2_LOGOFF */ 4,
64 /* SMB2_TREE_CONNECT */ 9,
65 /* SMB2_TREE_DISCONNECT */ 4,
66 /* SMB2_CREATE */ 57,
67 /* SMB2_CLOSE */ 24,
68 /* SMB2_FLUSH */ 24,
69 /* SMB2_READ */ 49,
70 /* SMB2_WRITE */ 49,
71 /* SMB2_LOCK */ 48,
72 /* SMB2_IOCTL */ 57,
73 /* SMB2_CANCEL */ 4,
74 /* SMB2_ECHO */ 4,
75 /* SMB2_QUERY_DIRECTORY */ 33,
76 /* SMB2_CHANGE_NOTIFY */ 32,
77 /* SMB2_QUERY_INFO */ 41,
78 /* SMB2_SET_INFO */ 33,
79 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
80};
81
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070082static int encryption_required(const struct cifs_tcon *tcon)
83{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080084 if (!tcon)
85 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070086 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
87 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
88 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080089 if (tcon->seal &&
90 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
91 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070092 return 0;
93}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040094
95static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070096smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040097 const struct cifs_tcon *tcon)
98{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070099 shdr->ProtocolId = SMB2_PROTO_NUMBER;
100 shdr->StructureSize = cpu_to_le16(64);
101 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100102 if (tcon && tcon->ses && tcon->ses->server) {
103 struct TCP_Server_Info *server = tcon->ses->server;
104
105 spin_lock(&server->req_lock);
106 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500107 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700108 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100109 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700110 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500111 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100112 server->credits, 2));
113 spin_unlock(&server->req_lock);
114 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700115 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100116 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700117 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400118
119 if (!tcon)
120 goto out;
121
Steve French2b80d042013-06-23 18:43:37 -0500122 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
123 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500124 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500125 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700126 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500127 /* else CreditCharge MBZ */
128
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700129 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400130 /* Uid is not converted */
131 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700132 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500133
134 /*
135 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
136 * to pass the path on the Open SMB prefixed by \\server\share.
137 * Not sure when we would need to do the augmented path (if ever) and
138 * setting this flag breaks the SMB2 open operation since it is
139 * illegal to send an empty path name (without \\server\share prefix)
140 * when the DFS flag is set in the SMB open header. We could
141 * consider setting the flag on all operations other than open
142 * but it is safer to net set it for now.
143 */
144/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700145 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500146
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700147 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
148 !encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700149 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400150out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400151 return;
152}
153
154static int
155smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
156{
157 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400158 struct nls_table *nls_codepage;
159 struct cifs_ses *ses;
160 struct TCP_Server_Info *server;
161
162 /*
163 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
164 * check for tcp and smb session status done differently
165 * for those three - in the calling routine.
166 */
167 if (tcon == NULL)
168 return rc;
169
170 if (smb2_command == SMB2_TREE_CONNECT)
171 return rc;
172
173 if (tcon->tidStatus == CifsExiting) {
174 /*
175 * only tree disconnect, open, and write,
176 * (and ulogoff which does not have tcon)
177 * are allowed as we start force umount.
178 */
179 if ((smb2_command != SMB2_WRITE) &&
180 (smb2_command != SMB2_CREATE) &&
181 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500182 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
183 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400184 return -ENODEV;
185 }
186 }
187 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
188 (!tcon->ses->server))
189 return -EIO;
190
191 ses = tcon->ses;
192 server = ses->server;
193
194 /*
195 * Give demultiplex thread up to 10 seconds to reconnect, should be
196 * greater than cifs socket timeout which is 7 seconds
197 */
198 while (server->tcpStatus == CifsNeedReconnect) {
199 /*
200 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
201 * here since they are implicitly done when session drops.
202 */
203 switch (smb2_command) {
204 /*
205 * BB Should we keep oplock break and add flush to exceptions?
206 */
207 case SMB2_TREE_DISCONNECT:
208 case SMB2_CANCEL:
209 case SMB2_CLOSE:
210 case SMB2_OPLOCK_BREAK:
211 return -EAGAIN;
212 }
213
214 wait_event_interruptible_timeout(server->response_q,
215 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
216
217 /* are we still trying to reconnect? */
218 if (server->tcpStatus != CifsNeedReconnect)
219 break;
220
221 /*
222 * on "soft" mounts we wait once. Hard mounts keep
223 * retrying until process is killed or server comes
224 * back on-line
225 */
226 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500227 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400228 return -EHOSTDOWN;
229 }
230 }
231
232 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
233 return rc;
234
235 nls_codepage = load_nls_default();
236
237 /*
238 * need to prevent multiple threads trying to simultaneously reconnect
239 * the same SMB session
240 */
241 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200242
243 /*
244 * Recheck after acquire mutex. If another thread is negotiating
245 * and the server never sends an answer the socket will be closed
246 * and tcpStatus set to reconnect.
247 */
248 if (server->tcpStatus == CifsNeedReconnect) {
249 rc = -EHOSTDOWN;
250 mutex_unlock(&tcon->ses->session_mutex);
251 goto out;
252 }
253
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400254 rc = cifs_negotiate_protocol(0, tcon->ses);
255 if (!rc && tcon->ses->need_reconnect)
256 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
257
258 if (rc || !tcon->need_reconnect) {
259 mutex_unlock(&tcon->ses->session_mutex);
260 goto out;
261 }
262
263 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800264 if (tcon->use_persistent)
265 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500266
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400267 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
268 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500269
Joe Perchesf96637b2013-05-04 22:12:25 -0500270 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500271 if (rc) {
272 /* If sess reconnected but tcon didn't, something strange ... */
273 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400274 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500275 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800276
277 if (smb2_command != SMB2_INTERNAL_CMD)
278 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
279
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400280 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400281out:
282 /*
283 * Check if handle based operation so we know whether we can continue
284 * or not without returning to caller to reset file handle.
285 */
286 /*
287 * BB Is flush done by server on drop of tcp session? Should we special
288 * case it and skip above?
289 */
290 switch (smb2_command) {
291 case SMB2_FLUSH:
292 case SMB2_READ:
293 case SMB2_WRITE:
294 case SMB2_LOCK:
295 case SMB2_IOCTL:
296 case SMB2_QUERY_DIRECTORY:
297 case SMB2_CHANGE_NOTIFY:
298 case SMB2_QUERY_INFO:
299 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800300 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400301 }
302 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400303 return rc;
304}
305
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700306static void
307fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
308 unsigned int *total_len)
309{
310 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
311 /* lookup word count ie StructureSize from table */
312 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
313
314 /*
315 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
316 * largest operations (Create)
317 */
318 memset(buf, 0, 256);
319
320 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
321 spdu->StructureSize2 = cpu_to_le16(parmsize);
322
323 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
324}
325
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100326/*
327 * Allocate and return pointer to an SMB request hdr, and set basic
328 * SMB information in the SMB header. If the return code is zero, this
329 * function must have filled in request_buf pointer.
330 */
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800331static int
332smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
333 void **request_buf, unsigned int *total_len)
334{
335 int rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800336
337 rc = smb2_reconnect(smb2_command, tcon);
338 if (rc)
339 return rc;
340
341 /* BB eventually switch this to SMB2 specific small buf size */
342 *request_buf = cifs_small_buf_get();
343 if (*request_buf == NULL) {
344 /* BB should we add a retry in here if not a writepage? */
345 return -ENOMEM;
346 }
347
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100348 fill_small_buf(smb2_command, tcon,
349 (struct smb2_sync_hdr *)(*request_buf),
350 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400351
352 if (tcon != NULL) {
353#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400354 uint16_t com_code = le16_to_cpu(smb2_command);
355 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400356#endif
357 cifs_stats_inc(&tcon->num_smbs_sent);
358 }
359
360 return rc;
361}
362
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500363#ifdef CONFIG_CIFS_SMB311
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100364/* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
365#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500366
367
368#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
369#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
370
371static void
372build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
373{
374 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
375 pneg_ctxt->DataLength = cpu_to_le16(38);
376 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
377 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
378 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
379 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
380}
381
382static void
383build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
384{
385 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
386 pneg_ctxt->DataLength = cpu_to_le16(6);
387 pneg_ctxt->CipherCount = cpu_to_le16(2);
388 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
389 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
390}
391
392static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100393assemble_neg_contexts(struct smb2_negotiate_req *req,
394 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500395{
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100396 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500397
398 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
399 /* Add 2 to size to round to 8 byte boundary */
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100400
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500401 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
402 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
403 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
404 req->NegotiateContextCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100405
406 *total_len += 4 + sizeof(struct smb2_preauth_neg_context)
407 + sizeof(struct smb2_encryption_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500408}
Steve French5100d8a2018-04-09 10:47:14 -0500409
410static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
411{
412 unsigned int len = le16_to_cpu(ctxt->DataLength);
413
414 /* If invalid preauth context warn but use what we requested, SHA-512 */
415 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
416 printk_once(KERN_WARNING "server sent bad preauth context\n");
417 return;
418 }
419 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
420 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
421 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
422 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
423}
424
425static int decode_encrypt_ctx(struct TCP_Server_Info *server,
426 struct smb2_encryption_neg_context *ctxt)
427{
428 unsigned int len = le16_to_cpu(ctxt->DataLength);
429
430 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
431 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
432 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
433 return -EINVAL;
434 }
435
436 if (le16_to_cpu(ctxt->CipherCount) != 1) {
437 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
438 return -EINVAL;
439 }
440 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
441 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
442 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
443 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
444 return -EINVAL;
445 }
446 server->cipher_type = ctxt->Ciphers[0];
447 return 0;
448}
449
450static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
451 struct TCP_Server_Info *server)
452{
453 struct smb2_neg_context *pctx;
454 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
455 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
456 unsigned int len_of_smb = be32_to_cpu(rsp->hdr.smb2_buf_length);
457 unsigned int len_of_ctxts, i;
458 int rc = 0;
459
460 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
461 if (len_of_smb <= offset) {
462 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
463 return -EINVAL;
464 }
465
466 len_of_ctxts = len_of_smb - offset;
467
468 for (i = 0; i < ctxt_cnt; i++) {
469 int clen;
470 /* check that offset is not beyond end of SMB */
471 if (len_of_ctxts == 0)
472 break;
473
474 if (len_of_ctxts < sizeof(struct smb2_neg_context))
475 break;
476
Steve French0d4b46b2018-04-12 20:32:13 -0500477 pctx = (struct smb2_neg_context *)(offset +
478 server->vals->header_preamble_size + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500479 clen = le16_to_cpu(pctx->DataLength);
480 if (clen > len_of_ctxts)
481 break;
482
483 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
484 decode_preauth_context(
485 (struct smb2_preauth_neg_context *)pctx);
486 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
487 rc = decode_encrypt_ctx(server,
488 (struct smb2_encryption_neg_context *)pctx);
489 else
490 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
491 le16_to_cpu(pctx->ContextType));
492
493 if (rc)
494 break;
495 /* offsets must be 8 byte aligned */
496 clen = (clen + 7) & ~0x7;
497 offset += clen + sizeof(struct smb2_neg_context);
498 len_of_ctxts -= clen;
499 }
500 return rc;
501}
502
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500503#else
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100504static void assemble_neg_contexts(struct smb2_negotiate_req *req,
505 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500506{
507 return;
508}
509#endif /* SMB311 */
510
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400511/*
512 *
513 * SMB2 Worker functions follow:
514 *
515 * The general structure of the worker functions is:
516 * 1) Call smb2_init (assembles SMB2 header)
517 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
518 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
519 * 4) Decode SMB2 command specific fields in the fixed length area
520 * 5) Decode variable length data area (if any for this SMB2 command type)
521 * 6) Call free smb buffer
522 * 7) return
523 *
524 */
525
526int
527SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
528{
529 struct smb2_negotiate_req *req;
530 struct smb2_negotiate_rsp *rsp;
531 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700532 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400533 int rc = 0;
534 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400535 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400536 int blob_offset, blob_length;
537 char *security_blob;
538 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100539 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400540
Joe Perchesf96637b2013-05-04 22:12:25 -0500541 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400542
Jeff Layton3534b852013-05-24 07:41:01 -0400543 if (!server) {
544 WARN(1, "%s: server is NULL!\n", __func__);
545 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400546 }
547
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100548 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400549 if (rc)
550 return rc;
551
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100552 req->sync_hdr.SessionId = 0;
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100553#ifdef CONFIG_CIFS_SMB311
554 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
555 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
556#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400557
Steve French9764c022017-09-17 10:41:35 -0500558 if (strcmp(ses->server->vals->version_string,
559 SMB3ANY_VERSION_STRING) == 0) {
560 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
561 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
562 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100563 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500564 } else if (strcmp(ses->server->vals->version_string,
565 SMBDEFAULT_VERSION_STRING) == 0) {
566 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
567 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
568 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
569 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100570 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500571 } else {
572 /* otherwise send specific dialect */
573 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
574 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100575 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500576 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400577
578 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400579 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500580 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400581 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500582 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400583 else
584 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400585
Steve Frenche4aa25e2012-10-01 12:26:22 -0500586 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400587
Steve French3c5f9be12014-05-13 13:37:45 -0700588 /* ClientGUID must be zero for SMB2.02 dialect */
589 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
590 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500591 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700592 memcpy(req->ClientGUID, server->client_guid,
593 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500594 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100595 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500596 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400597 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100598 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400599
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100600 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700601 cifs_small_buf_release(req);
602 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400603 /*
604 * No tcon so can't do
605 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
606 */
Steve French7e682f72017-08-31 21:34:24 -0500607 if (rc == -EOPNOTSUPP) {
608 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500609 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500610 " older servers\n");
611 goto neg_exit;
612 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400613 goto neg_exit;
614
Steve French9764c022017-09-17 10:41:35 -0500615 if (strcmp(ses->server->vals->version_string,
616 SMB3ANY_VERSION_STRING) == 0) {
617 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
618 cifs_dbg(VFS,
619 "SMB2 dialect returned but not requested\n");
620 return -EIO;
621 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
622 cifs_dbg(VFS,
623 "SMB2.1 dialect returned but not requested\n");
624 return -EIO;
625 }
626 } else if (strcmp(ses->server->vals->version_string,
627 SMBDEFAULT_VERSION_STRING) == 0) {
628 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
629 cifs_dbg(VFS,
630 "SMB2 dialect returned but not requested\n");
631 return -EIO;
632 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
633 /* ops set to 3.0 by default for default so update */
634 ses->server->ops = &smb21_operations;
635 }
Steve French590d08d32017-09-19 11:43:47 -0500636 } else if (le16_to_cpu(rsp->DialectRevision) !=
637 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500638 /* if requested single dialect ensure returned dialect matched */
639 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d32017-09-19 11:43:47 -0500640 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500641 return -EIO;
642 }
643
Joe Perchesf96637b2013-05-04 22:12:25 -0500644 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400645
Steve Frenche4aa25e2012-10-01 12:26:22 -0500646 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500647 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500648 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500649 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500650 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500651 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500652 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
653 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600654#ifdef CONFIG_CIFS_SMB311
655 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
656 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
657#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400658 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500659 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500660 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400661 rc = -EIO;
662 goto neg_exit;
663 }
664 server->dialect = le16_to_cpu(rsp->DialectRevision);
665
Steve French9764c022017-09-17 10:41:35 -0500666 /* BB: add check that dialect was valid given dialect(s) we asked for */
667
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100668#ifdef CONFIG_CIFS_SMB311
669 /*
670 * Keep a copy of the hash after negprot. This hash will be
671 * the starting hash value for all sessions made from this
672 * server.
673 */
674 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
675 SMB2_PREAUTH_HASH_SIZE);
676#endif
Jeff Laytone598d1d82013-05-26 07:00:59 -0400677 /* SMB2 only has an extended negflavor */
678 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400679 /* set it to the maximum buffer size value we can send with 1 credit */
680 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
681 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400682 server->max_read = le32_to_cpu(rsp->MaxReadSize);
683 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400684 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500685 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
686 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
687 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400688 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400689 /* Internal types */
690 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400691
692 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
693 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500694 /*
695 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
696 * for us will be
697 * ses->sectype = RawNTLMSSP;
698 * but for time being this is our only auth choice so doesn't matter.
699 * We just found a server which sets blob length to zero expecting raw.
700 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700701 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500702 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700703 server->sec_ntlmssp = true;
704 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700705
Jeff Layton38d77c52013-05-26 07:01:00 -0400706 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400707 if (rc)
708 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500709 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500710 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500711 if (rc == 1)
712 rc = 0;
713 else if (rc == 0)
714 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400715 }
Steve French5100d8a2018-04-09 10:47:14 -0500716
717#ifdef CONFIG_CIFS_SMB311
718 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
719 if (rsp->NegotiateContextCount)
720 rc = smb311_decode_neg_context(rsp, server);
721 else
722 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
723 }
724#endif /* CONFIG_CIFS_SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400725neg_exit:
726 free_rsp_buf(resp_buftype, rsp);
727 return rc;
728}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400729
Steve Frenchff1c0382013-11-19 23:44:46 -0600730int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
731{
732 int rc = 0;
733 struct validate_negotiate_info_req vneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200734 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600735 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500736 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600737
738 cifs_dbg(FYI, "validate negotiate\n");
739
Long Li8801e902017-11-22 17:38:49 -0700740#ifdef CONFIG_CIFS_SMB_DIRECT
741 if (tcon->ses->server->rdma)
742 return 0;
743#endif
744
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100745 /* In SMB3.11 preauth integrity supersedes validate negotiate */
746 if (tcon->ses->server->dialect == SMB311_PROT_ID)
747 return 0;
748
Steve Frenchff1c0382013-11-19 23:44:46 -0600749 /*
750 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500751 * can not sign it (ie are not known user). Even if signing is not
752 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600753 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500754 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600755 */
Steve French0603c962017-09-20 19:57:18 -0500756 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600757 return 0; /* validation requires signing */
758
Steve French0603c962017-09-20 19:57:18 -0500759 if (tcon->ses->user_name == NULL) {
760 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
761 return 0; /* validation requires signing */
762 }
763
764 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
765 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
766
Steve Frenchff1c0382013-11-19 23:44:46 -0600767 vneg_inbuf.Capabilities =
768 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100769 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
770 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600771
772 if (tcon->ses->sign)
773 vneg_inbuf.SecurityMode =
774 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
775 else if (global_secflags & CIFSSEC_MAY_SIGN)
776 vneg_inbuf.SecurityMode =
777 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
778 else
779 vneg_inbuf.SecurityMode = 0;
780
Steve French9764c022017-09-17 10:41:35 -0500781
782 if (strcmp(tcon->ses->server->vals->version_string,
783 SMB3ANY_VERSION_STRING) == 0) {
784 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
785 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
786 vneg_inbuf.DialectCount = cpu_to_le16(2);
787 /* structure is big enough for 3 dialects, sending only 2 */
788 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
789 } else if (strcmp(tcon->ses->server->vals->version_string,
790 SMBDEFAULT_VERSION_STRING) == 0) {
791 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
792 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
793 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
794 vneg_inbuf.DialectCount = cpu_to_le16(3);
795 /* structure is big enough for 3 dialects */
796 inbuflen = sizeof(struct validate_negotiate_info_req);
797 } else {
798 /* otherwise specific dialect was requested */
799 vneg_inbuf.Dialects[0] =
800 cpu_to_le16(tcon->ses->server->vals->protocol_id);
801 vneg_inbuf.DialectCount = cpu_to_le16(1);
802 /* structure is big enough for 3 dialects, sending only 1 */
803 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
804 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600805
806 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
807 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
808 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
809 (char **)&pneg_rsp, &rsplen);
810
811 if (rc != 0) {
812 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
813 return -EIO;
814 }
815
816 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500817 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
818 rsplen);
819
820 /* relax check since Mac returns max bufsize allowed on ioctl */
David Disseldorpa2d9daa2017-10-20 14:49:38 +0200821 if ((rsplen > CIFSMaxBufSize)
822 || (rsplen < sizeof(struct validate_negotiate_info_rsp)))
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200823 goto err_rsp_free;
Steve Frenchff1c0382013-11-19 23:44:46 -0600824 }
825
826 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +0100827 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600828 goto vneg_out;
829
830 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
831 goto vneg_out;
832
833 /* do not validate server guid because not saved at negprot time yet */
834
835 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
836 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
837 goto vneg_out;
838
839 /* validate negotiate successful */
840 cifs_dbg(FYI, "validate negotiate info successful\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200841 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600842 return 0;
843
844vneg_out:
845 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200846err_rsp_free:
847 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600848 return -EIO;
849}
850
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530851enum securityEnum
852smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
853{
854 switch (requested) {
855 case Kerberos:
856 case RawNTLMSSP:
857 return requested;
858 case NTLMv2:
859 return RawNTLMSSP;
860 case Unspecified:
861 if (server->sec_ntlmssp &&
862 (global_secflags & CIFSSEC_MAY_NTLMSSP))
863 return RawNTLMSSP;
864 if ((server->sec_kerberos || server->sec_mskerberos) &&
865 (global_secflags & CIFSSEC_MAY_KRB5))
866 return Kerberos;
867 /* Fallthrough */
868 default:
869 return Unspecified;
870 }
871}
872
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100873struct SMB2_sess_data {
874 unsigned int xid;
875 struct cifs_ses *ses;
876 struct nls_table *nls_cp;
877 void (*func)(struct SMB2_sess_data *);
878 int result;
879 u64 previous_session;
880
881 /* we will send the SMB in three pieces:
882 * a fixed length beginning part, an optional
883 * SPNEGO blob (which can be zero length), and a
884 * last part which will include the strings
885 * and rest of bcc area. This allows us to avoid
886 * a large buffer 17K allocation
887 */
888 int buf0_type;
889 struct kvec iov[2];
890};
891
892static int
893SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
894{
895 int rc;
896 struct cifs_ses *ses = sess_data->ses;
897 struct smb2_sess_setup_req *req;
898 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100899 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100900
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100901 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
902 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100903 if (rc)
904 return rc;
905
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700906 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100907 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100908
909 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
910 req->PreviousSessionId = sess_data->previous_session;
911
912 req->Flags = 0; /* MBZ */
913 /* to enable echos and oplocks */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100914 req->sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100915
916 /* only one of SMB2 signing flags may be set in SMB2 request */
917 if (server->sign)
918 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
919 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
920 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
921 else
922 req->SecurityMode = 0;
923
924 req->Capabilities = 0;
925 req->Channel = 0; /* MBZ */
926
927 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100928 /* 1 for pad */
929 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100930 /*
931 * This variable will be used to clear the buffer
932 * allocated above in case of any error in the calling function.
933 */
934 sess_data->buf0_type = CIFS_SMALL_BUFFER;
935
936 return 0;
937}
938
939static void
940SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
941{
942 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
943 sess_data->buf0_type = CIFS_NO_BUFFER;
944}
945
946static int
947SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
948{
949 int rc;
950 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700951 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100952
953 /* Testing shows that buffer offset must be at location of Buffer[0] */
954 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100955 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100956 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
957
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100958 /* BB add code to build os and lm fields */
959
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100960 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
961 sess_data->iov, 2,
962 &sess_data->buf0_type,
963 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700964 cifs_small_buf_release(sess_data->iov[0].iov_base);
965 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100966
967 return rc;
968}
969
970static int
971SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
972{
973 int rc = 0;
974 struct cifs_ses *ses = sess_data->ses;
975
976 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800977 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100978 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100979 if (rc) {
980 cifs_dbg(FYI,
981 "SMB3 session key generation failed\n");
982 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800983 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100984 }
985 }
986 if (!ses->server->session_estab) {
987 ses->server->sequence_number = 0x2;
988 ses->server->session_estab = true;
989 }
990 mutex_unlock(&ses->server->srv_mutex);
991
992 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
993 spin_lock(&GlobalMid_Lock);
994 ses->status = CifsGood;
995 ses->need_reconnect = false;
996 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100997 return rc;
998}
999
1000#ifdef CONFIG_CIFS_UPCALL
1001static void
1002SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1003{
1004 int rc;
1005 struct cifs_ses *ses = sess_data->ses;
1006 struct cifs_spnego_msg *msg;
1007 struct key *spnego_key = NULL;
1008 struct smb2_sess_setup_rsp *rsp = NULL;
1009
1010 rc = SMB2_sess_alloc_buffer(sess_data);
1011 if (rc)
1012 goto out;
1013
1014 spnego_key = cifs_get_spnego_key(ses);
1015 if (IS_ERR(spnego_key)) {
1016 rc = PTR_ERR(spnego_key);
1017 spnego_key = NULL;
1018 goto out;
1019 }
1020
1021 msg = spnego_key->payload.data[0];
1022 /*
1023 * check version field to make sure that cifs.upcall is
1024 * sending us a response in an expected form
1025 */
1026 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1027 cifs_dbg(VFS,
1028 "bad cifs.upcall version. Expected %d got %d",
1029 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1030 rc = -EKEYREJECTED;
1031 goto out_put_spnego_key;
1032 }
1033
1034 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1035 GFP_KERNEL);
1036 if (!ses->auth_key.response) {
1037 cifs_dbg(VFS,
1038 "Kerberos can't allocate (%u bytes) memory",
1039 msg->sesskey_len);
1040 rc = -ENOMEM;
1041 goto out_put_spnego_key;
1042 }
1043 ses->auth_key.len = msg->sesskey_len;
1044
1045 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1046 sess_data->iov[1].iov_len = msg->secblob_len;
1047
1048 rc = SMB2_sess_sendreceive(sess_data);
1049 if (rc)
1050 goto out_put_spnego_key;
1051
1052 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001053 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001054
1055 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001056
1057 rc = SMB2_sess_establish_session(sess_data);
1058out_put_spnego_key:
1059 key_invalidate(spnego_key);
1060 key_put(spnego_key);
1061out:
1062 sess_data->result = rc;
1063 sess_data->func = NULL;
1064 SMB2_sess_free_buffer(sess_data);
1065}
1066#else
1067static void
1068SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1069{
1070 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1071 sess_data->result = -EOPNOTSUPP;
1072 sess_data->func = NULL;
1073}
1074#endif
1075
Sachin Prabhu166cea42016-10-07 19:11:22 +01001076static void
1077SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1078
1079static void
1080SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1081{
1082 int rc;
1083 struct cifs_ses *ses = sess_data->ses;
1084 struct smb2_sess_setup_rsp *rsp = NULL;
1085 char *ntlmssp_blob = NULL;
1086 bool use_spnego = false; /* else use raw ntlmssp */
1087 u16 blob_length = 0;
1088
1089 /*
1090 * If memory allocation is successful, caller of this function
1091 * frees it.
1092 */
1093 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1094 if (!ses->ntlmssp) {
1095 rc = -ENOMEM;
1096 goto out_err;
1097 }
1098 ses->ntlmssp->sesskey_per_smbsess = true;
1099
1100 rc = SMB2_sess_alloc_buffer(sess_data);
1101 if (rc)
1102 goto out_err;
1103
1104 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1105 GFP_KERNEL);
1106 if (ntlmssp_blob == NULL) {
1107 rc = -ENOMEM;
1108 goto out;
1109 }
1110
1111 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1112 if (use_spnego) {
1113 /* BB eventually need to add this */
1114 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1115 rc = -EOPNOTSUPP;
1116 goto out;
1117 } else {
1118 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1119 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1120 }
1121 sess_data->iov[1].iov_base = ntlmssp_blob;
1122 sess_data->iov[1].iov_len = blob_length;
1123
1124 rc = SMB2_sess_sendreceive(sess_data);
1125 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1126
1127 /* If true, rc here is expected and not an error */
1128 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001129 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001130 rc = 0;
1131
1132 if (rc)
1133 goto out;
1134
Ronnie Sahlberg9fdd2e02018-04-09 18:06:27 +10001135 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - ses->server->vals->header_preamble_size !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001136 le16_to_cpu(rsp->SecurityBufferOffset)) {
1137 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1138 le16_to_cpu(rsp->SecurityBufferOffset));
1139 rc = -EIO;
1140 goto out;
1141 }
1142 rc = decode_ntlmssp_challenge(rsp->Buffer,
1143 le16_to_cpu(rsp->SecurityBufferLength), ses);
1144 if (rc)
1145 goto out;
1146
1147 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1148
1149
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001150 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001151 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001152
1153out:
1154 kfree(ntlmssp_blob);
1155 SMB2_sess_free_buffer(sess_data);
1156 if (!rc) {
1157 sess_data->result = 0;
1158 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1159 return;
1160 }
1161out_err:
1162 kfree(ses->ntlmssp);
1163 ses->ntlmssp = NULL;
1164 sess_data->result = rc;
1165 sess_data->func = NULL;
1166}
1167
1168static void
1169SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1170{
1171 int rc;
1172 struct cifs_ses *ses = sess_data->ses;
1173 struct smb2_sess_setup_req *req;
1174 struct smb2_sess_setup_rsp *rsp = NULL;
1175 unsigned char *ntlmssp_blob = NULL;
1176 bool use_spnego = false; /* else use raw ntlmssp */
1177 u16 blob_length = 0;
1178
1179 rc = SMB2_sess_alloc_buffer(sess_data);
1180 if (rc)
1181 goto out;
1182
1183 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001184 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001185
1186 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1187 sess_data->nls_cp);
1188 if (rc) {
1189 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1190 goto out;
1191 }
1192
1193 if (use_spnego) {
1194 /* BB eventually need to add this */
1195 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1196 rc = -EOPNOTSUPP;
1197 goto out;
1198 }
1199 sess_data->iov[1].iov_base = ntlmssp_blob;
1200 sess_data->iov[1].iov_len = blob_length;
1201
1202 rc = SMB2_sess_sendreceive(sess_data);
1203 if (rc)
1204 goto out;
1205
1206 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1207
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001208 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001209 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001210
1211 rc = SMB2_sess_establish_session(sess_data);
1212out:
1213 kfree(ntlmssp_blob);
1214 SMB2_sess_free_buffer(sess_data);
1215 kfree(ses->ntlmssp);
1216 ses->ntlmssp = NULL;
1217 sess_data->result = rc;
1218 sess_data->func = NULL;
1219}
1220
1221static int
1222SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1223{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301224 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001225
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301226 type = smb2_select_sectype(ses->server, ses->sectype);
1227 cifs_dbg(FYI, "sess setup type %d\n", type);
1228 if (type == Unspecified) {
1229 cifs_dbg(VFS,
1230 "Unable to select appropriate authentication method!");
1231 return -EINVAL;
1232 }
1233
1234 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001235 case Kerberos:
1236 sess_data->func = SMB2_auth_kerberos;
1237 break;
1238 case RawNTLMSSP:
1239 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1240 break;
1241 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301242 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001243 return -EOPNOTSUPP;
1244 }
1245
1246 return 0;
1247}
1248
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001249int
1250SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1251 const struct nls_table *nls_cp)
1252{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001253 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001254 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001255 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001256
Joe Perchesf96637b2013-05-04 22:12:25 -05001257 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001258
Jeff Layton3534b852013-05-24 07:41:01 -04001259 if (!server) {
1260 WARN(1, "%s: server is NULL!\n", __func__);
1261 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001262 }
1263
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001264 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1265 if (!sess_data)
1266 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001267
1268 rc = SMB2_select_sec(ses, sess_data);
1269 if (rc)
1270 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001271 sess_data->xid = xid;
1272 sess_data->ses = ses;
1273 sess_data->buf0_type = CIFS_NO_BUFFER;
1274 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001275
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001276#ifdef CONFIG_CIFS_SMB311
1277 /*
1278 * Initialize the session hash with the server one.
1279 */
1280 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1281 SMB2_PREAUTH_HASH_SIZE);
1282#endif
1283
Sachin Prabhu166cea42016-10-07 19:11:22 +01001284 while (sess_data->func)
1285 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001286
Steve Frenchc721c382017-09-19 18:40:03 -05001287 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1288 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001289 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001290out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001291 kfree(sess_data);
1292 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001293}
1294
1295int
1296SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1297{
1298 struct smb2_logoff_req *req; /* response is also trivial struct */
1299 int rc = 0;
1300 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001301 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001302 unsigned int total_len;
1303 struct kvec iov[1];
1304 struct kvec rsp_iov;
1305 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001306
Joe Perchesf96637b2013-05-04 22:12:25 -05001307 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001308
1309 if (ses && (ses->server))
1310 server = ses->server;
1311 else
1312 return -EIO;
1313
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001314 /* no need to send SMB logoff if uid already closed due to reconnect */
1315 if (ses->need_reconnect)
1316 goto smb2_session_already_dead;
1317
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001318 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001319 if (rc)
1320 return rc;
1321
1322 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001323 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001324
1325 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1326 flags |= CIFS_TRANSFORM_REQ;
1327 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001328 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001329
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001330 flags |= CIFS_NO_RESP;
1331
1332 iov[0].iov_base = (char *)req;
1333 iov[0].iov_len = total_len;
1334
1335 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001336 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001337 /*
1338 * No tcon so can't do
1339 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1340 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001341
1342smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001343 return rc;
1344}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001345
1346static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1347{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001348 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001349}
1350
1351#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1352
Steve Frenchde9f68df2013-11-15 11:26:24 -06001353/* These are similar values to what Windows uses */
1354static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1355{
1356 tcon->max_chunks = 256;
1357 tcon->max_bytes_chunk = 1048576;
1358 tcon->max_bytes_copy = 16777216;
1359}
1360
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001361int
1362SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1363 struct cifs_tcon *tcon, const struct nls_table *cp)
1364{
1365 struct smb2_tree_connect_req *req;
1366 struct smb2_tree_connect_rsp *rsp = NULL;
1367 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001368 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001369 int rc = 0;
1370 int resp_buftype;
1371 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001372 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001373 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001374 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001375
Joe Perchesf96637b2013-05-04 22:12:25 -05001376 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001377
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001378 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001379 return -EIO;
1380
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001381 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1382 if (unc_path == NULL)
1383 return -ENOMEM;
1384
1385 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1386 unc_path_len *= 2;
1387 if (unc_path_len < 2) {
1388 kfree(unc_path);
1389 return -EINVAL;
1390 }
1391
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001392 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001393 tcon->tid = 0;
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001394
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001395 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1396 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001397 if (rc) {
1398 kfree(unc_path);
1399 return rc;
1400 }
1401
Aurelien Aptelb327a712018-01-24 13:46:10 +01001402 if (encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001403 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001404
1405 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001406 /* 1 for pad */
1407 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001408
1409 /* Testing shows that buffer offset must be at location of Buffer[0] */
1410 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001411 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001412 req->PathLength = cpu_to_le16(unc_path_len - 2);
1413 iov[1].iov_base = unc_path;
1414 iov[1].iov_len = unc_path_len;
1415
Steve French6188f282018-03-13 02:29:36 -05001416 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1417 if ((ses->server->dialect == SMB311_PROT_ID) &&
1418 !encryption_required(tcon))
1419 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1420
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001421 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001422 cifs_small_buf_release(req);
1423 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001424
1425 if (rc != 0) {
1426 if (tcon) {
1427 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1428 tcon->need_reconnect = true;
1429 }
1430 goto tcon_error_exit;
1431 }
1432
Christophe JAILLETcd123002017-05-12 17:59:32 +02001433 switch (rsp->ShareType) {
1434 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001435 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001436 break;
1437 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001438 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001439 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001440 break;
1441 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001442 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001443 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001444 break;
1445 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001446 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001447 rc = -EOPNOTSUPP;
1448 goto tcon_error_exit;
1449 }
1450
1451 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001452 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001453 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1454 tcon->tidStatus = CifsGood;
1455 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001456 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001457 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001458
1459 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1460 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001461 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001462
1463 if (tcon->seal &&
1464 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1465 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1466
Steve Frenchde9f68df2013-11-15 11:26:24 -06001467 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001468 if (tcon->ses->server->ops->validate_negotiate)
1469 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001470tcon_exit:
1471 free_rsp_buf(resp_buftype, rsp);
1472 kfree(unc_path);
1473 return rc;
1474
1475tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001476 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001477 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001478 }
1479 goto tcon_exit;
1480}
1481
1482int
1483SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1484{
1485 struct smb2_tree_disconnect_req *req; /* response is trivial */
1486 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001487 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001488 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001489 unsigned int total_len;
1490 struct kvec iov[1];
1491 struct kvec rsp_iov;
1492 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001493
Joe Perchesf96637b2013-05-04 22:12:25 -05001494 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001495
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001496 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001497 return -EIO;
1498
1499 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1500 return 0;
1501
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001502 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1503 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001504 if (rc)
1505 return rc;
1506
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001507 if (encryption_required(tcon))
1508 flags |= CIFS_TRANSFORM_REQ;
1509
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001510 flags |= CIFS_NO_RESP;
1511
1512 iov[0].iov_base = (char *)req;
1513 iov[0].iov_len = total_len;
1514
1515 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001516 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001517 if (rc)
1518 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1519
1520 return rc;
1521}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001522
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001523
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001524static struct create_durable *
1525create_durable_buf(void)
1526{
1527 struct create_durable *buf;
1528
1529 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1530 if (!buf)
1531 return NULL;
1532
1533 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001534 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001535 buf->ccontext.DataLength = cpu_to_le32(16);
1536 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1537 (struct create_durable, Name));
1538 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001539 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001540 buf->Name[0] = 'D';
1541 buf->Name[1] = 'H';
1542 buf->Name[2] = 'n';
1543 buf->Name[3] = 'Q';
1544 return buf;
1545}
1546
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001547static struct create_durable *
1548create_reconnect_durable_buf(struct cifs_fid *fid)
1549{
1550 struct create_durable *buf;
1551
1552 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1553 if (!buf)
1554 return NULL;
1555
1556 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1557 (struct create_durable, Data));
1558 buf->ccontext.DataLength = cpu_to_le32(16);
1559 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1560 (struct create_durable, Name));
1561 buf->ccontext.NameLength = cpu_to_le16(4);
1562 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1563 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001564 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001565 buf->Name[0] = 'D';
1566 buf->Name[1] = 'H';
1567 buf->Name[2] = 'n';
1568 buf->Name[3] = 'C';
1569 return buf;
1570}
1571
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001572static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001573parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1574 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001575{
1576 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001577 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001578 unsigned int next;
1579 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001580 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001581
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001582 data_offset = (char *)rsp + server->vals->header_preamble_size + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001583 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001584 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001585 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001586 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001587 if (le16_to_cpu(cc->NameLength) == 4 &&
1588 strncmp(name, "RqLs", 4) == 0)
1589 return server->ops->parse_lease_buf(cc, epoch);
1590
1591 next = le32_to_cpu(cc->Next);
1592 if (!next)
1593 break;
1594 remaining -= next;
1595 cc = (struct create_context *)((char *)cc + next);
1596 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001597
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001598 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001599}
1600
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001601static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001602add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1603 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001604{
1605 struct smb2_create_req *req = iov[0].iov_base;
1606 unsigned int num = *num_iovec;
1607
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001608 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001609 if (iov[num].iov_base == NULL)
1610 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001611 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001612 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1613 if (!req->CreateContextsOffset)
1614 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001615 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001616 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001617 le32_add_cpu(&req->CreateContextsLength,
1618 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001619 *num_iovec = num + 1;
1620 return 0;
1621}
1622
Steve Frenchb56eae42015-11-03 09:26:27 -06001623static struct create_durable_v2 *
1624create_durable_v2_buf(struct cifs_fid *pfid)
1625{
1626 struct create_durable_v2 *buf;
1627
1628 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1629 if (!buf)
1630 return NULL;
1631
1632 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1633 (struct create_durable_v2, dcontext));
1634 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1635 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1636 (struct create_durable_v2, Name));
1637 buf->ccontext.NameLength = cpu_to_le16(4);
1638
1639 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1640 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001641 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001642 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1643
1644 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1645 buf->Name[0] = 'D';
1646 buf->Name[1] = 'H';
1647 buf->Name[2] = '2';
1648 buf->Name[3] = 'Q';
1649 return buf;
1650}
1651
1652static struct create_durable_handle_reconnect_v2 *
1653create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1654{
1655 struct create_durable_handle_reconnect_v2 *buf;
1656
1657 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1658 GFP_KERNEL);
1659 if (!buf)
1660 return NULL;
1661
1662 buf->ccontext.DataOffset =
1663 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1664 dcontext));
1665 buf->ccontext.DataLength =
1666 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1667 buf->ccontext.NameOffset =
1668 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1669 Name));
1670 buf->ccontext.NameLength = cpu_to_le16(4);
1671
1672 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1673 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1674 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1675 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1676
1677 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1678 buf->Name[0] = 'D';
1679 buf->Name[1] = 'H';
1680 buf->Name[2] = '2';
1681 buf->Name[3] = 'C';
1682 return buf;
1683}
1684
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001685static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001686add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001687 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001688{
1689 struct smb2_create_req *req = iov[0].iov_base;
1690 unsigned int num = *num_iovec;
1691
Steve Frenchb56eae42015-11-03 09:26:27 -06001692 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1693 if (iov[num].iov_base == NULL)
1694 return -ENOMEM;
1695 iov[num].iov_len = sizeof(struct create_durable_v2);
1696 if (!req->CreateContextsOffset)
1697 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001698 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001699 iov[1].iov_len);
1700 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001701 *num_iovec = num + 1;
1702 return 0;
1703}
1704
1705static int
1706add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1707 struct cifs_open_parms *oparms)
1708{
1709 struct smb2_create_req *req = iov[0].iov_base;
1710 unsigned int num = *num_iovec;
1711
1712 /* indicate that we don't need to relock the file */
1713 oparms->reconnect = false;
1714
1715 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1716 if (iov[num].iov_base == NULL)
1717 return -ENOMEM;
1718 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1719 if (!req->CreateContextsOffset)
1720 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001721 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001722 iov[1].iov_len);
1723 le32_add_cpu(&req->CreateContextsLength,
1724 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001725 *num_iovec = num + 1;
1726 return 0;
1727}
1728
1729static int
1730add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1731 struct cifs_open_parms *oparms, bool use_persistent)
1732{
1733 struct smb2_create_req *req = iov[0].iov_base;
1734 unsigned int num = *num_iovec;
1735
1736 if (use_persistent) {
1737 if (oparms->reconnect)
1738 return add_durable_reconnect_v2_context(iov, num_iovec,
1739 oparms);
1740 else
1741 return add_durable_v2_context(iov, num_iovec, oparms);
1742 }
1743
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001744 if (oparms->reconnect) {
1745 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1746 /* indicate that we don't need to relock the file */
1747 oparms->reconnect = false;
1748 } else
1749 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001750 if (iov[num].iov_base == NULL)
1751 return -ENOMEM;
1752 iov[num].iov_len = sizeof(struct create_durable);
1753 if (!req->CreateContextsOffset)
1754 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001755 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001756 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001757 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001758 *num_iovec = num + 1;
1759 return 0;
1760}
1761
Aurelien Aptelf0712922017-02-22 14:47:17 +01001762static int
1763alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1764 const char *treename, const __le16 *path)
1765{
1766 int treename_len, path_len;
1767 struct nls_table *cp;
1768 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1769
1770 /*
1771 * skip leading "\\"
1772 */
1773 treename_len = strlen(treename);
1774 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1775 return -EINVAL;
1776
1777 treename += 2;
1778 treename_len -= 2;
1779
1780 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1781
1782 /*
1783 * make room for one path separator between the treename and
1784 * path
1785 */
1786 *out_len = treename_len + 1 + path_len;
1787
1788 /*
1789 * final path needs to be null-terminated UTF16 with a
1790 * size aligned to 8
1791 */
1792
1793 *out_size = roundup((*out_len+1)*2, 8);
1794 *out_path = kzalloc(*out_size, GFP_KERNEL);
1795 if (!*out_path)
1796 return -ENOMEM;
1797
1798 cp = load_nls_default();
1799 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1800 UniStrcat(*out_path, sep);
1801 UniStrcat(*out_path, path);
1802 unload_nls(cp);
1803
1804 return 0;
1805}
1806
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001807int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001808SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001809 __u8 *oplock, struct smb2_file_all_info *buf,
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001810 struct kvec *err_iov)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001811{
1812 struct smb2_create_req *req;
1813 struct smb2_create_rsp *rsp;
1814 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001815 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001816 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001817 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001818 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001819 int resp_buftype;
1820 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001821 __le16 *copy_path = NULL;
1822 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001823 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001824 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001825 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001826 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001827 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001828 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001829
Joe Perchesf96637b2013-05-04 22:12:25 -05001830 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001831
1832 if (ses && (ses->server))
1833 server = ses->server;
1834 else
1835 return -EIO;
1836
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001837 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1838
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001839 if (rc)
1840 return rc;
1841
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001842 if (encryption_required(tcon))
1843 flags |= CIFS_TRANSFORM_REQ;
1844
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001845 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001846 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001847 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1848 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001849
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001850 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001851 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001852 /* File attributes ignored on open (used in create though) */
1853 req->FileAttributes = cpu_to_le32(file_attributes);
1854 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001855 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1856 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001857
1858 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001859 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001860 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001861
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001862 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01001863
1864 /* [MS-SMB2] 2.2.13 NameOffset:
1865 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1866 * the SMB2 header, the file name includes a prefix that will
1867 * be processed during DFS name normalization as specified in
1868 * section 3.3.5.9. Otherwise, the file name is relative to
1869 * the share that is identified by the TreeId in the SMB2
1870 * header.
1871 */
1872 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1873 int name_len;
1874
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001875 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001876 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1877 &name_len,
1878 tcon->treeName, path);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001879 if (rc) {
1880 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001881 return rc;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001882 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001883 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001884 uni_path_len = copy_size;
1885 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001886 } else {
1887 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1888 /* MUST set path len (NameLength) to 0 opening root of share */
1889 req->NameLength = cpu_to_le16(uni_path_len - 2);
1890 if (uni_path_len % 8 != 0) {
1891 copy_size = roundup(uni_path_len, 8);
1892 copy_path = kzalloc(copy_size, GFP_KERNEL);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001893 if (!copy_path) {
1894 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001895 return -ENOMEM;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001896 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001897 memcpy((char *)copy_path, (const char *)path,
1898 uni_path_len);
1899 uni_path_len = copy_size;
1900 path = copy_path;
1901 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001902 }
1903
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001904 iov[1].iov_len = uni_path_len;
1905 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001906
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001907 if (!server->oplocks)
1908 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1909
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001910 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001911 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1912 req->RequestedOplockLevel = *oplock;
1913 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001914 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001915 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001916 cifs_small_buf_release(req);
1917 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001918 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001919 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001920 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001921 }
1922
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001923 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1924 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001925 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001926 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001927 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001928 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001929 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001930 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001931
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001932 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001933 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001934 if (rc) {
1935 cifs_small_buf_release(req);
1936 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001937 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001938 return rc;
1939 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001940 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001941 }
1942
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001943 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1944 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001945 cifs_small_buf_release(req);
1946 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001947
1948 if (rc != 0) {
1949 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001950 if (err_iov && rsp) {
1951 *err_iov = rsp_iov;
1952 resp_buftype = CIFS_NO_BUFFER;
1953 rsp = NULL;
1954 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001955 goto creat_exit;
1956 }
1957
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001958 oparms->fid->persistent_fid = rsp->PersistentFileId;
1959 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001960
1961 if (buf) {
1962 memcpy(buf, &rsp->CreationTime, 32);
1963 buf->AllocationSize = rsp->AllocationSize;
1964 buf->EndOfFile = rsp->EndofFile;
1965 buf->Attributes = rsp->FileAttributes;
1966 buf->NumberOfLinks = cpu_to_le32(1);
1967 buf->DeletePending = 0;
1968 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001969
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001970 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001971 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001972 else
1973 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001974creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001975 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001976 kfree(lc_buf);
1977 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001978 free_rsp_buf(resp_buftype, rsp);
1979 return rc;
1980}
1981
Steve French4a72daf2013-06-25 00:20:49 -05001982/*
1983 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1984 */
1985int
1986SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001987 u64 volatile_fid, u32 opcode, bool is_fsctl,
Aurelien Aptel51146622017-02-28 15:08:41 +01001988 char *in_data, u32 indatalen,
1989 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001990{
1991 struct smb2_ioctl_req *req;
1992 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001993 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001994 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001995 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001996 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001997 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001998 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001999 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002000 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002001 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05002002
2003 cifs_dbg(FYI, "SMB2 IOCTL\n");
2004
Steve French3d1a3742014-08-11 21:05:25 -05002005 if (out_data != NULL)
2006 *out_data = NULL;
2007
Steve French4a72daf2013-06-25 00:20:49 -05002008 /* zero out returned data len, in case of error */
2009 if (plen)
2010 *plen = 0;
2011
Steve French8e353102015-03-26 19:47:02 -05002012 if (tcon)
2013 ses = tcon->ses;
2014 else
2015 return -EIO;
2016
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002017 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05002018 return -EIO;
2019
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002020 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002021 if (rc)
2022 return rc;
2023
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002024 if (encryption_required(tcon))
2025 flags |= CIFS_TRANSFORM_REQ;
2026
Steve French4a72daf2013-06-25 00:20:49 -05002027 req->CtlCode = cpu_to_le32(opcode);
2028 req->PersistentFileId = persistent_fid;
2029 req->VolatileFileId = volatile_fid;
2030
2031 if (indatalen) {
2032 req->InputCount = cpu_to_le32(indatalen);
2033 /* do not set InputOffset if no input data */
2034 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002035 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05002036 iov[1].iov_base = in_data;
2037 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002038 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05002039 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002040 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05002041
2042 req->OutputOffset = 0;
2043 req->OutputCount = 0; /* MBZ */
2044
2045 /*
2046 * Could increase MaxOutputResponse, but that would require more
2047 * than one credit. Windows typically sets this smaller, but for some
2048 * ioctls it may be useful to allow server to send more. No point
2049 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05002050 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2051 * (by default, note that it can be overridden to make max larger)
2052 * in responses (except for read responses which can be bigger.
2053 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05002054 */
Steve French7db0a6e2017-05-03 21:12:20 -05002055 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05002056
2057 if (is_fsctl)
2058 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2059 else
2060 req->Flags = 0;
2061
2062 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05002063
Steve French7ff8d452013-10-14 00:44:19 -05002064 /*
2065 * If no input data, the size of ioctl struct in
2066 * protocol spec still includes a 1 byte data buffer,
2067 * but if input data passed to ioctl, we do not
2068 * want to double count this, so we do not send
2069 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002070 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05002071 */
2072
2073 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002074 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05002075 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002076 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05002077
Steve French4587eee2017-10-25 15:58:31 -05002078 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2079 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002080 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002081
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002082 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2083 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002084 cifs_small_buf_release(req);
2085 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002086
Steve French9bf0c9c2013-11-16 18:05:28 -06002087 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002088 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002089 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002090 } else if (rc == -EINVAL) {
2091 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2092 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002093 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002094 goto ioctl_exit;
2095 }
Steve French4a72daf2013-06-25 00:20:49 -05002096 }
2097
2098 /* check if caller wants to look at return data or just return rc */
2099 if ((plen == NULL) || (out_data == NULL))
2100 goto ioctl_exit;
2101
2102 *plen = le32_to_cpu(rsp->OutputCount);
2103
2104 /* We check for obvious errors in the output buffer length and offset */
2105 if (*plen == 0)
2106 goto ioctl_exit; /* server returned no data */
2107 else if (*plen > 0xFF00) {
2108 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2109 *plen = 0;
2110 rc = -EIO;
2111 goto ioctl_exit;
2112 }
2113
2114 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2115 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2116 le32_to_cpu(rsp->OutputOffset));
2117 *plen = 0;
2118 rc = -EIO;
2119 goto ioctl_exit;
2120 }
2121
2122 *out_data = kmalloc(*plen, GFP_KERNEL);
2123 if (*out_data == NULL) {
2124 rc = -ENOMEM;
2125 goto ioctl_exit;
2126 }
2127
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002128 shdr = get_sync_hdr(rsp);
2129 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002130ioctl_exit:
2131 free_rsp_buf(resp_buftype, rsp);
2132 return rc;
2133}
2134
Steve French64a5cfa2013-10-14 15:31:32 -05002135/*
2136 * Individual callers to ioctl worker function follow
2137 */
2138
2139int
2140SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2141 u64 persistent_fid, u64 volatile_fid)
2142{
2143 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002144 struct compress_ioctl fsctl_input;
2145 char *ret_data = NULL;
2146
2147 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002148 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002149
2150 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2151 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2152 (char *)&fsctl_input /* data input */,
2153 2 /* in data len */, &ret_data /* out data */, NULL);
2154
2155 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002156
2157 return rc;
2158}
2159
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002160int
2161SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2162 u64 persistent_fid, u64 volatile_fid)
2163{
2164 struct smb2_close_req *req;
2165 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002166 struct cifs_ses *ses = tcon->ses;
2167 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002168 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002169 int resp_buftype;
2170 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002171 int flags = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002172 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002173
Joe Perchesf96637b2013-05-04 22:12:25 -05002174 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002175
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002176 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002177 return -EIO;
2178
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002179 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002180 if (rc)
2181 return rc;
2182
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002183 if (encryption_required(tcon))
2184 flags |= CIFS_TRANSFORM_REQ;
2185
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002186 req->PersistentFileId = persistent_fid;
2187 req->VolatileFileId = volatile_fid;
2188
2189 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002190 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002191
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002192 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002193 cifs_small_buf_release(req);
2194 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002195
2196 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002197 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002198 goto close_exit;
2199 }
2200
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002201 /* BB FIXME - decode close response, update inode for caching */
2202
2203close_exit:
2204 free_rsp_buf(resp_buftype, rsp);
2205 return rc;
2206}
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002207
2208static int
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002209validate_iov(struct TCP_Server_Info *server,
2210 unsigned int offset, unsigned int buffer_length,
2211 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002212{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002213 unsigned int smb_len = iov->iov_len;
2214 char *end_of_smb = smb_len + server->vals->header_preamble_size + (char *)iov->iov_base;
2215 char *begin_of_buf = server->vals->header_preamble_size + offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002216 char *end_of_buf = begin_of_buf + buffer_length;
2217
2218
2219 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002220 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2221 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002222 return -EINVAL;
2223 }
2224
2225 /* check if beyond RFC1001 maximum length */
2226 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002227 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2228 buffer_length, smb_len);
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002229 return -EINVAL;
2230 }
2231
2232 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002233 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002234 return -EINVAL;
2235 }
2236
2237 return 0;
2238}
2239
2240/*
2241 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2242 * Caller must free buffer.
2243 */
2244static int
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002245validate_and_copy_iov(struct TCP_Server_Info *server,
2246 unsigned int offset, unsigned int buffer_length,
2247 struct kvec *iov, unsigned int minbufsize,
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002248 char *data)
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002249{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002250 char *begin_of_buf = server->vals->header_preamble_size + offset + (char *)(iov->iov_base);
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002251 int rc;
2252
2253 if (!data)
2254 return -EINVAL;
2255
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002256 rc = validate_iov(server, offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002257 if (rc)
2258 return rc;
2259
2260 memcpy(data, begin_of_buf, buffer_length);
2261
2262 return 0;
2263}
2264
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002265static int
2266query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002267 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2268 u32 additional_info, size_t output_len, size_t min_len, void **data,
2269 u32 *dlen)
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002270{
2271 struct smb2_query_info_req *req;
2272 struct smb2_query_info_rsp *rsp = NULL;
2273 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002274 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002275 int rc = 0;
2276 int resp_buftype;
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002277 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002278 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002279 unsigned int total_len;
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002280
Joe Perchesf96637b2013-05-04 22:12:25 -05002281 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002282
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002283 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002284 return -EIO;
2285
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002286 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2287 &total_len);
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002288 if (rc)
2289 return rc;
2290
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002291 if (encryption_required(tcon))
2292 flags |= CIFS_TRANSFORM_REQ;
2293
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002294 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002295 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002296 req->PersistentFileId = persistent_fid;
2297 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002298 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002299
2300 /*
2301 * We do not use the input buffer (do not send extra byte)
2302 */
2303 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002304
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002305 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002306
2307 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002308 /* 1 for Buffer */
2309 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002310
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002311 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002312 cifs_small_buf_release(req);
2313 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002314
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002315 if (rc) {
2316 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2317 goto qinf_exit;
2318 }
2319
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002320 if (dlen) {
2321 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2322 if (!*data) {
2323 *data = kmalloc(*dlen, GFP_KERNEL);
2324 if (!*data) {
2325 cifs_dbg(VFS,
2326 "Error %d allocating memory for acl\n",
2327 rc);
2328 *dlen = 0;
2329 goto qinf_exit;
2330 }
2331 }
2332 }
2333
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002334 rc = validate_and_copy_iov(ses->server,
2335 le16_to_cpu(rsp->OutputBufferOffset),
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002336 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002337 &rsp_iov, min_len, *data);
Pavel Shilovskybe4cb9e32011-12-29 17:06:33 +04002338
2339qinf_exit:
2340 free_rsp_buf(resp_buftype, rsp);
2341 return rc;
2342}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002343
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002344int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002345 u64 persistent_fid, u64 volatile_fid,
2346 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002347{
2348 return query_info(xid, tcon, persistent_fid, volatile_fid,
2349 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002350 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002351 sizeof(struct smb2_file_full_ea_info),
2352 (void **)&data,
2353 NULL);
2354}
2355
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002356int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2357 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002358{
2359 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002360 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002361 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002362 sizeof(struct smb2_file_all_info), (void **)&data,
2363 NULL);
2364}
2365
2366int
2367SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2368 u64 persistent_fid, u64 volatile_fid,
2369 void **data, u32 *plen)
2370{
2371 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2372 *plen = 0;
2373
2374 return query_info(xid, tcon, persistent_fid, volatile_fid,
2375 0, SMB2_O_INFO_SECURITY, additional_info,
2376 SMB2_MAX_BUFFER_SIZE,
2377 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002378}
2379
2380int
2381SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2382 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2383{
2384 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002385 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002386 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002387 sizeof(struct smb2_file_internal_info),
2388 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002389}
2390
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002391/*
2392 * This is a no-op for now. We're not really interested in the reply, but
2393 * rather in the fact that the server sent one and that server->lstrp
2394 * gets updated.
2395 *
2396 * FIXME: maybe we should consider checking that the reply matches request?
2397 */
2398static void
2399smb2_echo_callback(struct mid_q_entry *mid)
2400{
2401 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002402 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002403 unsigned int credits_received = 1;
2404
2405 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002406 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002407
2408 DeleteMidQEntry(mid);
2409 add_credits(server, credits_received, CIFS_ECHO_OP);
2410}
2411
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002412void smb2_reconnect_server(struct work_struct *work)
2413{
2414 struct TCP_Server_Info *server = container_of(work,
2415 struct TCP_Server_Info, reconnect.work);
2416 struct cifs_ses *ses;
2417 struct cifs_tcon *tcon, *tcon2;
2418 struct list_head tmp_list;
2419 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002420 int rc;
2421 int resched = false;
2422
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002423
2424 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2425 mutex_lock(&server->reconnect_mutex);
2426
2427 INIT_LIST_HEAD(&tmp_list);
2428 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2429
2430 spin_lock(&cifs_tcp_ses_lock);
2431 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2432 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002433 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002434 tcon->tc_count++;
2435 list_add_tail(&tcon->rlist, &tmp_list);
2436 tcon_exist = true;
2437 }
2438 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002439 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2440 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2441 tcon_exist = true;
2442 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002443 }
2444 /*
2445 * Get the reference to server struct to be sure that the last call of
2446 * cifs_put_tcon() in the loop below won't release the server pointer.
2447 */
2448 if (tcon_exist)
2449 server->srv_count++;
2450
2451 spin_unlock(&cifs_tcp_ses_lock);
2452
2453 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002454 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2455 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002456 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002457 else
2458 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002459 list_del_init(&tcon->rlist);
2460 cifs_put_tcon(tcon);
2461 }
2462
2463 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002464 if (resched)
2465 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002466 mutex_unlock(&server->reconnect_mutex);
2467
2468 /* now we can safely release srv struct */
2469 if (tcon_exist)
2470 cifs_put_tcp_session(server, 1);
2471}
2472
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002473int
2474SMB2_echo(struct TCP_Server_Info *server)
2475{
2476 struct smb2_echo_req *req;
2477 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002478 struct kvec iov[2];
2479 struct smb_rqst rqst = { .rq_iov = iov,
2480 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002481 unsigned int total_len;
2482 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002483
Joe Perchesf96637b2013-05-04 22:12:25 -05002484 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002485
Steve French4fcd1812016-06-22 20:12:05 -05002486 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002487 /* No need to send echo on newly established connections */
2488 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2489 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002490 }
2491
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002492 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002493 if (rc)
2494 return rc;
2495
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002496 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002497
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002498 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002499 rfc1002_marker = cpu_to_be32(total_len);
2500 iov[0].iov_base = &rfc1002_marker;
2501 iov[1].iov_len = total_len;
2502 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002503
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002504 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2505 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002506 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002507 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002508
2509 cifs_small_buf_release(req);
2510 return rc;
2511}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002512
2513int
2514SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2515 u64 volatile_fid)
2516{
2517 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002518 struct cifs_ses *ses = tcon->ses;
2519 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002520 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002521 int resp_buftype;
2522 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002523 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002524 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002525
Joe Perchesf96637b2013-05-04 22:12:25 -05002526 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002527
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002528 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002529 return -EIO;
2530
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002531 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002532 if (rc)
2533 return rc;
2534
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002535 if (encryption_required(tcon))
2536 flags |= CIFS_TRANSFORM_REQ;
2537
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002538 req->PersistentFileId = persistent_fid;
2539 req->VolatileFileId = volatile_fid;
2540
2541 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002542 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002543
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002544 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002545 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002546
Steve Frenchdfebe402015-03-27 01:00:06 -05002547 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002548 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2549
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002550 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002551 return rc;
2552}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002553
2554/*
2555 * To form a chain of read requests, any read requests after the first should
2556 * have the end_of_chain boolean set to true.
2557 */
2558static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002559smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002560 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2561 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002562{
2563 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002564 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002565 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002566 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002567
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002568 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2569 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002570 if (rc)
2571 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002572
2573 server = io_parms->tcon->ses->server;
2574 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002575 return -ECONNABORTED;
2576
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002577 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002578 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002579
2580 req->PersistentFileId = io_parms->persistent_fid;
2581 req->VolatileFileId = io_parms->volatile_fid;
2582 req->ReadChannelInfoOffset = 0; /* reserved */
2583 req->ReadChannelInfoLength = 0; /* reserved */
2584 req->Channel = 0; /* reserved */
2585 req->MinimumCount = 0;
2586 req->Length = cpu_to_le32(io_parms->length);
2587 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07002588#ifdef CONFIG_CIFS_SMB_DIRECT
2589 /*
2590 * If we want to do a RDMA write, fill in and append
2591 * smbd_buffer_descriptor_v1 to the end of read request
2592 */
2593 if (server->rdma && rdata &&
2594 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002595
Long Libd3dcc62017-11-22 17:38:47 -07002596 struct smbd_buffer_descriptor_v1 *v1;
2597 bool need_invalidate =
2598 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2599
2600 rdata->mr = smbd_register_mr(
2601 server->smbd_conn, rdata->pages,
2602 rdata->nr_pages, rdata->tailsz,
2603 true, need_invalidate);
2604 if (!rdata->mr)
2605 return -ENOBUFS;
2606
2607 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2608 if (need_invalidate)
2609 req->Channel = SMB2_CHANNEL_RDMA_V1;
2610 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002611 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07002612 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002613 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07002614 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002615 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2616 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2617 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07002618
2619 *total_len += sizeof(*v1) - 1;
2620 }
2621#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002622 if (request_type & CHAINED_REQUEST) {
2623 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002624 /* next 8-byte aligned request */
2625 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2626 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002627 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002628 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002629 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002630 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002631 /*
2632 * Related requests use info from previous read request
2633 * in chain.
2634 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002635 shdr->SessionId = 0xFFFFFFFF;
2636 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002637 req->PersistentFileId = 0xFFFFFFFF;
2638 req->VolatileFileId = 0xFFFFFFFF;
2639 }
2640 }
2641 if (remaining_bytes > io_parms->length)
2642 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2643 else
2644 req->RemainingBytes = 0;
2645
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002646 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002647 return rc;
2648}
2649
2650static void
2651smb2_readv_callback(struct mid_q_entry *mid)
2652{
2653 struct cifs_readdata *rdata = mid->callback_data;
2654 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2655 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002656 struct smb2_sync_hdr *shdr =
2657 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002658 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002659 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2660 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002661 .rq_pages = rdata->pages,
2662 .rq_npages = rdata->nr_pages,
2663 .rq_pagesz = rdata->pagesz,
2664 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002665
Joe Perchesf96637b2013-05-04 22:12:25 -05002666 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2667 __func__, mid->mid, mid->mid_state, rdata->result,
2668 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002669
2670 switch (mid->mid_state) {
2671 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002672 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002673 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002674 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002675 int rc;
2676
Jeff Layton0b688cf2012-09-18 16:20:34 -07002677 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002678 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002679 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2680 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002681 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002682 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002683 task_io_account_read(rdata->got_bytes);
2684 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002685 break;
2686 case MID_REQUEST_SUBMITTED:
2687 case MID_RETRY_NEEDED:
2688 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002689 if (server->sign && rdata->got_bytes)
2690 /* reset bytes number since we can not check a sign */
2691 rdata->got_bytes = 0;
2692 /* FIXME: should this be counted toward the initiating task? */
2693 task_io_account_read(rdata->got_bytes);
2694 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002695 break;
2696 default:
2697 if (rdata->result != -ENODATA)
2698 rdata->result = -EIO;
2699 }
Long Libd3dcc62017-11-22 17:38:47 -07002700#ifdef CONFIG_CIFS_SMB_DIRECT
2701 /*
2702 * If this rdata has a memmory registered, the MR can be freed
2703 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2704 * because they have limited number and are used for future I/Os
2705 */
2706 if (rdata->mr) {
2707 smbd_deregister_mr(rdata->mr);
2708 rdata->mr = NULL;
2709 }
2710#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002711 if (rdata->result)
2712 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2713
2714 queue_work(cifsiod_wq, &rdata->work);
2715 DeleteMidQEntry(mid);
2716 add_credits(server, credits_received, 0);
2717}
2718
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002719/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002720int
2721smb2_async_readv(struct cifs_readdata *rdata)
2722{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002723 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002724 char *buf;
2725 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002726 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002727 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2728 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002729 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002730 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002731 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002732
Joe Perchesf96637b2013-05-04 22:12:25 -05002733 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2734 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002735
2736 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2737 io_parms.offset = rdata->offset;
2738 io_parms.length = rdata->bytes;
2739 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2740 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2741 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002742
2743 server = io_parms.tcon->ses->server;
2744
Long Li2dabfd52017-11-07 01:54:53 -07002745 rc = smb2_new_read_req(
2746 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002747 if (rc) {
2748 if (rc == -EAGAIN && rdata->credits) {
2749 /* credits was reset by reconnect */
2750 rdata->credits = 0;
2751 /* reduce in_flight value since we won't send the req */
2752 spin_lock(&server->req_lock);
2753 server->in_flight--;
2754 spin_unlock(&server->req_lock);
2755 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002756 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002757 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002758
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002759 if (encryption_required(io_parms.tcon))
2760 flags |= CIFS_TRANSFORM_REQ;
2761
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002762 req_len = cpu_to_be32(total_len);
2763
2764 rdata->iov[0].iov_base = &req_len;
2765 rdata->iov[0].iov_len = sizeof(__be32);
2766 rdata->iov[1].iov_base = buf;
2767 rdata->iov[1].iov_len = total_len;
2768
2769 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002770
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002771 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002772 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002773 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002774 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002775 spin_lock(&server->req_lock);
2776 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002777 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002778 spin_unlock(&server->req_lock);
2779 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002780 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002781 }
2782
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002783 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002784 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002785 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002786 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002787 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002788 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002789 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2790 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002791
2792 cifs_small_buf_release(buf);
2793 return rc;
2794}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002795
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002796int
2797SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2798 unsigned int *nbytes, char **buf, int *buf_type)
2799{
2800 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002801 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002802 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002803 struct smb2_sync_hdr *shdr;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002804 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002805 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002806 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002807 int flags = CIFS_LOG_ERROR;
2808 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002809
2810 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07002811 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002812 if (rc)
2813 return rc;
2814
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002815 if (encryption_required(io_parms->tcon))
2816 flags |= CIFS_TRANSFORM_REQ;
2817
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002818 iov[0].iov_base = (char *)req;
2819 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002820
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002821 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002822 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002823
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002824 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002825
2826 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002827 if (rc != -ENODATA) {
2828 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2829 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002830 }
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002831 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2832 return rc == -ENODATA ? 0 : rc;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002833 }
2834
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002835 *nbytes = le32_to_cpu(rsp->DataLength);
2836 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2837 (*nbytes > io_parms->length)) {
2838 cifs_dbg(FYI, "bad length %d for count %d\n",
2839 *nbytes, io_parms->length);
2840 rc = -EIO;
2841 *nbytes = 0;
2842 }
2843
2844 shdr = get_sync_hdr(rsp);
2845
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002846 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002847 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002848 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002849 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002850 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002851 if (resp_buftype == CIFS_SMALL_BUFFER)
2852 *buf_type = CIFS_SMALL_BUFFER;
2853 else if (resp_buftype == CIFS_LARGE_BUFFER)
2854 *buf_type = CIFS_LARGE_BUFFER;
2855 }
2856 return rc;
2857}
2858
Pavel Shilovsky33319142012-09-18 16:20:29 -07002859/*
2860 * Check the mid_state and signature on received buffer (if any), and queue the
2861 * workqueue completion task.
2862 */
2863static void
2864smb2_writev_callback(struct mid_q_entry *mid)
2865{
2866 struct cifs_writedata *wdata = mid->callback_data;
2867 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2868 unsigned int written;
2869 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2870 unsigned int credits_received = 1;
2871
2872 switch (mid->mid_state) {
2873 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002874 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002875 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2876 if (wdata->result != 0)
2877 break;
2878
2879 written = le32_to_cpu(rsp->DataLength);
2880 /*
2881 * Mask off high 16 bits when bytes written as returned
2882 * by the server is greater than bytes requested by the
2883 * client. OS/2 servers are known to set incorrect
2884 * CountHigh values.
2885 */
2886 if (written > wdata->bytes)
2887 written &= 0xFFFF;
2888
2889 if (written < wdata->bytes)
2890 wdata->result = -ENOSPC;
2891 else
2892 wdata->bytes = written;
2893 break;
2894 case MID_REQUEST_SUBMITTED:
2895 case MID_RETRY_NEEDED:
2896 wdata->result = -EAGAIN;
2897 break;
2898 default:
2899 wdata->result = -EIO;
2900 break;
2901 }
Long Lidb223a52017-11-22 17:38:45 -07002902#ifdef CONFIG_CIFS_SMB_DIRECT
2903 /*
2904 * If this wdata has a memory registered, the MR can be freed
2905 * The number of MRs available is limited, it's important to recover
2906 * used MR as soon as I/O is finished. Hold MR longer in the later
2907 * I/O process can possibly result in I/O deadlock due to lack of MR
2908 * to send request on I/O retry
2909 */
2910 if (wdata->mr) {
2911 smbd_deregister_mr(wdata->mr);
2912 wdata->mr = NULL;
2913 }
2914#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002915 if (wdata->result)
2916 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2917
2918 queue_work(cifsiod_wq, &wdata->work);
2919 DeleteMidQEntry(mid);
2920 add_credits(tcon->ses->server, credits_received, 0);
2921}
2922
2923/* smb2_async_writev - send an async write, and set up mid to handle result */
2924int
Steve French4a5c80d2014-02-07 20:45:12 -06002925smb2_async_writev(struct cifs_writedata *wdata,
2926 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002927{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002928 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002929 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002930 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002931 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002932 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002933 struct kvec iov[2];
2934 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002935 unsigned int total_len;
2936 __be32 rfc1002_marker;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002937
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002938 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002939 if (rc) {
2940 if (rc == -EAGAIN && wdata->credits) {
2941 /* credits was reset by reconnect */
2942 wdata->credits = 0;
2943 /* reduce in_flight value since we won't send the req */
2944 spin_lock(&server->req_lock);
2945 server->in_flight--;
2946 spin_unlock(&server->req_lock);
2947 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002948 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002949 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002950
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002951 if (encryption_required(tcon))
2952 flags |= CIFS_TRANSFORM_REQ;
2953
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002954 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002955 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002956
2957 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2958 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2959 req->WriteChannelInfoOffset = 0;
2960 req->WriteChannelInfoLength = 0;
2961 req->Channel = 0;
2962 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002963 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002964 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07002965 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07002966#ifdef CONFIG_CIFS_SMB_DIRECT
2967 /*
2968 * If we want to do a server RDMA read, fill in and append
2969 * smbd_buffer_descriptor_v1 to the end of write request
2970 */
2971 if (server->rdma && wdata->bytes >=
2972 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07002973
Long Lidb223a52017-11-22 17:38:45 -07002974 struct smbd_buffer_descriptor_v1 *v1;
2975 bool need_invalidate = server->dialect == SMB30_PROT_ID;
2976
2977 wdata->mr = smbd_register_mr(
2978 server->smbd_conn, wdata->pages,
2979 wdata->nr_pages, wdata->tailsz,
2980 false, need_invalidate);
2981 if (!wdata->mr) {
2982 rc = -ENOBUFS;
2983 goto async_writev_out;
2984 }
2985 req->Length = 0;
2986 req->DataOffset = 0;
2987 req->RemainingBytes =
Steve French2026b062018-01-24 23:07:41 -06002988 cpu_to_le32((wdata->nr_pages-1)*PAGE_SIZE + wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07002989 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2990 if (need_invalidate)
2991 req->Channel = SMB2_CHANNEL_RDMA_V1;
2992 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002993 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07002994 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002995 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07002996 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002997 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
2998 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
2999 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003000 }
3001#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003002 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003003 iov[0].iov_len = 4;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003004 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
3005 iov[0].iov_base = &rfc1002_marker;
3006 iov[1].iov_len = total_len - 1;
3007 iov[1].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003008
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003009 rqst.rq_iov = iov;
3010 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003011 rqst.rq_pages = wdata->pages;
3012 rqst.rq_npages = wdata->nr_pages;
3013 rqst.rq_pagesz = wdata->pagesz;
3014 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003015#ifdef CONFIG_CIFS_SMB_DIRECT
3016 if (wdata->mr) {
3017 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3018 rqst.rq_npages = 0;
3019 }
3020#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003021 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3022 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003023
Long Lidb223a52017-11-22 17:38:45 -07003024#ifdef CONFIG_CIFS_SMB_DIRECT
3025 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3026 if (!wdata->mr)
3027 req->Length = cpu_to_le32(wdata->bytes);
3028#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003029 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003030#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003031
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003032 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003033 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003034 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003035 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003036 spin_lock(&server->req_lock);
3037 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003038 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003039 spin_unlock(&server->req_lock);
3040 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003041 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003042 }
3043
Pavel Shilovsky33319142012-09-18 16:20:29 -07003044 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003045 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3046 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003047
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003048 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06003049 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003050 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3051 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003052
Pavel Shilovsky33319142012-09-18 16:20:29 -07003053async_writev_out:
3054 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003055 return rc;
3056}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003057
3058/*
3059 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3060 * The length field from io_parms must be at least 1 and indicates a number of
3061 * elements with data to write that begins with position 1 in iov array. All
3062 * data length is specified by count.
3063 */
3064int
3065SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3066 unsigned int *nbytes, struct kvec *iov, int n_vec)
3067{
3068 int rc = 0;
3069 struct smb2_write_req *req = NULL;
3070 struct smb2_write_rsp *rsp = NULL;
3071 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003072 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003073 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003074 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003075
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003076 *nbytes = 0;
3077
3078 if (n_vec < 1)
3079 return rc;
3080
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003081 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3082 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003083 if (rc)
3084 return rc;
3085
3086 if (io_parms->tcon->ses->server == NULL)
3087 return -ECONNABORTED;
3088
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003089 if (encryption_required(io_parms->tcon))
3090 flags |= CIFS_TRANSFORM_REQ;
3091
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003092 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003093
3094 req->PersistentFileId = io_parms->persistent_fid;
3095 req->VolatileFileId = io_parms->volatile_fid;
3096 req->WriteChannelInfoOffset = 0;
3097 req->WriteChannelInfoLength = 0;
3098 req->Channel = 0;
3099 req->Length = cpu_to_le32(io_parms->length);
3100 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003101 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003102 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003103 req->RemainingBytes = 0;
3104
3105 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003106 /* 1 for Buffer */
3107 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003108
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003109 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
3110 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003111 cifs_small_buf_release(req);
3112 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003113
3114 if (rc) {
3115 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003116 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003117 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003118 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003119
3120 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003121 return rc;
3122}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003123
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003124static unsigned int
3125num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3126{
3127 int len;
3128 unsigned int entrycount = 0;
3129 unsigned int next_offset = 0;
3130 FILE_DIRECTORY_INFO *entryptr;
3131
3132 if (bufstart == NULL)
3133 return 0;
3134
3135 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3136
3137 while (1) {
3138 entryptr = (FILE_DIRECTORY_INFO *)
3139 ((char *)entryptr + next_offset);
3140
3141 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003142 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003143 break;
3144 }
3145
3146 len = le32_to_cpu(entryptr->FileNameLength);
3147 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003148 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3149 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003150 break;
3151 }
3152
3153 *lastentry = (char *)entryptr;
3154 entrycount++;
3155
3156 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3157 if (!next_offset)
3158 break;
3159 }
3160
3161 return entrycount;
3162}
3163
3164/*
3165 * Readdir/FindFirst
3166 */
3167int
3168SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3169 u64 persistent_fid, u64 volatile_fid, int index,
3170 struct cifs_search_info *srch_inf)
3171{
3172 struct smb2_query_directory_req *req;
3173 struct smb2_query_directory_rsp *rsp = NULL;
3174 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003175 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003176 int rc = 0;
3177 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003178 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003179 unsigned char *bufptr;
3180 struct TCP_Server_Info *server;
3181 struct cifs_ses *ses = tcon->ses;
3182 __le16 asteriks = cpu_to_le16('*');
3183 char *end_of_smb;
3184 unsigned int output_size = CIFSMaxBufSize;
3185 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003186 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003187 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003188
3189 if (ses && (ses->server))
3190 server = ses->server;
3191 else
3192 return -EIO;
3193
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003194 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3195 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003196 if (rc)
3197 return rc;
3198
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003199 if (encryption_required(tcon))
3200 flags |= CIFS_TRANSFORM_REQ;
3201
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003202 switch (srch_inf->info_level) {
3203 case SMB_FIND_FILE_DIRECTORY_INFO:
3204 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3205 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3206 break;
3207 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3208 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3209 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3210 break;
3211 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003212 cifs_dbg(VFS, "info level %u isn't supported\n",
3213 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003214 rc = -EINVAL;
3215 goto qdir_exit;
3216 }
3217
3218 req->FileIndex = cpu_to_le32(index);
3219 req->PersistentFileId = persistent_fid;
3220 req->VolatileFileId = volatile_fid;
3221
3222 len = 0x2;
3223 bufptr = req->Buffer;
3224 memcpy(bufptr, &asteriks, len);
3225
3226 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003227 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003228 req->FileNameLength = cpu_to_le16(len);
3229 /*
3230 * BB could be 30 bytes or so longer if we used SMB2 specific
3231 * buffer lengths, but this is safe and close enough.
3232 */
3233 output_size = min_t(unsigned int, output_size, server->maxBuf);
3234 output_size = min_t(unsigned int, output_size, 2 << 15);
3235 req->OutputBufferLength = cpu_to_le32(output_size);
3236
3237 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003238 /* 1 for Buffer */
3239 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003240
3241 iov[1].iov_base = (char *)(req->Buffer);
3242 iov[1].iov_len = len;
3243
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003244 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003245 cifs_small_buf_release(req);
3246 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003247
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003248 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003249 if (rc == -ENODATA &&
3250 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003251 srch_inf->endOfSearch = true;
3252 rc = 0;
3253 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003254 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3255 goto qdir_exit;
3256 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003257
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003258 rc = validate_iov(server,
3259 le16_to_cpu(rsp->OutputBufferOffset),
3260 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003261 info_buf_size);
3262 if (rc)
3263 goto qdir_exit;
3264
3265 srch_inf->unicode = true;
3266
3267 if (srch_inf->ntwrk_buf_start) {
3268 if (srch_inf->smallBuf)
3269 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3270 else
3271 cifs_buf_release(srch_inf->ntwrk_buf_start);
3272 }
3273 srch_inf->ntwrk_buf_start = (char *)rsp;
3274 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3275 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3276 /* 4 for rfc1002 length field */
3277 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3278 srch_inf->entries_in_buffer =
3279 num_entries(srch_inf->srch_entries_start, end_of_smb,
3280 &srch_inf->last_entry, info_buf_size);
3281 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003282 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3283 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3284 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003285 if (resp_buftype == CIFS_LARGE_BUFFER)
3286 srch_inf->smallBuf = false;
3287 else if (resp_buftype == CIFS_SMALL_BUFFER)
3288 srch_inf->smallBuf = true;
3289 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003290 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003291
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003292 return rc;
3293
3294qdir_exit:
3295 free_rsp_buf(resp_buftype, rsp);
3296 return rc;
3297}
3298
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003299static int
3300send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003301 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3302 u8 info_type, u32 additional_info, unsigned int num,
3303 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003304{
3305 struct smb2_set_info_req *req;
3306 struct smb2_set_info_rsp *rsp = NULL;
3307 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003308 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003309 int rc = 0;
3310 int resp_buftype;
3311 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003312 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003313 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003314 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003315
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003316 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003317 return -EIO;
3318
3319 if (!num)
3320 return -EINVAL;
3321
3322 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3323 if (!iov)
3324 return -ENOMEM;
3325
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003326 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003327 if (rc) {
3328 kfree(iov);
3329 return rc;
3330 }
3331
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003332 if (encryption_required(tcon))
3333 flags |= CIFS_TRANSFORM_REQ;
3334
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003335 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003336
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003337 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003338 req->FileInfoClass = info_class;
3339 req->PersistentFileId = persistent_fid;
3340 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003341 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003342
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003343 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003344 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003345 req->BufferLength = cpu_to_le32(*size);
3346
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003347 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003348 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003349
3350 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003351 /* 1 for Buffer */
3352 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003353
3354 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003355 le32_add_cpu(&req->BufferLength, size[i]);
3356 iov[i].iov_base = (char *)data[i];
3357 iov[i].iov_len = size[i];
3358 }
3359
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003360 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3361 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003362 cifs_small_buf_release(req);
3363 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003364
Steve French7d3fb242013-11-18 09:56:28 -06003365 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003366 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003367
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003368 free_rsp_buf(resp_buftype, rsp);
3369 kfree(iov);
3370 return rc;
3371}
3372
3373int
3374SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3375 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3376{
3377 struct smb2_file_rename_info info;
3378 void **data;
3379 unsigned int size[2];
3380 int rc;
3381 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3382
3383 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3384 if (!data)
3385 return -ENOMEM;
3386
3387 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3388 /* 0 = fail if target already exists */
3389 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3390 info.FileNameLength = cpu_to_le32(len);
3391
3392 data[0] = &info;
3393 size[0] = sizeof(struct smb2_file_rename_info);
3394
3395 data[1] = target_file;
3396 size[1] = len + 2 /* null */;
3397
3398 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003399 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3400 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003401 kfree(data);
3402 return rc;
3403}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003404
3405int
Steve French897fba12016-05-12 21:20:36 -05003406SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3407 u64 persistent_fid, u64 volatile_fid)
3408{
3409 __u8 delete_pending = 1;
3410 void *data;
3411 unsigned int size;
3412
3413 data = &delete_pending;
3414 size = 1; /* sizeof __u8 */
3415
3416 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003417 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3418 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003419}
3420
3421int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003422SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3423 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3424{
3425 struct smb2_file_link_info info;
3426 void **data;
3427 unsigned int size[2];
3428 int rc;
3429 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3430
3431 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3432 if (!data)
3433 return -ENOMEM;
3434
3435 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3436 /* 0 = fail if link already exists */
3437 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3438 info.FileNameLength = cpu_to_le32(len);
3439
3440 data[0] = &info;
3441 size[0] = sizeof(struct smb2_file_link_info);
3442
3443 data[1] = target_file;
3444 size[1] = len + 2 /* null */;
3445
3446 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003447 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3448 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003449 kfree(data);
3450 return rc;
3451}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003452
3453int
3454SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003455 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003456{
3457 struct smb2_file_eof_info info;
3458 void *data;
3459 unsigned int size;
3460
3461 info.EndOfFile = *eof;
3462
3463 data = &info;
3464 size = sizeof(struct smb2_file_eof_info);
3465
Steve Frenchf29ebb42014-07-19 21:44:58 -05003466 if (is_falloc)
3467 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003468 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3469 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003470 else
3471 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003472 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3473 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003474}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003475
3476int
3477SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3478 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3479{
3480 unsigned int size;
3481 size = sizeof(FILE_BASIC_INFO);
3482 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003483 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3484 0, 1, (void **)&buf, &size);
3485}
3486
3487int
3488SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3489 u64 persistent_fid, u64 volatile_fid,
3490 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3491{
3492 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3493 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3494 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003495}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003496
3497int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003498SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3499 u64 persistent_fid, u64 volatile_fid,
3500 struct smb2_file_full_ea_info *buf, int len)
3501{
3502 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3503 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3504 0, 1, (void **)&buf, &len);
3505}
3506
3507int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003508SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3509 const u64 persistent_fid, const u64 volatile_fid,
3510 __u8 oplock_level)
3511{
3512 int rc;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003513 struct smb2_oplock_break_req *req = NULL;
3514 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003515 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003516 unsigned int total_len;
3517 struct kvec iov[1];
3518 struct kvec rsp_iov;
3519 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003520
Joe Perchesf96637b2013-05-04 22:12:25 -05003521 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003522 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3523 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003524 if (rc)
3525 return rc;
3526
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003527 if (encryption_required(tcon))
3528 flags |= CIFS_TRANSFORM_REQ;
3529
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003530 req->VolatileFid = volatile_fid;
3531 req->PersistentFid = persistent_fid;
3532 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003533 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003534
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003535 flags |= CIFS_NO_RESP;
3536
3537 iov[0].iov_base = (char *)req;
3538 iov[0].iov_len = total_len;
3539
3540 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003541 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003542
3543 if (rc) {
3544 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003545 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003546 }
3547
3548 return rc;
3549}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003550
3551static void
3552copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3553 struct kstatfs *kst)
3554{
3555 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3556 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3557 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303558 kst->f_bfree = kst->f_bavail =
3559 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003560 return;
3561}
3562
3563static int
3564build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3565 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3566{
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003567 struct TCP_Server_Info *server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003568 int rc;
3569 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003570 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003571
Joe Perchesf96637b2013-05-04 22:12:25 -05003572 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003573
3574 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3575 return -EIO;
3576
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003577 server = tcon->ses->server;
3578
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003579 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3580 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003581 if (rc)
3582 return rc;
3583
3584 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3585 req->FileInfoClass = level;
3586 req->PersistentFileId = persistent_fid;
3587 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003588 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003589 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003590 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003591 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003592 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - server->vals->header_preamble_size);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003593
3594 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003595 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003596 return 0;
3597}
3598
3599int
3600SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3601 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3602{
3603 struct smb2_query_info_rsp *rsp = NULL;
3604 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003605 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003606 int rc = 0;
3607 int resp_buftype;
3608 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003609 struct TCP_Server_Info *server = ses->server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003610 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003611 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003612
3613 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3614 sizeof(struct smb2_fs_full_size_info),
3615 persistent_fid, volatile_fid);
3616 if (rc)
3617 return rc;
3618
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003619 if (encryption_required(tcon))
3620 flags |= CIFS_TRANSFORM_REQ;
3621
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003622 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003623 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003624 if (rc) {
3625 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003626 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003627 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003628 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003629
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003630 info = (struct smb2_fs_full_size_info *)(server->vals->header_preamble_size +
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003631 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003632 rc = validate_iov(server,
3633 le16_to_cpu(rsp->OutputBufferOffset),
3634 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003635 sizeof(struct smb2_fs_full_size_info));
3636 if (!rc)
3637 copy_fs_info_to_kstatfs(info, fsdata);
3638
Steve French34f62642013-10-09 02:07:00 -05003639qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003640 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003641 return rc;
3642}
3643
3644int
3645SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003646 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003647{
3648 struct smb2_query_info_rsp *rsp = NULL;
3649 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003650 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003651 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003652 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003653 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003654 struct TCP_Server_Info *server = ses->server;
Steve French34f62642013-10-09 02:07:00 -05003655 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003656 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003657
Steven French21671142013-10-09 13:36:35 -05003658 if (level == FS_DEVICE_INFORMATION) {
3659 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3660 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3661 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3662 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3663 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003664 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3665 max_len = sizeof(struct smb3_fs_ss_info);
3666 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003667 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003668 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003669 return -EINVAL;
3670 }
3671
3672 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003673 persistent_fid, volatile_fid);
3674 if (rc)
3675 return rc;
3676
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003677 if (encryption_required(tcon))
3678 flags |= CIFS_TRANSFORM_REQ;
3679
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003680 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003681 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003682 if (rc) {
3683 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3684 goto qfsattr_exit;
3685 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003686 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003687
3688 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3689 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003690 rc = validate_iov(server, offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05003691 if (rc)
3692 goto qfsattr_exit;
3693
3694 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003695 memcpy(&tcon->fsAttrInfo, server->vals->header_preamble_size + offset
Steve French34f62642013-10-09 02:07:00 -05003696 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003697 rsp_len, max_len));
3698 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003699 memcpy(&tcon->fsDevInfo, server->vals->header_preamble_size + offset
Steven French21671142013-10-09 13:36:35 -05003700 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003701 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3702 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003703 (server->vals->header_preamble_size + offset + (char *)&rsp->hdr);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003704 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3705 tcon->perf_sector_size =
3706 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3707 }
Steve French34f62642013-10-09 02:07:00 -05003708
3709qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003710 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003711 return rc;
3712}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003713
3714int
3715smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3716 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3717 const __u32 num_lock, struct smb2_lock_element *buf)
3718{
3719 int rc = 0;
3720 struct smb2_lock_req *req = NULL;
3721 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003722 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003723 int resp_buf_type;
3724 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003725 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003726 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003727
Joe Perchesf96637b2013-05-04 22:12:25 -05003728 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003729
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003730 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003731 if (rc)
3732 return rc;
3733
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003734 if (encryption_required(tcon))
3735 flags |= CIFS_TRANSFORM_REQ;
3736
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003737 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003738 req->LockCount = cpu_to_le16(num_lock);
3739
3740 req->PersistentFileId = persist_fid;
3741 req->VolatileFileId = volatile_fid;
3742
3743 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003744
3745 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003746 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003747 iov[1].iov_base = (char *)buf;
3748 iov[1].iov_len = count;
3749
3750 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003751 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3752 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003753 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003754 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003755 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003756 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3757 }
3758
3759 return rc;
3760}
3761
3762int
3763SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3764 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3765 const __u64 length, const __u64 offset, const __u32 lock_flags,
3766 const bool wait)
3767{
3768 struct smb2_lock_element lock;
3769
3770 lock.Offset = cpu_to_le64(offset);
3771 lock.Length = cpu_to_le64(length);
3772 lock.Flags = cpu_to_le32(lock_flags);
3773 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3774 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3775
3776 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3777}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003778
3779int
3780SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3781 __u8 *lease_key, const __le32 lease_state)
3782{
3783 int rc;
3784 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003785 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003786 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003787 unsigned int total_len;
3788 struct kvec iov[1];
3789 struct kvec rsp_iov;
3790 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003791
Joe Perchesf96637b2013-05-04 22:12:25 -05003792 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003793 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3794 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003795 if (rc)
3796 return rc;
3797
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003798 if (encryption_required(tcon))
3799 flags |= CIFS_TRANSFORM_REQ;
3800
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003801 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003802 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003803 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003804
3805 memcpy(req->LeaseKey, lease_key, 16);
3806 req->LeaseState = lease_state;
3807
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003808 flags |= CIFS_NO_RESP;
3809
3810 iov[0].iov_base = (char *)req;
3811 iov[0].iov_len = total_len;
3812
3813 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003814 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003815
3816 if (rc) {
3817 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003818 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003819 }
3820
3821 return rc;
3822}