blob: e01be06d588ee1148109904cf057109bbddb6104 [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
477 pctx = (struct smb2_neg_context *)(offset + 4 + (char *)rsp);
478 clen = le16_to_cpu(pctx->DataLength);
479 if (clen > len_of_ctxts)
480 break;
481
482 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
483 decode_preauth_context(
484 (struct smb2_preauth_neg_context *)pctx);
485 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
486 rc = decode_encrypt_ctx(server,
487 (struct smb2_encryption_neg_context *)pctx);
488 else
489 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
490 le16_to_cpu(pctx->ContextType));
491
492 if (rc)
493 break;
494 /* offsets must be 8 byte aligned */
495 clen = (clen + 7) & ~0x7;
496 offset += clen + sizeof(struct smb2_neg_context);
497 len_of_ctxts -= clen;
498 }
499 return rc;
500}
501
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500502#else
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100503static void assemble_neg_contexts(struct smb2_negotiate_req *req,
504 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500505{
506 return;
507}
508#endif /* SMB311 */
509
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400510/*
511 *
512 * SMB2 Worker functions follow:
513 *
514 * The general structure of the worker functions is:
515 * 1) Call smb2_init (assembles SMB2 header)
516 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
517 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
518 * 4) Decode SMB2 command specific fields in the fixed length area
519 * 5) Decode variable length data area (if any for this SMB2 command type)
520 * 6) Call free smb buffer
521 * 7) return
522 *
523 */
524
525int
526SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
527{
528 struct smb2_negotiate_req *req;
529 struct smb2_negotiate_rsp *rsp;
530 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700531 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400532 int rc = 0;
533 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400534 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400535 int blob_offset, blob_length;
536 char *security_blob;
537 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100538 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400539
Joe Perchesf96637b2013-05-04 22:12:25 -0500540 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400541
Jeff Layton3534b852013-05-24 07:41:01 -0400542 if (!server) {
543 WARN(1, "%s: server is NULL!\n", __func__);
544 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400545 }
546
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100547 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400548 if (rc)
549 return rc;
550
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100551 req->sync_hdr.SessionId = 0;
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100552#ifdef CONFIG_CIFS_SMB311
553 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
554 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
555#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400556
Steve French9764c022017-09-17 10:41:35 -0500557 if (strcmp(ses->server->vals->version_string,
558 SMB3ANY_VERSION_STRING) == 0) {
559 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
560 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
561 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100562 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500563 } else if (strcmp(ses->server->vals->version_string,
564 SMBDEFAULT_VERSION_STRING) == 0) {
565 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
566 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
567 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
568 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100569 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500570 } else {
571 /* otherwise send specific dialect */
572 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
573 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100574 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500575 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400576
577 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400578 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500579 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400580 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500581 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400582 else
583 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400584
Steve Frenche4aa25e2012-10-01 12:26:22 -0500585 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400586
Steve French3c5f9be12014-05-13 13:37:45 -0700587 /* ClientGUID must be zero for SMB2.02 dialect */
588 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
589 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500590 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700591 memcpy(req->ClientGUID, server->client_guid,
592 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500593 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100594 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500595 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400596 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100597 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400598
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100599 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700600 cifs_small_buf_release(req);
601 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400602 /*
603 * No tcon so can't do
604 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
605 */
Steve French7e682f72017-08-31 21:34:24 -0500606 if (rc == -EOPNOTSUPP) {
607 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500608 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500609 " older servers\n");
610 goto neg_exit;
611 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400612 goto neg_exit;
613
Steve French9764c022017-09-17 10:41:35 -0500614 if (strcmp(ses->server->vals->version_string,
615 SMB3ANY_VERSION_STRING) == 0) {
616 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
617 cifs_dbg(VFS,
618 "SMB2 dialect returned but not requested\n");
619 return -EIO;
620 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
621 cifs_dbg(VFS,
622 "SMB2.1 dialect returned but not requested\n");
623 return -EIO;
624 }
625 } else if (strcmp(ses->server->vals->version_string,
626 SMBDEFAULT_VERSION_STRING) == 0) {
627 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
628 cifs_dbg(VFS,
629 "SMB2 dialect returned but not requested\n");
630 return -EIO;
631 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
632 /* ops set to 3.0 by default for default so update */
633 ses->server->ops = &smb21_operations;
634 }
Steve French590d08d2017-09-19 11:43:47 -0500635 } else if (le16_to_cpu(rsp->DialectRevision) !=
636 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500637 /* if requested single dialect ensure returned dialect matched */
638 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500639 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500640 return -EIO;
641 }
642
Joe Perchesf96637b2013-05-04 22:12:25 -0500643 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400644
Steve Frenche4aa25e2012-10-01 12:26:22 -0500645 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500646 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500647 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500648 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500649 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500650 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500651 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
652 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600653#ifdef CONFIG_CIFS_SMB311
654 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
655 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
656#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400657 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500658 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500659 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400660 rc = -EIO;
661 goto neg_exit;
662 }
663 server->dialect = le16_to_cpu(rsp->DialectRevision);
664
Steve French9764c022017-09-17 10:41:35 -0500665 /* BB: add check that dialect was valid given dialect(s) we asked for */
666
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100667#ifdef CONFIG_CIFS_SMB311
668 /*
669 * Keep a copy of the hash after negprot. This hash will be
670 * the starting hash value for all sessions made from this
671 * server.
672 */
673 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
674 SMB2_PREAUTH_HASH_SIZE);
675#endif
Jeff Laytone598d1d82013-05-26 07:00:59 -0400676 /* SMB2 only has an extended negflavor */
677 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400678 /* set it to the maximum buffer size value we can send with 1 credit */
679 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
680 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400681 server->max_read = le32_to_cpu(rsp->MaxReadSize);
682 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400683 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500684 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
685 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
686 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400687 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400688 /* Internal types */
689 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400690
691 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
692 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500693 /*
694 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
695 * for us will be
696 * ses->sectype = RawNTLMSSP;
697 * but for time being this is our only auth choice so doesn't matter.
698 * We just found a server which sets blob length to zero expecting raw.
699 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700700 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500701 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700702 server->sec_ntlmssp = true;
703 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700704
Jeff Layton38d77c52013-05-26 07:01:00 -0400705 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400706 if (rc)
707 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500708 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500709 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500710 if (rc == 1)
711 rc = 0;
712 else if (rc == 0)
713 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400714 }
Steve French5100d8a2018-04-09 10:47:14 -0500715
716#ifdef CONFIG_CIFS_SMB311
717 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
718 if (rsp->NegotiateContextCount)
719 rc = smb311_decode_neg_context(rsp, server);
720 else
721 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
722 }
723#endif /* CONFIG_CIFS_SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400724neg_exit:
725 free_rsp_buf(resp_buftype, rsp);
726 return rc;
727}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400728
Steve Frenchff1c0382013-11-19 23:44:46 -0600729int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
730{
731 int rc = 0;
732 struct validate_negotiate_info_req vneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200733 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600734 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500735 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600736
737 cifs_dbg(FYI, "validate negotiate\n");
738
Long Li8801e902017-11-22 17:38:49 -0700739#ifdef CONFIG_CIFS_SMB_DIRECT
740 if (tcon->ses->server->rdma)
741 return 0;
742#endif
743
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100744 /* In SMB3.11 preauth integrity supersedes validate negotiate */
745 if (tcon->ses->server->dialect == SMB311_PROT_ID)
746 return 0;
747
Steve Frenchff1c0382013-11-19 23:44:46 -0600748 /*
749 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500750 * can not sign it (ie are not known user). Even if signing is not
751 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600752 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500753 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600754 */
Steve French0603c962017-09-20 19:57:18 -0500755 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600756 return 0; /* validation requires signing */
757
Steve French0603c962017-09-20 19:57:18 -0500758 if (tcon->ses->user_name == NULL) {
759 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
760 return 0; /* validation requires signing */
761 }
762
763 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
764 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
765
Steve Frenchff1c0382013-11-19 23:44:46 -0600766 vneg_inbuf.Capabilities =
767 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100768 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
769 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600770
771 if (tcon->ses->sign)
772 vneg_inbuf.SecurityMode =
773 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
774 else if (global_secflags & CIFSSEC_MAY_SIGN)
775 vneg_inbuf.SecurityMode =
776 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
777 else
778 vneg_inbuf.SecurityMode = 0;
779
Steve French9764c022017-09-17 10:41:35 -0500780
781 if (strcmp(tcon->ses->server->vals->version_string,
782 SMB3ANY_VERSION_STRING) == 0) {
783 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
784 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
785 vneg_inbuf.DialectCount = cpu_to_le16(2);
786 /* structure is big enough for 3 dialects, sending only 2 */
787 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
788 } else if (strcmp(tcon->ses->server->vals->version_string,
789 SMBDEFAULT_VERSION_STRING) == 0) {
790 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
791 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
792 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
793 vneg_inbuf.DialectCount = cpu_to_le16(3);
794 /* structure is big enough for 3 dialects */
795 inbuflen = sizeof(struct validate_negotiate_info_req);
796 } else {
797 /* otherwise specific dialect was requested */
798 vneg_inbuf.Dialects[0] =
799 cpu_to_le16(tcon->ses->server->vals->protocol_id);
800 vneg_inbuf.DialectCount = cpu_to_le16(1);
801 /* structure is big enough for 3 dialects, sending only 1 */
802 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
803 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600804
805 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
806 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
807 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
808 (char **)&pneg_rsp, &rsplen);
809
810 if (rc != 0) {
811 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
812 return -EIO;
813 }
814
815 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500816 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
817 rsplen);
818
819 /* relax check since Mac returns max bufsize allowed on ioctl */
David Disseldorpa2d9daa2017-10-20 14:49:38 +0200820 if ((rsplen > CIFSMaxBufSize)
821 || (rsplen < sizeof(struct validate_negotiate_info_rsp)))
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200822 goto err_rsp_free;
Steve Frenchff1c0382013-11-19 23:44:46 -0600823 }
824
825 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +0100826 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600827 goto vneg_out;
828
829 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
830 goto vneg_out;
831
832 /* do not validate server guid because not saved at negprot time yet */
833
834 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
835 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
836 goto vneg_out;
837
838 /* validate negotiate successful */
839 cifs_dbg(FYI, "validate negotiate info successful\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200840 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600841 return 0;
842
843vneg_out:
844 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200845err_rsp_free:
846 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600847 return -EIO;
848}
849
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530850enum securityEnum
851smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
852{
853 switch (requested) {
854 case Kerberos:
855 case RawNTLMSSP:
856 return requested;
857 case NTLMv2:
858 return RawNTLMSSP;
859 case Unspecified:
860 if (server->sec_ntlmssp &&
861 (global_secflags & CIFSSEC_MAY_NTLMSSP))
862 return RawNTLMSSP;
863 if ((server->sec_kerberos || server->sec_mskerberos) &&
864 (global_secflags & CIFSSEC_MAY_KRB5))
865 return Kerberos;
866 /* Fallthrough */
867 default:
868 return Unspecified;
869 }
870}
871
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100872struct SMB2_sess_data {
873 unsigned int xid;
874 struct cifs_ses *ses;
875 struct nls_table *nls_cp;
876 void (*func)(struct SMB2_sess_data *);
877 int result;
878 u64 previous_session;
879
880 /* we will send the SMB in three pieces:
881 * a fixed length beginning part, an optional
882 * SPNEGO blob (which can be zero length), and a
883 * last part which will include the strings
884 * and rest of bcc area. This allows us to avoid
885 * a large buffer 17K allocation
886 */
887 int buf0_type;
888 struct kvec iov[2];
889};
890
891static int
892SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
893{
894 int rc;
895 struct cifs_ses *ses = sess_data->ses;
896 struct smb2_sess_setup_req *req;
897 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100898 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100899
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100900 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
901 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100902 if (rc)
903 return rc;
904
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700905 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100906 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100907
908 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
909 req->PreviousSessionId = sess_data->previous_session;
910
911 req->Flags = 0; /* MBZ */
912 /* to enable echos and oplocks */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100913 req->sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100914
915 /* only one of SMB2 signing flags may be set in SMB2 request */
916 if (server->sign)
917 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
918 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
919 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
920 else
921 req->SecurityMode = 0;
922
923 req->Capabilities = 0;
924 req->Channel = 0; /* MBZ */
925
926 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100927 /* 1 for pad */
928 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100929 /*
930 * This variable will be used to clear the buffer
931 * allocated above in case of any error in the calling function.
932 */
933 sess_data->buf0_type = CIFS_SMALL_BUFFER;
934
935 return 0;
936}
937
938static void
939SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
940{
941 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
942 sess_data->buf0_type = CIFS_NO_BUFFER;
943}
944
945static int
946SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
947{
948 int rc;
949 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700950 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100951
952 /* Testing shows that buffer offset must be at location of Buffer[0] */
953 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100954 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100955 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
956
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100957 /* BB add code to build os and lm fields */
958
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100959 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
960 sess_data->iov, 2,
961 &sess_data->buf0_type,
962 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700963 cifs_small_buf_release(sess_data->iov[0].iov_base);
964 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100965
966 return rc;
967}
968
969static int
970SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
971{
972 int rc = 0;
973 struct cifs_ses *ses = sess_data->ses;
974
975 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800976 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100977 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100978 if (rc) {
979 cifs_dbg(FYI,
980 "SMB3 session key generation failed\n");
981 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800982 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100983 }
984 }
985 if (!ses->server->session_estab) {
986 ses->server->sequence_number = 0x2;
987 ses->server->session_estab = true;
988 }
989 mutex_unlock(&ses->server->srv_mutex);
990
991 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
992 spin_lock(&GlobalMid_Lock);
993 ses->status = CifsGood;
994 ses->need_reconnect = false;
995 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100996 return rc;
997}
998
999#ifdef CONFIG_CIFS_UPCALL
1000static void
1001SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1002{
1003 int rc;
1004 struct cifs_ses *ses = sess_data->ses;
1005 struct cifs_spnego_msg *msg;
1006 struct key *spnego_key = NULL;
1007 struct smb2_sess_setup_rsp *rsp = NULL;
1008
1009 rc = SMB2_sess_alloc_buffer(sess_data);
1010 if (rc)
1011 goto out;
1012
1013 spnego_key = cifs_get_spnego_key(ses);
1014 if (IS_ERR(spnego_key)) {
1015 rc = PTR_ERR(spnego_key);
1016 spnego_key = NULL;
1017 goto out;
1018 }
1019
1020 msg = spnego_key->payload.data[0];
1021 /*
1022 * check version field to make sure that cifs.upcall is
1023 * sending us a response in an expected form
1024 */
1025 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1026 cifs_dbg(VFS,
1027 "bad cifs.upcall version. Expected %d got %d",
1028 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1029 rc = -EKEYREJECTED;
1030 goto out_put_spnego_key;
1031 }
1032
1033 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1034 GFP_KERNEL);
1035 if (!ses->auth_key.response) {
1036 cifs_dbg(VFS,
1037 "Kerberos can't allocate (%u bytes) memory",
1038 msg->sesskey_len);
1039 rc = -ENOMEM;
1040 goto out_put_spnego_key;
1041 }
1042 ses->auth_key.len = msg->sesskey_len;
1043
1044 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1045 sess_data->iov[1].iov_len = msg->secblob_len;
1046
1047 rc = SMB2_sess_sendreceive(sess_data);
1048 if (rc)
1049 goto out_put_spnego_key;
1050
1051 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001052 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001053
1054 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001055
1056 rc = SMB2_sess_establish_session(sess_data);
1057out_put_spnego_key:
1058 key_invalidate(spnego_key);
1059 key_put(spnego_key);
1060out:
1061 sess_data->result = rc;
1062 sess_data->func = NULL;
1063 SMB2_sess_free_buffer(sess_data);
1064}
1065#else
1066static void
1067SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1068{
1069 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1070 sess_data->result = -EOPNOTSUPP;
1071 sess_data->func = NULL;
1072}
1073#endif
1074
Sachin Prabhu166cea42016-10-07 19:11:22 +01001075static void
1076SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1077
1078static void
1079SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1080{
1081 int rc;
1082 struct cifs_ses *ses = sess_data->ses;
1083 struct smb2_sess_setup_rsp *rsp = NULL;
1084 char *ntlmssp_blob = NULL;
1085 bool use_spnego = false; /* else use raw ntlmssp */
1086 u16 blob_length = 0;
1087
1088 /*
1089 * If memory allocation is successful, caller of this function
1090 * frees it.
1091 */
1092 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1093 if (!ses->ntlmssp) {
1094 rc = -ENOMEM;
1095 goto out_err;
1096 }
1097 ses->ntlmssp->sesskey_per_smbsess = true;
1098
1099 rc = SMB2_sess_alloc_buffer(sess_data);
1100 if (rc)
1101 goto out_err;
1102
1103 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1104 GFP_KERNEL);
1105 if (ntlmssp_blob == NULL) {
1106 rc = -ENOMEM;
1107 goto out;
1108 }
1109
1110 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1111 if (use_spnego) {
1112 /* BB eventually need to add this */
1113 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1114 rc = -EOPNOTSUPP;
1115 goto out;
1116 } else {
1117 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1118 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1119 }
1120 sess_data->iov[1].iov_base = ntlmssp_blob;
1121 sess_data->iov[1].iov_len = blob_length;
1122
1123 rc = SMB2_sess_sendreceive(sess_data);
1124 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1125
1126 /* If true, rc here is expected and not an error */
1127 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001128 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001129 rc = 0;
1130
1131 if (rc)
1132 goto out;
1133
1134 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1135 le16_to_cpu(rsp->SecurityBufferOffset)) {
1136 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1137 le16_to_cpu(rsp->SecurityBufferOffset));
1138 rc = -EIO;
1139 goto out;
1140 }
1141 rc = decode_ntlmssp_challenge(rsp->Buffer,
1142 le16_to_cpu(rsp->SecurityBufferLength), ses);
1143 if (rc)
1144 goto out;
1145
1146 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1147
1148
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001149 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001150 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001151
1152out:
1153 kfree(ntlmssp_blob);
1154 SMB2_sess_free_buffer(sess_data);
1155 if (!rc) {
1156 sess_data->result = 0;
1157 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1158 return;
1159 }
1160out_err:
1161 kfree(ses->ntlmssp);
1162 ses->ntlmssp = NULL;
1163 sess_data->result = rc;
1164 sess_data->func = NULL;
1165}
1166
1167static void
1168SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1169{
1170 int rc;
1171 struct cifs_ses *ses = sess_data->ses;
1172 struct smb2_sess_setup_req *req;
1173 struct smb2_sess_setup_rsp *rsp = NULL;
1174 unsigned char *ntlmssp_blob = NULL;
1175 bool use_spnego = false; /* else use raw ntlmssp */
1176 u16 blob_length = 0;
1177
1178 rc = SMB2_sess_alloc_buffer(sess_data);
1179 if (rc)
1180 goto out;
1181
1182 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001183 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001184
1185 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1186 sess_data->nls_cp);
1187 if (rc) {
1188 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1189 goto out;
1190 }
1191
1192 if (use_spnego) {
1193 /* BB eventually need to add this */
1194 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1195 rc = -EOPNOTSUPP;
1196 goto out;
1197 }
1198 sess_data->iov[1].iov_base = ntlmssp_blob;
1199 sess_data->iov[1].iov_len = blob_length;
1200
1201 rc = SMB2_sess_sendreceive(sess_data);
1202 if (rc)
1203 goto out;
1204
1205 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1206
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001207 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001208 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001209
1210 rc = SMB2_sess_establish_session(sess_data);
1211out:
1212 kfree(ntlmssp_blob);
1213 SMB2_sess_free_buffer(sess_data);
1214 kfree(ses->ntlmssp);
1215 ses->ntlmssp = NULL;
1216 sess_data->result = rc;
1217 sess_data->func = NULL;
1218}
1219
1220static int
1221SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1222{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301223 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001224
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301225 type = smb2_select_sectype(ses->server, ses->sectype);
1226 cifs_dbg(FYI, "sess setup type %d\n", type);
1227 if (type == Unspecified) {
1228 cifs_dbg(VFS,
1229 "Unable to select appropriate authentication method!");
1230 return -EINVAL;
1231 }
1232
1233 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001234 case Kerberos:
1235 sess_data->func = SMB2_auth_kerberos;
1236 break;
1237 case RawNTLMSSP:
1238 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1239 break;
1240 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301241 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001242 return -EOPNOTSUPP;
1243 }
1244
1245 return 0;
1246}
1247
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001248int
1249SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1250 const struct nls_table *nls_cp)
1251{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001252 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001253 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001254 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001255
Joe Perchesf96637b2013-05-04 22:12:25 -05001256 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001257
Jeff Layton3534b852013-05-24 07:41:01 -04001258 if (!server) {
1259 WARN(1, "%s: server is NULL!\n", __func__);
1260 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001261 }
1262
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001263 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1264 if (!sess_data)
1265 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001266
1267 rc = SMB2_select_sec(ses, sess_data);
1268 if (rc)
1269 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001270 sess_data->xid = xid;
1271 sess_data->ses = ses;
1272 sess_data->buf0_type = CIFS_NO_BUFFER;
1273 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001274
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001275#ifdef CONFIG_CIFS_SMB311
1276 /*
1277 * Initialize the session hash with the server one.
1278 */
1279 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1280 SMB2_PREAUTH_HASH_SIZE);
1281#endif
1282
Sachin Prabhu166cea42016-10-07 19:11:22 +01001283 while (sess_data->func)
1284 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001285
Steve Frenchc721c382017-09-19 18:40:03 -05001286 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1287 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001288 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001289out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001290 kfree(sess_data);
1291 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001292}
1293
1294int
1295SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1296{
1297 struct smb2_logoff_req *req; /* response is also trivial struct */
1298 int rc = 0;
1299 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001300 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001301 unsigned int total_len;
1302 struct kvec iov[1];
1303 struct kvec rsp_iov;
1304 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001305
Joe Perchesf96637b2013-05-04 22:12:25 -05001306 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001307
1308 if (ses && (ses->server))
1309 server = ses->server;
1310 else
1311 return -EIO;
1312
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001313 /* no need to send SMB logoff if uid already closed due to reconnect */
1314 if (ses->need_reconnect)
1315 goto smb2_session_already_dead;
1316
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001317 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001318 if (rc)
1319 return rc;
1320
1321 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001322 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001323
1324 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1325 flags |= CIFS_TRANSFORM_REQ;
1326 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001327 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001328
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001329 flags |= CIFS_NO_RESP;
1330
1331 iov[0].iov_base = (char *)req;
1332 iov[0].iov_len = total_len;
1333
1334 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001335 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001336 /*
1337 * No tcon so can't do
1338 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1339 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001340
1341smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001342 return rc;
1343}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001344
1345static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1346{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001347 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001348}
1349
1350#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1351
Steve Frenchde9f68d2013-11-15 11:26:24 -06001352/* These are similar values to what Windows uses */
1353static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1354{
1355 tcon->max_chunks = 256;
1356 tcon->max_bytes_chunk = 1048576;
1357 tcon->max_bytes_copy = 16777216;
1358}
1359
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001360int
1361SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1362 struct cifs_tcon *tcon, const struct nls_table *cp)
1363{
1364 struct smb2_tree_connect_req *req;
1365 struct smb2_tree_connect_rsp *rsp = NULL;
1366 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001367 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001368 int rc = 0;
1369 int resp_buftype;
1370 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001371 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001372 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001373 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001374
Joe Perchesf96637b2013-05-04 22:12:25 -05001375 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001376
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001377 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001378 return -EIO;
1379
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001380 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1381 if (unc_path == NULL)
1382 return -ENOMEM;
1383
1384 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1385 unc_path_len *= 2;
1386 if (unc_path_len < 2) {
1387 kfree(unc_path);
1388 return -EINVAL;
1389 }
1390
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001391 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001392 tcon->tid = 0;
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001393
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001394 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1395 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001396 if (rc) {
1397 kfree(unc_path);
1398 return rc;
1399 }
1400
Aurelien Aptelb327a712018-01-24 13:46:10 +01001401 if (encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001402 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001403
1404 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001405 /* 1 for pad */
1406 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001407
1408 /* Testing shows that buffer offset must be at location of Buffer[0] */
1409 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001410 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001411 req->PathLength = cpu_to_le16(unc_path_len - 2);
1412 iov[1].iov_base = unc_path;
1413 iov[1].iov_len = unc_path_len;
1414
Steve French6188f282018-03-13 02:29:36 -05001415 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1416 if ((ses->server->dialect == SMB311_PROT_ID) &&
1417 !encryption_required(tcon))
1418 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1419
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001420 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001421 cifs_small_buf_release(req);
1422 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001423
1424 if (rc != 0) {
1425 if (tcon) {
1426 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1427 tcon->need_reconnect = true;
1428 }
1429 goto tcon_error_exit;
1430 }
1431
Christophe JAILLETcd123002017-05-12 17:59:32 +02001432 switch (rsp->ShareType) {
1433 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001434 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001435 break;
1436 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001437 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001438 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001439 break;
1440 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001441 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001442 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001443 break;
1444 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001445 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001446 rc = -EOPNOTSUPP;
1447 goto tcon_error_exit;
1448 }
1449
1450 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001451 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001452 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1453 tcon->tidStatus = CifsGood;
1454 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001455 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001456 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001457
1458 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1459 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001460 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001461
1462 if (tcon->seal &&
1463 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1464 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1465
Steve Frenchde9f68d2013-11-15 11:26:24 -06001466 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001467 if (tcon->ses->server->ops->validate_negotiate)
1468 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001469tcon_exit:
1470 free_rsp_buf(resp_buftype, rsp);
1471 kfree(unc_path);
1472 return rc;
1473
1474tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001475 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001476 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001477 }
1478 goto tcon_exit;
1479}
1480
1481int
1482SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1483{
1484 struct smb2_tree_disconnect_req *req; /* response is trivial */
1485 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001486 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001487 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001488 unsigned int total_len;
1489 struct kvec iov[1];
1490 struct kvec rsp_iov;
1491 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001492
Joe Perchesf96637b2013-05-04 22:12:25 -05001493 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001494
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001495 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001496 return -EIO;
1497
1498 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1499 return 0;
1500
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001501 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1502 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001503 if (rc)
1504 return rc;
1505
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001506 if (encryption_required(tcon))
1507 flags |= CIFS_TRANSFORM_REQ;
1508
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001509 flags |= CIFS_NO_RESP;
1510
1511 iov[0].iov_base = (char *)req;
1512 iov[0].iov_len = total_len;
1513
1514 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001515 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001516 if (rc)
1517 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1518
1519 return rc;
1520}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001521
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001522
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001523static struct create_durable *
1524create_durable_buf(void)
1525{
1526 struct create_durable *buf;
1527
1528 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1529 if (!buf)
1530 return NULL;
1531
1532 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001533 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001534 buf->ccontext.DataLength = cpu_to_le32(16);
1535 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1536 (struct create_durable, Name));
1537 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001538 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001539 buf->Name[0] = 'D';
1540 buf->Name[1] = 'H';
1541 buf->Name[2] = 'n';
1542 buf->Name[3] = 'Q';
1543 return buf;
1544}
1545
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001546static struct create_durable *
1547create_reconnect_durable_buf(struct cifs_fid *fid)
1548{
1549 struct create_durable *buf;
1550
1551 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1552 if (!buf)
1553 return NULL;
1554
1555 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1556 (struct create_durable, Data));
1557 buf->ccontext.DataLength = cpu_to_le32(16);
1558 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1559 (struct create_durable, Name));
1560 buf->ccontext.NameLength = cpu_to_le16(4);
1561 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1562 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001563 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001564 buf->Name[0] = 'D';
1565 buf->Name[1] = 'H';
1566 buf->Name[2] = 'n';
1567 buf->Name[3] = 'C';
1568 return buf;
1569}
1570
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001571static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001572parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1573 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001574{
1575 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001576 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001577 unsigned int next;
1578 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001579 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001580
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001581 data_offset = (char *)rsp + server->vals->header_preamble_size + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001582 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001583 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001584 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001585 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001586 if (le16_to_cpu(cc->NameLength) == 4 &&
1587 strncmp(name, "RqLs", 4) == 0)
1588 return server->ops->parse_lease_buf(cc, epoch);
1589
1590 next = le32_to_cpu(cc->Next);
1591 if (!next)
1592 break;
1593 remaining -= next;
1594 cc = (struct create_context *)((char *)cc + next);
1595 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001596
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001597 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001598}
1599
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001600static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001601add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1602 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001603{
1604 struct smb2_create_req *req = iov[0].iov_base;
1605 unsigned int num = *num_iovec;
1606
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001607 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001608 if (iov[num].iov_base == NULL)
1609 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001610 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001611 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1612 if (!req->CreateContextsOffset)
1613 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001614 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001615 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001616 le32_add_cpu(&req->CreateContextsLength,
1617 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001618 *num_iovec = num + 1;
1619 return 0;
1620}
1621
Steve Frenchb56eae42015-11-03 09:26:27 -06001622static struct create_durable_v2 *
1623create_durable_v2_buf(struct cifs_fid *pfid)
1624{
1625 struct create_durable_v2 *buf;
1626
1627 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1628 if (!buf)
1629 return NULL;
1630
1631 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1632 (struct create_durable_v2, dcontext));
1633 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1634 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1635 (struct create_durable_v2, Name));
1636 buf->ccontext.NameLength = cpu_to_le16(4);
1637
1638 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1639 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001640 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001641 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1642
1643 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1644 buf->Name[0] = 'D';
1645 buf->Name[1] = 'H';
1646 buf->Name[2] = '2';
1647 buf->Name[3] = 'Q';
1648 return buf;
1649}
1650
1651static struct create_durable_handle_reconnect_v2 *
1652create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1653{
1654 struct create_durable_handle_reconnect_v2 *buf;
1655
1656 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1657 GFP_KERNEL);
1658 if (!buf)
1659 return NULL;
1660
1661 buf->ccontext.DataOffset =
1662 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1663 dcontext));
1664 buf->ccontext.DataLength =
1665 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1666 buf->ccontext.NameOffset =
1667 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1668 Name));
1669 buf->ccontext.NameLength = cpu_to_le16(4);
1670
1671 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1672 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1673 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1674 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1675
1676 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1677 buf->Name[0] = 'D';
1678 buf->Name[1] = 'H';
1679 buf->Name[2] = '2';
1680 buf->Name[3] = 'C';
1681 return buf;
1682}
1683
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001684static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001685add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001686 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001687{
1688 struct smb2_create_req *req = iov[0].iov_base;
1689 unsigned int num = *num_iovec;
1690
Steve Frenchb56eae42015-11-03 09:26:27 -06001691 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1692 if (iov[num].iov_base == NULL)
1693 return -ENOMEM;
1694 iov[num].iov_len = sizeof(struct create_durable_v2);
1695 if (!req->CreateContextsOffset)
1696 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001697 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001698 iov[1].iov_len);
1699 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001700 *num_iovec = num + 1;
1701 return 0;
1702}
1703
1704static int
1705add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1706 struct cifs_open_parms *oparms)
1707{
1708 struct smb2_create_req *req = iov[0].iov_base;
1709 unsigned int num = *num_iovec;
1710
1711 /* indicate that we don't need to relock the file */
1712 oparms->reconnect = false;
1713
1714 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1715 if (iov[num].iov_base == NULL)
1716 return -ENOMEM;
1717 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1718 if (!req->CreateContextsOffset)
1719 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001720 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001721 iov[1].iov_len);
1722 le32_add_cpu(&req->CreateContextsLength,
1723 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001724 *num_iovec = num + 1;
1725 return 0;
1726}
1727
1728static int
1729add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1730 struct cifs_open_parms *oparms, bool use_persistent)
1731{
1732 struct smb2_create_req *req = iov[0].iov_base;
1733 unsigned int num = *num_iovec;
1734
1735 if (use_persistent) {
1736 if (oparms->reconnect)
1737 return add_durable_reconnect_v2_context(iov, num_iovec,
1738 oparms);
1739 else
1740 return add_durable_v2_context(iov, num_iovec, oparms);
1741 }
1742
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001743 if (oparms->reconnect) {
1744 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1745 /* indicate that we don't need to relock the file */
1746 oparms->reconnect = false;
1747 } else
1748 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001749 if (iov[num].iov_base == NULL)
1750 return -ENOMEM;
1751 iov[num].iov_len = sizeof(struct create_durable);
1752 if (!req->CreateContextsOffset)
1753 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001754 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001755 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001756 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001757 *num_iovec = num + 1;
1758 return 0;
1759}
1760
Aurelien Aptelf0712922017-02-22 14:47:17 +01001761static int
1762alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1763 const char *treename, const __le16 *path)
1764{
1765 int treename_len, path_len;
1766 struct nls_table *cp;
1767 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1768
1769 /*
1770 * skip leading "\\"
1771 */
1772 treename_len = strlen(treename);
1773 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1774 return -EINVAL;
1775
1776 treename += 2;
1777 treename_len -= 2;
1778
1779 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1780
1781 /*
1782 * make room for one path separator between the treename and
1783 * path
1784 */
1785 *out_len = treename_len + 1 + path_len;
1786
1787 /*
1788 * final path needs to be null-terminated UTF16 with a
1789 * size aligned to 8
1790 */
1791
1792 *out_size = roundup((*out_len+1)*2, 8);
1793 *out_path = kzalloc(*out_size, GFP_KERNEL);
1794 if (!*out_path)
1795 return -ENOMEM;
1796
1797 cp = load_nls_default();
1798 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1799 UniStrcat(*out_path, sep);
1800 UniStrcat(*out_path, path);
1801 unload_nls(cp);
1802
1803 return 0;
1804}
1805
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001806int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001807SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001808 __u8 *oplock, struct smb2_file_all_info *buf,
1809 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001810{
1811 struct smb2_create_req *req;
1812 struct smb2_create_rsp *rsp;
1813 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001814 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001815 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001816 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001817 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001818 int resp_buftype;
1819 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001820 __le16 *copy_path = NULL;
1821 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001822 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001823 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001824 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001825 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001826 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001827 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001828
Joe Perchesf96637b2013-05-04 22:12:25 -05001829 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001830
1831 if (ses && (ses->server))
1832 server = ses->server;
1833 else
1834 return -EIO;
1835
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001836 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1837
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001838 if (rc)
1839 return rc;
1840
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001841 if (encryption_required(tcon))
1842 flags |= CIFS_TRANSFORM_REQ;
1843
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001844 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001845 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001846 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1847 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001848
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001849 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001850 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001851 /* File attributes ignored on open (used in create though) */
1852 req->FileAttributes = cpu_to_le32(file_attributes);
1853 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001854 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1855 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001856
1857 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001858 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001859 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001860
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001861 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01001862
1863 /* [MS-SMB2] 2.2.13 NameOffset:
1864 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1865 * the SMB2 header, the file name includes a prefix that will
1866 * be processed during DFS name normalization as specified in
1867 * section 3.3.5.9. Otherwise, the file name is relative to
1868 * the share that is identified by the TreeId in the SMB2
1869 * header.
1870 */
1871 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1872 int name_len;
1873
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001874 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001875 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1876 &name_len,
1877 tcon->treeName, path);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001878 if (rc) {
1879 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001880 return rc;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001881 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001882 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001883 uni_path_len = copy_size;
1884 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001885 } else {
1886 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1887 /* MUST set path len (NameLength) to 0 opening root of share */
1888 req->NameLength = cpu_to_le16(uni_path_len - 2);
1889 if (uni_path_len % 8 != 0) {
1890 copy_size = roundup(uni_path_len, 8);
1891 copy_path = kzalloc(copy_size, GFP_KERNEL);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001892 if (!copy_path) {
1893 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001894 return -ENOMEM;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001895 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001896 memcpy((char *)copy_path, (const char *)path,
1897 uni_path_len);
1898 uni_path_len = copy_size;
1899 path = copy_path;
1900 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001901 }
1902
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001903 iov[1].iov_len = uni_path_len;
1904 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001905
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001906 if (!server->oplocks)
1907 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1908
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001909 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001910 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1911 req->RequestedOplockLevel = *oplock;
1912 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001913 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001914 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001915 cifs_small_buf_release(req);
1916 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001917 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001918 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001919 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001920 }
1921
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001922 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1923 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001924 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001925 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001926 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001927 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001928 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001929 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001930
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001931 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001932 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001933 if (rc) {
1934 cifs_small_buf_release(req);
1935 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001936 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001937 return rc;
1938 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001939 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001940 }
1941
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001942 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1943 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001944 cifs_small_buf_release(req);
1945 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001946
1947 if (rc != 0) {
1948 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001949 if (err_buf && rsp)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001950 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1951 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001952 goto creat_exit;
1953 }
1954
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001955 oparms->fid->persistent_fid = rsp->PersistentFileId;
1956 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001957
1958 if (buf) {
1959 memcpy(buf, &rsp->CreationTime, 32);
1960 buf->AllocationSize = rsp->AllocationSize;
1961 buf->EndOfFile = rsp->EndofFile;
1962 buf->Attributes = rsp->FileAttributes;
1963 buf->NumberOfLinks = cpu_to_le32(1);
1964 buf->DeletePending = 0;
1965 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001966
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001967 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001968 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001969 else
1970 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001971creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001972 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001973 kfree(lc_buf);
1974 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001975 free_rsp_buf(resp_buftype, rsp);
1976 return rc;
1977}
1978
Steve French4a72daf2013-06-25 00:20:49 -05001979/*
1980 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1981 */
1982int
1983SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001984 u64 volatile_fid, u32 opcode, bool is_fsctl,
Aurelien Aptel51146622017-02-28 15:08:41 +01001985 char *in_data, u32 indatalen,
1986 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001987{
1988 struct smb2_ioctl_req *req;
1989 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001990 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001991 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001992 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001993 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001994 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001995 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001996 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001997 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001998 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05001999
2000 cifs_dbg(FYI, "SMB2 IOCTL\n");
2001
Steve French3d1a3742014-08-11 21:05:25 -05002002 if (out_data != NULL)
2003 *out_data = NULL;
2004
Steve French4a72daf2013-06-25 00:20:49 -05002005 /* zero out returned data len, in case of error */
2006 if (plen)
2007 *plen = 0;
2008
Steve French8e353102015-03-26 19:47:02 -05002009 if (tcon)
2010 ses = tcon->ses;
2011 else
2012 return -EIO;
2013
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002014 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05002015 return -EIO;
2016
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002017 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002018 if (rc)
2019 return rc;
2020
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002021 if (encryption_required(tcon))
2022 flags |= CIFS_TRANSFORM_REQ;
2023
Steve French4a72daf2013-06-25 00:20:49 -05002024 req->CtlCode = cpu_to_le32(opcode);
2025 req->PersistentFileId = persistent_fid;
2026 req->VolatileFileId = volatile_fid;
2027
2028 if (indatalen) {
2029 req->InputCount = cpu_to_le32(indatalen);
2030 /* do not set InputOffset if no input data */
2031 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002032 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05002033 iov[1].iov_base = in_data;
2034 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002035 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05002036 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002037 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05002038
2039 req->OutputOffset = 0;
2040 req->OutputCount = 0; /* MBZ */
2041
2042 /*
2043 * Could increase MaxOutputResponse, but that would require more
2044 * than one credit. Windows typically sets this smaller, but for some
2045 * ioctls it may be useful to allow server to send more. No point
2046 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05002047 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2048 * (by default, note that it can be overridden to make max larger)
2049 * in responses (except for read responses which can be bigger.
2050 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05002051 */
Steve French7db0a6e2017-05-03 21:12:20 -05002052 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05002053
2054 if (is_fsctl)
2055 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2056 else
2057 req->Flags = 0;
2058
2059 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05002060
Steve French7ff8d452013-10-14 00:44:19 -05002061 /*
2062 * If no input data, the size of ioctl struct in
2063 * protocol spec still includes a 1 byte data buffer,
2064 * but if input data passed to ioctl, we do not
2065 * want to double count this, so we do not send
2066 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002067 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05002068 */
2069
2070 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002071 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05002072 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002073 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05002074
Steve French4587eee2017-10-25 15:58:31 -05002075 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2076 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002077 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002078
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002079 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2080 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002081 cifs_small_buf_release(req);
2082 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002083
Steve French9bf0c9c2013-11-16 18:05:28 -06002084 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002085 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002086 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002087 } else if (rc == -EINVAL) {
2088 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2089 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002090 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002091 goto ioctl_exit;
2092 }
Steve French4a72daf2013-06-25 00:20:49 -05002093 }
2094
2095 /* check if caller wants to look at return data or just return rc */
2096 if ((plen == NULL) || (out_data == NULL))
2097 goto ioctl_exit;
2098
2099 *plen = le32_to_cpu(rsp->OutputCount);
2100
2101 /* We check for obvious errors in the output buffer length and offset */
2102 if (*plen == 0)
2103 goto ioctl_exit; /* server returned no data */
2104 else if (*plen > 0xFF00) {
2105 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2106 *plen = 0;
2107 rc = -EIO;
2108 goto ioctl_exit;
2109 }
2110
2111 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2112 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2113 le32_to_cpu(rsp->OutputOffset));
2114 *plen = 0;
2115 rc = -EIO;
2116 goto ioctl_exit;
2117 }
2118
2119 *out_data = kmalloc(*plen, GFP_KERNEL);
2120 if (*out_data == NULL) {
2121 rc = -ENOMEM;
2122 goto ioctl_exit;
2123 }
2124
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002125 shdr = get_sync_hdr(rsp);
2126 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002127ioctl_exit:
2128 free_rsp_buf(resp_buftype, rsp);
2129 return rc;
2130}
2131
Steve French64a5cfa2013-10-14 15:31:32 -05002132/*
2133 * Individual callers to ioctl worker function follow
2134 */
2135
2136int
2137SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2138 u64 persistent_fid, u64 volatile_fid)
2139{
2140 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002141 struct compress_ioctl fsctl_input;
2142 char *ret_data = NULL;
2143
2144 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002145 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002146
2147 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2148 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2149 (char *)&fsctl_input /* data input */,
2150 2 /* in data len */, &ret_data /* out data */, NULL);
2151
2152 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002153
2154 return rc;
2155}
2156
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002157int
2158SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2159 u64 persistent_fid, u64 volatile_fid)
2160{
2161 struct smb2_close_req *req;
2162 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002163 struct cifs_ses *ses = tcon->ses;
2164 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002165 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002166 int resp_buftype;
2167 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002168 int flags = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002169 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002170
Joe Perchesf96637b2013-05-04 22:12:25 -05002171 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002172
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002173 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002174 return -EIO;
2175
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002176 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002177 if (rc)
2178 return rc;
2179
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002180 if (encryption_required(tcon))
2181 flags |= CIFS_TRANSFORM_REQ;
2182
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002183 req->PersistentFileId = persistent_fid;
2184 req->VolatileFileId = volatile_fid;
2185
2186 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002187 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002188
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002189 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002190 cifs_small_buf_release(req);
2191 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002192
2193 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002194 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002195 goto close_exit;
2196 }
2197
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002198 /* BB FIXME - decode close response, update inode for caching */
2199
2200close_exit:
2201 free_rsp_buf(resp_buftype, rsp);
2202 return rc;
2203}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002204
2205static int
2206validate_buf(unsigned int offset, unsigned int buffer_length,
2207 struct smb2_hdr *hdr, unsigned int min_buf_size)
2208
2209{
2210 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2211 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2212 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2213 char *end_of_buf = begin_of_buf + buffer_length;
2214
2215
2216 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002217 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2218 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002219 return -EINVAL;
2220 }
2221
2222 /* check if beyond RFC1001 maximum length */
2223 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002224 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2225 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002226 return -EINVAL;
2227 }
2228
2229 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002230 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002231 return -EINVAL;
2232 }
2233
2234 return 0;
2235}
2236
2237/*
2238 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2239 * Caller must free buffer.
2240 */
2241static int
2242validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2243 struct smb2_hdr *hdr, unsigned int minbufsize,
2244 char *data)
2245
2246{
2247 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2248 int rc;
2249
2250 if (!data)
2251 return -EINVAL;
2252
2253 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2254 if (rc)
2255 return rc;
2256
2257 memcpy(data, begin_of_buf, buffer_length);
2258
2259 return 0;
2260}
2261
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002262static int
2263query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002264 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2265 u32 additional_info, size_t output_len, size_t min_len, void **data,
2266 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002267{
2268 struct smb2_query_info_req *req;
2269 struct smb2_query_info_rsp *rsp = NULL;
2270 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002271 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002272 int rc = 0;
2273 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002274 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002275 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002276 unsigned int total_len;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002277
Joe Perchesf96637b2013-05-04 22:12:25 -05002278 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002279
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002280 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002281 return -EIO;
2282
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002283 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2284 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002285 if (rc)
2286 return rc;
2287
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002288 if (encryption_required(tcon))
2289 flags |= CIFS_TRANSFORM_REQ;
2290
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002291 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002292 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002293 req->PersistentFileId = persistent_fid;
2294 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002295 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002296
2297 /*
2298 * We do not use the input buffer (do not send extra byte)
2299 */
2300 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002301
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002302 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002303
2304 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002305 /* 1 for Buffer */
2306 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002307
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002308 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002309 cifs_small_buf_release(req);
2310 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002311
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002312 if (rc) {
2313 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2314 goto qinf_exit;
2315 }
2316
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002317 if (dlen) {
2318 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2319 if (!*data) {
2320 *data = kmalloc(*dlen, GFP_KERNEL);
2321 if (!*data) {
2322 cifs_dbg(VFS,
2323 "Error %d allocating memory for acl\n",
2324 rc);
2325 *dlen = 0;
2326 goto qinf_exit;
2327 }
2328 }
2329 }
2330
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002331 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2332 le32_to_cpu(rsp->OutputBufferLength),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002333 &rsp->hdr, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002334
2335qinf_exit:
2336 free_rsp_buf(resp_buftype, rsp);
2337 return rc;
2338}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002339
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002340int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002341 u64 persistent_fid, u64 volatile_fid,
2342 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002343{
2344 return query_info(xid, tcon, persistent_fid, volatile_fid,
2345 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002346 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002347 sizeof(struct smb2_file_full_ea_info),
2348 (void **)&data,
2349 NULL);
2350}
2351
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002352int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2353 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002354{
2355 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002356 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002357 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002358 sizeof(struct smb2_file_all_info), (void **)&data,
2359 NULL);
2360}
2361
2362int
2363SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2364 u64 persistent_fid, u64 volatile_fid,
2365 void **data, u32 *plen)
2366{
2367 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2368 *plen = 0;
2369
2370 return query_info(xid, tcon, persistent_fid, volatile_fid,
2371 0, SMB2_O_INFO_SECURITY, additional_info,
2372 SMB2_MAX_BUFFER_SIZE,
2373 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002374}
2375
2376int
2377SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2378 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2379{
2380 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002381 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002382 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002383 sizeof(struct smb2_file_internal_info),
2384 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002385}
2386
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002387/*
2388 * This is a no-op for now. We're not really interested in the reply, but
2389 * rather in the fact that the server sent one and that server->lstrp
2390 * gets updated.
2391 *
2392 * FIXME: maybe we should consider checking that the reply matches request?
2393 */
2394static void
2395smb2_echo_callback(struct mid_q_entry *mid)
2396{
2397 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002398 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002399 unsigned int credits_received = 1;
2400
2401 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002402 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002403
2404 DeleteMidQEntry(mid);
2405 add_credits(server, credits_received, CIFS_ECHO_OP);
2406}
2407
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002408void smb2_reconnect_server(struct work_struct *work)
2409{
2410 struct TCP_Server_Info *server = container_of(work,
2411 struct TCP_Server_Info, reconnect.work);
2412 struct cifs_ses *ses;
2413 struct cifs_tcon *tcon, *tcon2;
2414 struct list_head tmp_list;
2415 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002416 int rc;
2417 int resched = false;
2418
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002419
2420 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2421 mutex_lock(&server->reconnect_mutex);
2422
2423 INIT_LIST_HEAD(&tmp_list);
2424 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2425
2426 spin_lock(&cifs_tcp_ses_lock);
2427 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2428 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002429 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002430 tcon->tc_count++;
2431 list_add_tail(&tcon->rlist, &tmp_list);
2432 tcon_exist = true;
2433 }
2434 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002435 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2436 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2437 tcon_exist = true;
2438 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002439 }
2440 /*
2441 * Get the reference to server struct to be sure that the last call of
2442 * cifs_put_tcon() in the loop below won't release the server pointer.
2443 */
2444 if (tcon_exist)
2445 server->srv_count++;
2446
2447 spin_unlock(&cifs_tcp_ses_lock);
2448
2449 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002450 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2451 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002452 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002453 else
2454 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002455 list_del_init(&tcon->rlist);
2456 cifs_put_tcon(tcon);
2457 }
2458
2459 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002460 if (resched)
2461 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002462 mutex_unlock(&server->reconnect_mutex);
2463
2464 /* now we can safely release srv struct */
2465 if (tcon_exist)
2466 cifs_put_tcp_session(server, 1);
2467}
2468
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002469int
2470SMB2_echo(struct TCP_Server_Info *server)
2471{
2472 struct smb2_echo_req *req;
2473 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002474 struct kvec iov[2];
2475 struct smb_rqst rqst = { .rq_iov = iov,
2476 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002477 unsigned int total_len;
2478 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002479
Joe Perchesf96637b2013-05-04 22:12:25 -05002480 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002481
Steve French4fcd1812016-06-22 20:12:05 -05002482 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002483 /* No need to send echo on newly established connections */
2484 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2485 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002486 }
2487
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002488 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002489 if (rc)
2490 return rc;
2491
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002492 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002493
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002494 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002495 rfc1002_marker = cpu_to_be32(total_len);
2496 iov[0].iov_base = &rfc1002_marker;
2497 iov[1].iov_len = total_len;
2498 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002499
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002500 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2501 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002502 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002503 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002504
2505 cifs_small_buf_release(req);
2506 return rc;
2507}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002508
2509int
2510SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2511 u64 volatile_fid)
2512{
2513 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002514 struct cifs_ses *ses = tcon->ses;
2515 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002516 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002517 int resp_buftype;
2518 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002519 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002520 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002521
Joe Perchesf96637b2013-05-04 22:12:25 -05002522 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002523
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002524 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002525 return -EIO;
2526
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002527 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002528 if (rc)
2529 return rc;
2530
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002531 if (encryption_required(tcon))
2532 flags |= CIFS_TRANSFORM_REQ;
2533
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002534 req->PersistentFileId = persistent_fid;
2535 req->VolatileFileId = volatile_fid;
2536
2537 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002538 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002539
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002540 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002541 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002542
Steve Frenchdfebe402015-03-27 01:00:06 -05002543 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002544 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2545
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002546 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002547 return rc;
2548}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002549
2550/*
2551 * To form a chain of read requests, any read requests after the first should
2552 * have the end_of_chain boolean set to true.
2553 */
2554static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002555smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002556 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2557 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002558{
2559 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002560 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002561 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002562 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002563
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002564 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2565 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002566 if (rc)
2567 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002568
2569 server = io_parms->tcon->ses->server;
2570 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002571 return -ECONNABORTED;
2572
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002573 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002574 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002575
2576 req->PersistentFileId = io_parms->persistent_fid;
2577 req->VolatileFileId = io_parms->volatile_fid;
2578 req->ReadChannelInfoOffset = 0; /* reserved */
2579 req->ReadChannelInfoLength = 0; /* reserved */
2580 req->Channel = 0; /* reserved */
2581 req->MinimumCount = 0;
2582 req->Length = cpu_to_le32(io_parms->length);
2583 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07002584#ifdef CONFIG_CIFS_SMB_DIRECT
2585 /*
2586 * If we want to do a RDMA write, fill in and append
2587 * smbd_buffer_descriptor_v1 to the end of read request
2588 */
2589 if (server->rdma && rdata &&
2590 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002591
Long Libd3dcc62017-11-22 17:38:47 -07002592 struct smbd_buffer_descriptor_v1 *v1;
2593 bool need_invalidate =
2594 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2595
2596 rdata->mr = smbd_register_mr(
2597 server->smbd_conn, rdata->pages,
2598 rdata->nr_pages, rdata->tailsz,
2599 true, need_invalidate);
2600 if (!rdata->mr)
2601 return -ENOBUFS;
2602
2603 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2604 if (need_invalidate)
2605 req->Channel = SMB2_CHANNEL_RDMA_V1;
2606 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002607 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07002608 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002609 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07002610 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002611 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2612 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2613 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07002614
2615 *total_len += sizeof(*v1) - 1;
2616 }
2617#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002618 if (request_type & CHAINED_REQUEST) {
2619 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002620 /* next 8-byte aligned request */
2621 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2622 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002623 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002624 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002625 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002626 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002627 /*
2628 * Related requests use info from previous read request
2629 * in chain.
2630 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002631 shdr->SessionId = 0xFFFFFFFF;
2632 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002633 req->PersistentFileId = 0xFFFFFFFF;
2634 req->VolatileFileId = 0xFFFFFFFF;
2635 }
2636 }
2637 if (remaining_bytes > io_parms->length)
2638 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2639 else
2640 req->RemainingBytes = 0;
2641
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002642 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002643 return rc;
2644}
2645
2646static void
2647smb2_readv_callback(struct mid_q_entry *mid)
2648{
2649 struct cifs_readdata *rdata = mid->callback_data;
2650 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2651 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002652 struct smb2_sync_hdr *shdr =
2653 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002654 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002655 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2656 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002657 .rq_pages = rdata->pages,
2658 .rq_npages = rdata->nr_pages,
2659 .rq_pagesz = rdata->pagesz,
2660 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002661
Joe Perchesf96637b2013-05-04 22:12:25 -05002662 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2663 __func__, mid->mid, mid->mid_state, rdata->result,
2664 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002665
2666 switch (mid->mid_state) {
2667 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002668 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002669 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002670 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002671 int rc;
2672
Jeff Layton0b688cf2012-09-18 16:20:34 -07002673 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002674 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002675 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2676 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002677 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002678 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002679 task_io_account_read(rdata->got_bytes);
2680 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002681 break;
2682 case MID_REQUEST_SUBMITTED:
2683 case MID_RETRY_NEEDED:
2684 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002685 if (server->sign && rdata->got_bytes)
2686 /* reset bytes number since we can not check a sign */
2687 rdata->got_bytes = 0;
2688 /* FIXME: should this be counted toward the initiating task? */
2689 task_io_account_read(rdata->got_bytes);
2690 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002691 break;
2692 default:
2693 if (rdata->result != -ENODATA)
2694 rdata->result = -EIO;
2695 }
Long Libd3dcc62017-11-22 17:38:47 -07002696#ifdef CONFIG_CIFS_SMB_DIRECT
2697 /*
2698 * If this rdata has a memmory registered, the MR can be freed
2699 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2700 * because they have limited number and are used for future I/Os
2701 */
2702 if (rdata->mr) {
2703 smbd_deregister_mr(rdata->mr);
2704 rdata->mr = NULL;
2705 }
2706#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002707 if (rdata->result)
2708 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2709
2710 queue_work(cifsiod_wq, &rdata->work);
2711 DeleteMidQEntry(mid);
2712 add_credits(server, credits_received, 0);
2713}
2714
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002715/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002716int
2717smb2_async_readv(struct cifs_readdata *rdata)
2718{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002719 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002720 char *buf;
2721 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002722 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002723 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2724 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002725 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002726 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002727 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002728
Joe Perchesf96637b2013-05-04 22:12:25 -05002729 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2730 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002731
2732 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2733 io_parms.offset = rdata->offset;
2734 io_parms.length = rdata->bytes;
2735 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2736 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2737 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002738
2739 server = io_parms.tcon->ses->server;
2740
Long Li2dabfd52017-11-07 01:54:53 -07002741 rc = smb2_new_read_req(
2742 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002743 if (rc) {
2744 if (rc == -EAGAIN && rdata->credits) {
2745 /* credits was reset by reconnect */
2746 rdata->credits = 0;
2747 /* reduce in_flight value since we won't send the req */
2748 spin_lock(&server->req_lock);
2749 server->in_flight--;
2750 spin_unlock(&server->req_lock);
2751 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002752 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002753 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002754
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002755 if (encryption_required(io_parms.tcon))
2756 flags |= CIFS_TRANSFORM_REQ;
2757
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002758 req_len = cpu_to_be32(total_len);
2759
2760 rdata->iov[0].iov_base = &req_len;
2761 rdata->iov[0].iov_len = sizeof(__be32);
2762 rdata->iov[1].iov_base = buf;
2763 rdata->iov[1].iov_len = total_len;
2764
2765 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002766
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002767 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002768 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002769 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002770 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002771 spin_lock(&server->req_lock);
2772 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002773 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002774 spin_unlock(&server->req_lock);
2775 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002776 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002777 }
2778
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002779 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002780 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002781 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002782 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002783 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002784 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002785 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2786 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002787
2788 cifs_small_buf_release(buf);
2789 return rc;
2790}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002791
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002792int
2793SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2794 unsigned int *nbytes, char **buf, int *buf_type)
2795{
2796 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002797 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002798 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002799 struct smb2_sync_hdr *shdr;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002800 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002801 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002802 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002803 int flags = CIFS_LOG_ERROR;
2804 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002805
2806 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07002807 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002808 if (rc)
2809 return rc;
2810
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002811 if (encryption_required(io_parms->tcon))
2812 flags |= CIFS_TRANSFORM_REQ;
2813
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002814 iov[0].iov_base = (char *)req;
2815 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002816
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002817 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002818 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002819
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002820 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002821
2822 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002823 if (rc != -ENODATA) {
2824 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2825 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002826 }
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002827 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2828 return rc == -ENODATA ? 0 : rc;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002829 }
2830
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002831 *nbytes = le32_to_cpu(rsp->DataLength);
2832 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2833 (*nbytes > io_parms->length)) {
2834 cifs_dbg(FYI, "bad length %d for count %d\n",
2835 *nbytes, io_parms->length);
2836 rc = -EIO;
2837 *nbytes = 0;
2838 }
2839
2840 shdr = get_sync_hdr(rsp);
2841
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002842 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002843 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002844 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002845 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002846 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002847 if (resp_buftype == CIFS_SMALL_BUFFER)
2848 *buf_type = CIFS_SMALL_BUFFER;
2849 else if (resp_buftype == CIFS_LARGE_BUFFER)
2850 *buf_type = CIFS_LARGE_BUFFER;
2851 }
2852 return rc;
2853}
2854
Pavel Shilovsky33319142012-09-18 16:20:29 -07002855/*
2856 * Check the mid_state and signature on received buffer (if any), and queue the
2857 * workqueue completion task.
2858 */
2859static void
2860smb2_writev_callback(struct mid_q_entry *mid)
2861{
2862 struct cifs_writedata *wdata = mid->callback_data;
2863 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2864 unsigned int written;
2865 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2866 unsigned int credits_received = 1;
2867
2868 switch (mid->mid_state) {
2869 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002870 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002871 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2872 if (wdata->result != 0)
2873 break;
2874
2875 written = le32_to_cpu(rsp->DataLength);
2876 /*
2877 * Mask off high 16 bits when bytes written as returned
2878 * by the server is greater than bytes requested by the
2879 * client. OS/2 servers are known to set incorrect
2880 * CountHigh values.
2881 */
2882 if (written > wdata->bytes)
2883 written &= 0xFFFF;
2884
2885 if (written < wdata->bytes)
2886 wdata->result = -ENOSPC;
2887 else
2888 wdata->bytes = written;
2889 break;
2890 case MID_REQUEST_SUBMITTED:
2891 case MID_RETRY_NEEDED:
2892 wdata->result = -EAGAIN;
2893 break;
2894 default:
2895 wdata->result = -EIO;
2896 break;
2897 }
Long Lidb223a52017-11-22 17:38:45 -07002898#ifdef CONFIG_CIFS_SMB_DIRECT
2899 /*
2900 * If this wdata has a memory registered, the MR can be freed
2901 * The number of MRs available is limited, it's important to recover
2902 * used MR as soon as I/O is finished. Hold MR longer in the later
2903 * I/O process can possibly result in I/O deadlock due to lack of MR
2904 * to send request on I/O retry
2905 */
2906 if (wdata->mr) {
2907 smbd_deregister_mr(wdata->mr);
2908 wdata->mr = NULL;
2909 }
2910#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002911 if (wdata->result)
2912 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2913
2914 queue_work(cifsiod_wq, &wdata->work);
2915 DeleteMidQEntry(mid);
2916 add_credits(tcon->ses->server, credits_received, 0);
2917}
2918
2919/* smb2_async_writev - send an async write, and set up mid to handle result */
2920int
Steve French4a5c80d2014-02-07 20:45:12 -06002921smb2_async_writev(struct cifs_writedata *wdata,
2922 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002923{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002924 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002925 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002926 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002927 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002928 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002929 struct kvec iov[2];
2930 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002931 unsigned int total_len;
2932 __be32 rfc1002_marker;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002933
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002934 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002935 if (rc) {
2936 if (rc == -EAGAIN && wdata->credits) {
2937 /* credits was reset by reconnect */
2938 wdata->credits = 0;
2939 /* reduce in_flight value since we won't send the req */
2940 spin_lock(&server->req_lock);
2941 server->in_flight--;
2942 spin_unlock(&server->req_lock);
2943 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002944 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002945 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002946
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002947 if (encryption_required(tcon))
2948 flags |= CIFS_TRANSFORM_REQ;
2949
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002950 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002951 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002952
2953 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2954 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2955 req->WriteChannelInfoOffset = 0;
2956 req->WriteChannelInfoLength = 0;
2957 req->Channel = 0;
2958 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002959 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002960 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07002961 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07002962#ifdef CONFIG_CIFS_SMB_DIRECT
2963 /*
2964 * If we want to do a server RDMA read, fill in and append
2965 * smbd_buffer_descriptor_v1 to the end of write request
2966 */
2967 if (server->rdma && wdata->bytes >=
2968 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07002969
Long Lidb223a52017-11-22 17:38:45 -07002970 struct smbd_buffer_descriptor_v1 *v1;
2971 bool need_invalidate = server->dialect == SMB30_PROT_ID;
2972
2973 wdata->mr = smbd_register_mr(
2974 server->smbd_conn, wdata->pages,
2975 wdata->nr_pages, wdata->tailsz,
2976 false, need_invalidate);
2977 if (!wdata->mr) {
2978 rc = -ENOBUFS;
2979 goto async_writev_out;
2980 }
2981 req->Length = 0;
2982 req->DataOffset = 0;
2983 req->RemainingBytes =
Steve French2026b062018-01-24 23:07:41 -06002984 cpu_to_le32((wdata->nr_pages-1)*PAGE_SIZE + wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07002985 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2986 if (need_invalidate)
2987 req->Channel = SMB2_CHANNEL_RDMA_V1;
2988 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002989 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07002990 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002991 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07002992 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002993 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
2994 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
2995 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07002996 }
2997#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002998 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002999 iov[0].iov_len = 4;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003000 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
3001 iov[0].iov_base = &rfc1002_marker;
3002 iov[1].iov_len = total_len - 1;
3003 iov[1].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003004
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003005 rqst.rq_iov = iov;
3006 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003007 rqst.rq_pages = wdata->pages;
3008 rqst.rq_npages = wdata->nr_pages;
3009 rqst.rq_pagesz = wdata->pagesz;
3010 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003011#ifdef CONFIG_CIFS_SMB_DIRECT
3012 if (wdata->mr) {
3013 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3014 rqst.rq_npages = 0;
3015 }
3016#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003017 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3018 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003019
Long Lidb223a52017-11-22 17:38:45 -07003020#ifdef CONFIG_CIFS_SMB_DIRECT
3021 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3022 if (!wdata->mr)
3023 req->Length = cpu_to_le32(wdata->bytes);
3024#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003025 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003026#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003027
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003028 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003029 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003030 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003031 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003032 spin_lock(&server->req_lock);
3033 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003034 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003035 spin_unlock(&server->req_lock);
3036 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003037 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003038 }
3039
Pavel Shilovsky33319142012-09-18 16:20:29 -07003040 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003041 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3042 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003043
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003044 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06003045 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003046 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3047 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003048
Pavel Shilovsky33319142012-09-18 16:20:29 -07003049async_writev_out:
3050 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003051 return rc;
3052}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003053
3054/*
3055 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3056 * The length field from io_parms must be at least 1 and indicates a number of
3057 * elements with data to write that begins with position 1 in iov array. All
3058 * data length is specified by count.
3059 */
3060int
3061SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3062 unsigned int *nbytes, struct kvec *iov, int n_vec)
3063{
3064 int rc = 0;
3065 struct smb2_write_req *req = NULL;
3066 struct smb2_write_rsp *rsp = NULL;
3067 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003068 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003069 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003070 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003071
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003072 *nbytes = 0;
3073
3074 if (n_vec < 1)
3075 return rc;
3076
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003077 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3078 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003079 if (rc)
3080 return rc;
3081
3082 if (io_parms->tcon->ses->server == NULL)
3083 return -ECONNABORTED;
3084
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003085 if (encryption_required(io_parms->tcon))
3086 flags |= CIFS_TRANSFORM_REQ;
3087
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003088 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003089
3090 req->PersistentFileId = io_parms->persistent_fid;
3091 req->VolatileFileId = io_parms->volatile_fid;
3092 req->WriteChannelInfoOffset = 0;
3093 req->WriteChannelInfoLength = 0;
3094 req->Channel = 0;
3095 req->Length = cpu_to_le32(io_parms->length);
3096 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003097 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003098 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003099 req->RemainingBytes = 0;
3100
3101 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003102 /* 1 for Buffer */
3103 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003104
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003105 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
3106 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003107 cifs_small_buf_release(req);
3108 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003109
3110 if (rc) {
3111 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003112 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003113 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003114 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003115
3116 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003117 return rc;
3118}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003119
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003120static unsigned int
3121num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3122{
3123 int len;
3124 unsigned int entrycount = 0;
3125 unsigned int next_offset = 0;
3126 FILE_DIRECTORY_INFO *entryptr;
3127
3128 if (bufstart == NULL)
3129 return 0;
3130
3131 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3132
3133 while (1) {
3134 entryptr = (FILE_DIRECTORY_INFO *)
3135 ((char *)entryptr + next_offset);
3136
3137 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003138 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003139 break;
3140 }
3141
3142 len = le32_to_cpu(entryptr->FileNameLength);
3143 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003144 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3145 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003146 break;
3147 }
3148
3149 *lastentry = (char *)entryptr;
3150 entrycount++;
3151
3152 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3153 if (!next_offset)
3154 break;
3155 }
3156
3157 return entrycount;
3158}
3159
3160/*
3161 * Readdir/FindFirst
3162 */
3163int
3164SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3165 u64 persistent_fid, u64 volatile_fid, int index,
3166 struct cifs_search_info *srch_inf)
3167{
3168 struct smb2_query_directory_req *req;
3169 struct smb2_query_directory_rsp *rsp = NULL;
3170 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003171 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003172 int rc = 0;
3173 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003174 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003175 unsigned char *bufptr;
3176 struct TCP_Server_Info *server;
3177 struct cifs_ses *ses = tcon->ses;
3178 __le16 asteriks = cpu_to_le16('*');
3179 char *end_of_smb;
3180 unsigned int output_size = CIFSMaxBufSize;
3181 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003182 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003183 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003184
3185 if (ses && (ses->server))
3186 server = ses->server;
3187 else
3188 return -EIO;
3189
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003190 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3191 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003192 if (rc)
3193 return rc;
3194
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003195 if (encryption_required(tcon))
3196 flags |= CIFS_TRANSFORM_REQ;
3197
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003198 switch (srch_inf->info_level) {
3199 case SMB_FIND_FILE_DIRECTORY_INFO:
3200 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3201 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3202 break;
3203 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3204 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3205 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3206 break;
3207 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003208 cifs_dbg(VFS, "info level %u isn't supported\n",
3209 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003210 rc = -EINVAL;
3211 goto qdir_exit;
3212 }
3213
3214 req->FileIndex = cpu_to_le32(index);
3215 req->PersistentFileId = persistent_fid;
3216 req->VolatileFileId = volatile_fid;
3217
3218 len = 0x2;
3219 bufptr = req->Buffer;
3220 memcpy(bufptr, &asteriks, len);
3221
3222 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003223 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003224 req->FileNameLength = cpu_to_le16(len);
3225 /*
3226 * BB could be 30 bytes or so longer if we used SMB2 specific
3227 * buffer lengths, but this is safe and close enough.
3228 */
3229 output_size = min_t(unsigned int, output_size, server->maxBuf);
3230 output_size = min_t(unsigned int, output_size, 2 << 15);
3231 req->OutputBufferLength = cpu_to_le32(output_size);
3232
3233 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003234 /* 1 for Buffer */
3235 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003236
3237 iov[1].iov_base = (char *)(req->Buffer);
3238 iov[1].iov_len = len;
3239
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003240 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003241 cifs_small_buf_release(req);
3242 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003243
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003244 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003245 if (rc == -ENODATA &&
3246 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003247 srch_inf->endOfSearch = true;
3248 rc = 0;
3249 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003250 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3251 goto qdir_exit;
3252 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003253
3254 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3255 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3256 info_buf_size);
3257 if (rc)
3258 goto qdir_exit;
3259
3260 srch_inf->unicode = true;
3261
3262 if (srch_inf->ntwrk_buf_start) {
3263 if (srch_inf->smallBuf)
3264 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3265 else
3266 cifs_buf_release(srch_inf->ntwrk_buf_start);
3267 }
3268 srch_inf->ntwrk_buf_start = (char *)rsp;
3269 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3270 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3271 /* 4 for rfc1002 length field */
3272 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3273 srch_inf->entries_in_buffer =
3274 num_entries(srch_inf->srch_entries_start, end_of_smb,
3275 &srch_inf->last_entry, info_buf_size);
3276 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003277 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3278 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3279 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003280 if (resp_buftype == CIFS_LARGE_BUFFER)
3281 srch_inf->smallBuf = false;
3282 else if (resp_buftype == CIFS_SMALL_BUFFER)
3283 srch_inf->smallBuf = true;
3284 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003285 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003286
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003287 return rc;
3288
3289qdir_exit:
3290 free_rsp_buf(resp_buftype, rsp);
3291 return rc;
3292}
3293
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003294static int
3295send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003296 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3297 u8 info_type, u32 additional_info, unsigned int num,
3298 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003299{
3300 struct smb2_set_info_req *req;
3301 struct smb2_set_info_rsp *rsp = NULL;
3302 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003303 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003304 int rc = 0;
3305 int resp_buftype;
3306 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003307 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003308 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003309 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003310
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003311 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003312 return -EIO;
3313
3314 if (!num)
3315 return -EINVAL;
3316
3317 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3318 if (!iov)
3319 return -ENOMEM;
3320
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003321 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003322 if (rc) {
3323 kfree(iov);
3324 return rc;
3325 }
3326
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003327 if (encryption_required(tcon))
3328 flags |= CIFS_TRANSFORM_REQ;
3329
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003330 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003331
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003332 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003333 req->FileInfoClass = info_class;
3334 req->PersistentFileId = persistent_fid;
3335 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003336 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003337
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003338 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003339 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003340 req->BufferLength = cpu_to_le32(*size);
3341
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003342 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003343 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003344
3345 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003346 /* 1 for Buffer */
3347 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003348
3349 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003350 le32_add_cpu(&req->BufferLength, size[i]);
3351 iov[i].iov_base = (char *)data[i];
3352 iov[i].iov_len = size[i];
3353 }
3354
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003355 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3356 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003357 cifs_small_buf_release(req);
3358 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003359
Steve French7d3fb242013-11-18 09:56:28 -06003360 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003361 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003362
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003363 free_rsp_buf(resp_buftype, rsp);
3364 kfree(iov);
3365 return rc;
3366}
3367
3368int
3369SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3370 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3371{
3372 struct smb2_file_rename_info info;
3373 void **data;
3374 unsigned int size[2];
3375 int rc;
3376 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3377
3378 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3379 if (!data)
3380 return -ENOMEM;
3381
3382 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3383 /* 0 = fail if target already exists */
3384 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3385 info.FileNameLength = cpu_to_le32(len);
3386
3387 data[0] = &info;
3388 size[0] = sizeof(struct smb2_file_rename_info);
3389
3390 data[1] = target_file;
3391 size[1] = len + 2 /* null */;
3392
3393 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003394 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3395 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003396 kfree(data);
3397 return rc;
3398}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003399
3400int
Steve French897fba12016-05-12 21:20:36 -05003401SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3402 u64 persistent_fid, u64 volatile_fid)
3403{
3404 __u8 delete_pending = 1;
3405 void *data;
3406 unsigned int size;
3407
3408 data = &delete_pending;
3409 size = 1; /* sizeof __u8 */
3410
3411 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003412 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3413 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003414}
3415
3416int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003417SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3418 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3419{
3420 struct smb2_file_link_info info;
3421 void **data;
3422 unsigned int size[2];
3423 int rc;
3424 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3425
3426 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3427 if (!data)
3428 return -ENOMEM;
3429
3430 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3431 /* 0 = fail if link already exists */
3432 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3433 info.FileNameLength = cpu_to_le32(len);
3434
3435 data[0] = &info;
3436 size[0] = sizeof(struct smb2_file_link_info);
3437
3438 data[1] = target_file;
3439 size[1] = len + 2 /* null */;
3440
3441 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003442 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3443 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003444 kfree(data);
3445 return rc;
3446}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003447
3448int
3449SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003450 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003451{
3452 struct smb2_file_eof_info info;
3453 void *data;
3454 unsigned int size;
3455
3456 info.EndOfFile = *eof;
3457
3458 data = &info;
3459 size = sizeof(struct smb2_file_eof_info);
3460
Steve Frenchf29ebb42014-07-19 21:44:58 -05003461 if (is_falloc)
3462 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003463 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3464 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003465 else
3466 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003467 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3468 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003469}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003470
3471int
3472SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3473 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3474{
3475 unsigned int size;
3476 size = sizeof(FILE_BASIC_INFO);
3477 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003478 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3479 0, 1, (void **)&buf, &size);
3480}
3481
3482int
3483SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3484 u64 persistent_fid, u64 volatile_fid,
3485 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3486{
3487 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3488 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3489 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003490}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003491
3492int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003493SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3494 u64 persistent_fid, u64 volatile_fid,
3495 struct smb2_file_full_ea_info *buf, int len)
3496{
3497 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3498 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3499 0, 1, (void **)&buf, &len);
3500}
3501
3502int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003503SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3504 const u64 persistent_fid, const u64 volatile_fid,
3505 __u8 oplock_level)
3506{
3507 int rc;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003508 struct smb2_oplock_break_req *req = NULL;
3509 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003510 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003511 unsigned int total_len;
3512 struct kvec iov[1];
3513 struct kvec rsp_iov;
3514 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003515
Joe Perchesf96637b2013-05-04 22:12:25 -05003516 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003517 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3518 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003519 if (rc)
3520 return rc;
3521
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003522 if (encryption_required(tcon))
3523 flags |= CIFS_TRANSFORM_REQ;
3524
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003525 req->VolatileFid = volatile_fid;
3526 req->PersistentFid = persistent_fid;
3527 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003528 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003529
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003530 flags |= CIFS_NO_RESP;
3531
3532 iov[0].iov_base = (char *)req;
3533 iov[0].iov_len = total_len;
3534
3535 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003536 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003537
3538 if (rc) {
3539 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003540 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003541 }
3542
3543 return rc;
3544}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003545
3546static void
3547copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3548 struct kstatfs *kst)
3549{
3550 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3551 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3552 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303553 kst->f_bfree = kst->f_bavail =
3554 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003555 return;
3556}
3557
3558static int
3559build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3560 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3561{
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003562 struct TCP_Server_Info *server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003563 int rc;
3564 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003565 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003566
Joe Perchesf96637b2013-05-04 22:12:25 -05003567 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003568
3569 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3570 return -EIO;
3571
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003572 server = tcon->ses->server;
3573
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003574 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3575 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003576 if (rc)
3577 return rc;
3578
3579 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3580 req->FileInfoClass = level;
3581 req->PersistentFileId = persistent_fid;
3582 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003583 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003584 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003585 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003586 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003587 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - server->vals->header_preamble_size);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003588
3589 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003590 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003591 return 0;
3592}
3593
3594int
3595SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3596 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3597{
3598 struct smb2_query_info_rsp *rsp = NULL;
3599 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003600 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003601 int rc = 0;
3602 int resp_buftype;
3603 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003604 struct TCP_Server_Info *server = ses->server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003605 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003606 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003607
3608 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3609 sizeof(struct smb2_fs_full_size_info),
3610 persistent_fid, volatile_fid);
3611 if (rc)
3612 return rc;
3613
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003614 if (encryption_required(tcon))
3615 flags |= CIFS_TRANSFORM_REQ;
3616
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003617 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003618 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003619 if (rc) {
3620 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003621 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003622 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003623 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003624
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003625 info = (struct smb2_fs_full_size_info *)(server->vals->header_preamble_size +
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003626 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3627 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3628 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3629 sizeof(struct smb2_fs_full_size_info));
3630 if (!rc)
3631 copy_fs_info_to_kstatfs(info, fsdata);
3632
Steve French34f62642013-10-09 02:07:00 -05003633qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003634 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003635 return rc;
3636}
3637
3638int
3639SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003640 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003641{
3642 struct smb2_query_info_rsp *rsp = NULL;
3643 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003644 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003645 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003646 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003647 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003648 struct TCP_Server_Info *server = ses->server;
Steve French34f62642013-10-09 02:07:00 -05003649 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003650 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003651
Steven French21671142013-10-09 13:36:35 -05003652 if (level == FS_DEVICE_INFORMATION) {
3653 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3654 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3655 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3656 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3657 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003658 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3659 max_len = sizeof(struct smb3_fs_ss_info);
3660 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003661 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003662 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003663 return -EINVAL;
3664 }
3665
3666 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003667 persistent_fid, volatile_fid);
3668 if (rc)
3669 return rc;
3670
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003671 if (encryption_required(tcon))
3672 flags |= CIFS_TRANSFORM_REQ;
3673
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003674 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003675 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003676 if (rc) {
3677 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3678 goto qfsattr_exit;
3679 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003680 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003681
3682 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3683 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003684 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3685 if (rc)
3686 goto qfsattr_exit;
3687
3688 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003689 memcpy(&tcon->fsAttrInfo, server->vals->header_preamble_size + offset
Steve French34f62642013-10-09 02:07:00 -05003690 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003691 rsp_len, max_len));
3692 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003693 memcpy(&tcon->fsDevInfo, server->vals->header_preamble_size + offset
Steven French21671142013-10-09 13:36:35 -05003694 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003695 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3696 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003697 (server->vals->header_preamble_size + offset + (char *)&rsp->hdr);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003698 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3699 tcon->perf_sector_size =
3700 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3701 }
Steve French34f62642013-10-09 02:07:00 -05003702
3703qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003704 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003705 return rc;
3706}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003707
3708int
3709smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3710 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3711 const __u32 num_lock, struct smb2_lock_element *buf)
3712{
3713 int rc = 0;
3714 struct smb2_lock_req *req = NULL;
3715 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003716 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003717 int resp_buf_type;
3718 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003719 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003720 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003721
Joe Perchesf96637b2013-05-04 22:12:25 -05003722 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003723
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003724 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003725 if (rc)
3726 return rc;
3727
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003728 if (encryption_required(tcon))
3729 flags |= CIFS_TRANSFORM_REQ;
3730
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003731 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003732 req->LockCount = cpu_to_le16(num_lock);
3733
3734 req->PersistentFileId = persist_fid;
3735 req->VolatileFileId = volatile_fid;
3736
3737 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003738
3739 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003740 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003741 iov[1].iov_base = (char *)buf;
3742 iov[1].iov_len = count;
3743
3744 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003745 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3746 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003747 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003748 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003749 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003750 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3751 }
3752
3753 return rc;
3754}
3755
3756int
3757SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3758 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3759 const __u64 length, const __u64 offset, const __u32 lock_flags,
3760 const bool wait)
3761{
3762 struct smb2_lock_element lock;
3763
3764 lock.Offset = cpu_to_le64(offset);
3765 lock.Length = cpu_to_le64(length);
3766 lock.Flags = cpu_to_le32(lock_flags);
3767 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3768 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3769
3770 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3771}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003772
3773int
3774SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3775 __u8 *lease_key, const __le32 lease_state)
3776{
3777 int rc;
3778 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003779 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003780 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003781 unsigned int total_len;
3782 struct kvec iov[1];
3783 struct kvec rsp_iov;
3784 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003785
Joe Perchesf96637b2013-05-04 22:12:25 -05003786 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003787 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3788 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003789 if (rc)
3790 return rc;
3791
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003792 if (encryption_required(tcon))
3793 flags |= CIFS_TRANSFORM_REQ;
3794
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003795 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003796 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003797 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003798
3799 memcpy(req->LeaseKey, lease_key, 16);
3800 req->LeaseState = lease_state;
3801
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003802 flags |= CIFS_NO_RESP;
3803
3804 iov[0].iov_base = (char *)req;
3805 iov[0].iov_len = total_len;
3806
3807 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003808 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003809
3810 if (rc) {
3811 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003812 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003813 }
3814
3815 return rc;
3816}