blob: 9aea138dd71fb1d57d89e5f9cd744f3460bf6f51 [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;
Steve French23657ad2018-04-22 15:14:58 -0500386 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
387 pneg_ctxt->CipherCount = cpu_to_le16(1);
388/* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
389 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500390}
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];
Steve French23657ad2018-04-22 15:14:58 -0500447 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500448 return 0;
449}
450
451static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
452 struct TCP_Server_Info *server)
453{
454 struct smb2_neg_context *pctx;
455 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
456 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
457 unsigned int len_of_smb = be32_to_cpu(rsp->hdr.smb2_buf_length);
458 unsigned int len_of_ctxts, i;
459 int rc = 0;
460
461 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
462 if (len_of_smb <= offset) {
463 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
464 return -EINVAL;
465 }
466
467 len_of_ctxts = len_of_smb - offset;
468
469 for (i = 0; i < ctxt_cnt; i++) {
470 int clen;
471 /* check that offset is not beyond end of SMB */
472 if (len_of_ctxts == 0)
473 break;
474
475 if (len_of_ctxts < sizeof(struct smb2_neg_context))
476 break;
477
Steve French0d4b46b2018-04-12 20:32:13 -0500478 pctx = (struct smb2_neg_context *)(offset +
479 server->vals->header_preamble_size + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500480 clen = le16_to_cpu(pctx->DataLength);
481 if (clen > len_of_ctxts)
482 break;
483
484 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
485 decode_preauth_context(
486 (struct smb2_preauth_neg_context *)pctx);
487 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
488 rc = decode_encrypt_ctx(server,
489 (struct smb2_encryption_neg_context *)pctx);
490 else
491 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
492 le16_to_cpu(pctx->ContextType));
493
494 if (rc)
495 break;
496 /* offsets must be 8 byte aligned */
497 clen = (clen + 7) & ~0x7;
498 offset += clen + sizeof(struct smb2_neg_context);
499 len_of_ctxts -= clen;
500 }
501 return rc;
502}
503
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500504#else
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100505static void assemble_neg_contexts(struct smb2_negotiate_req *req,
506 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500507{
508 return;
509}
510#endif /* SMB311 */
511
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400512/*
513 *
514 * SMB2 Worker functions follow:
515 *
516 * The general structure of the worker functions is:
517 * 1) Call smb2_init (assembles SMB2 header)
518 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
519 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
520 * 4) Decode SMB2 command specific fields in the fixed length area
521 * 5) Decode variable length data area (if any for this SMB2 command type)
522 * 6) Call free smb buffer
523 * 7) return
524 *
525 */
526
527int
528SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
529{
530 struct smb2_negotiate_req *req;
531 struct smb2_negotiate_rsp *rsp;
532 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700533 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400534 int rc = 0;
535 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400536 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400537 int blob_offset, blob_length;
538 char *security_blob;
539 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100540 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400541
Joe Perchesf96637b2013-05-04 22:12:25 -0500542 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400543
Jeff Layton3534b852013-05-24 07:41:01 -0400544 if (!server) {
545 WARN(1, "%s: server is NULL!\n", __func__);
546 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400547 }
548
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100549 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400550 if (rc)
551 return rc;
552
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100553 req->sync_hdr.SessionId = 0;
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100554#ifdef CONFIG_CIFS_SMB311
555 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
556 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
557#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400558
Steve French9764c022017-09-17 10:41:35 -0500559 if (strcmp(ses->server->vals->version_string,
560 SMB3ANY_VERSION_STRING) == 0) {
561 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
562 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
563 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100564 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500565 } else if (strcmp(ses->server->vals->version_string,
566 SMBDEFAULT_VERSION_STRING) == 0) {
567 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
568 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
569 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
570 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100571 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500572 } else {
573 /* otherwise send specific dialect */
574 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
575 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100576 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500577 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400578
579 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400580 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500581 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400582 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500583 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400584 else
585 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400586
Steve Frenche4aa25e2012-10-01 12:26:22 -0500587 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400588
Steve French3c5f9be12014-05-13 13:37:45 -0700589 /* ClientGUID must be zero for SMB2.02 dialect */
590 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
591 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500592 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700593 memcpy(req->ClientGUID, server->client_guid,
594 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500595 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100596 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500597 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400598 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100599 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400600
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100601 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700602 cifs_small_buf_release(req);
603 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400604 /*
605 * No tcon so can't do
606 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
607 */
Steve French7e682f72017-08-31 21:34:24 -0500608 if (rc == -EOPNOTSUPP) {
609 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500610 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500611 " older servers\n");
612 goto neg_exit;
613 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400614 goto neg_exit;
615
Steve French9764c022017-09-17 10:41:35 -0500616 if (strcmp(ses->server->vals->version_string,
617 SMB3ANY_VERSION_STRING) == 0) {
618 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
619 cifs_dbg(VFS,
620 "SMB2 dialect returned but not requested\n");
621 return -EIO;
622 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
623 cifs_dbg(VFS,
624 "SMB2.1 dialect returned but not requested\n");
625 return -EIO;
626 }
627 } else if (strcmp(ses->server->vals->version_string,
628 SMBDEFAULT_VERSION_STRING) == 0) {
629 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
630 cifs_dbg(VFS,
631 "SMB2 dialect returned but not requested\n");
632 return -EIO;
633 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
634 /* ops set to 3.0 by default for default so update */
635 ses->server->ops = &smb21_operations;
636 }
Steve French590d08d2017-09-19 11:43:47 -0500637 } else if (le16_to_cpu(rsp->DialectRevision) !=
638 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500639 /* if requested single dialect ensure returned dialect matched */
640 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500641 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500642 return -EIO;
643 }
644
Joe Perchesf96637b2013-05-04 22:12:25 -0500645 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400646
Steve Frenche4aa25e2012-10-01 12:26:22 -0500647 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500648 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500649 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500650 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500651 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500652 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500653 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
654 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600655#ifdef CONFIG_CIFS_SMB311
656 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
657 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
658#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400659 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500660 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500661 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400662 rc = -EIO;
663 goto neg_exit;
664 }
665 server->dialect = le16_to_cpu(rsp->DialectRevision);
666
Steve French9764c022017-09-17 10:41:35 -0500667 /* BB: add check that dialect was valid given dialect(s) we asked for */
668
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100669#ifdef CONFIG_CIFS_SMB311
670 /*
671 * Keep a copy of the hash after negprot. This hash will be
672 * the starting hash value for all sessions made from this
673 * server.
674 */
675 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
676 SMB2_PREAUTH_HASH_SIZE);
677#endif
Jeff Laytone598d1d82013-05-26 07:00:59 -0400678 /* SMB2 only has an extended negflavor */
679 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400680 /* set it to the maximum buffer size value we can send with 1 credit */
681 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
682 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400683 server->max_read = le32_to_cpu(rsp->MaxReadSize);
684 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400685 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500686 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
687 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
688 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400689 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400690 /* Internal types */
691 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400692
693 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
694 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500695 /*
696 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
697 * for us will be
698 * ses->sectype = RawNTLMSSP;
699 * but for time being this is our only auth choice so doesn't matter.
700 * We just found a server which sets blob length to zero expecting raw.
701 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700702 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500703 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700704 server->sec_ntlmssp = true;
705 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700706
Jeff Layton38d77c52013-05-26 07:01:00 -0400707 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400708 if (rc)
709 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500710 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500711 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500712 if (rc == 1)
713 rc = 0;
714 else if (rc == 0)
715 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400716 }
Steve French5100d8a2018-04-09 10:47:14 -0500717
718#ifdef CONFIG_CIFS_SMB311
719 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
720 if (rsp->NegotiateContextCount)
721 rc = smb311_decode_neg_context(rsp, server);
722 else
723 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
724 }
725#endif /* CONFIG_CIFS_SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400726neg_exit:
727 free_rsp_buf(resp_buftype, rsp);
728 return rc;
729}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400730
Steve Frenchff1c0382013-11-19 23:44:46 -0600731int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
732{
733 int rc = 0;
734 struct validate_negotiate_info_req vneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200735 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600736 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500737 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600738
739 cifs_dbg(FYI, "validate negotiate\n");
740
Long Li8801e902017-11-22 17:38:49 -0700741#ifdef CONFIG_CIFS_SMB_DIRECT
742 if (tcon->ses->server->rdma)
743 return 0;
744#endif
745
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100746 /* In SMB3.11 preauth integrity supersedes validate negotiate */
747 if (tcon->ses->server->dialect == SMB311_PROT_ID)
748 return 0;
749
Steve Frenchff1c0382013-11-19 23:44:46 -0600750 /*
751 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500752 * can not sign it (ie are not known user). Even if signing is not
753 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600754 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500755 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600756 */
Steve French0603c962017-09-20 19:57:18 -0500757 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600758 return 0; /* validation requires signing */
759
Steve French0603c962017-09-20 19:57:18 -0500760 if (tcon->ses->user_name == NULL) {
761 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
762 return 0; /* validation requires signing */
763 }
764
765 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
766 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
767
Steve Frenchff1c0382013-11-19 23:44:46 -0600768 vneg_inbuf.Capabilities =
769 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100770 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
771 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600772
773 if (tcon->ses->sign)
774 vneg_inbuf.SecurityMode =
775 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
776 else if (global_secflags & CIFSSEC_MAY_SIGN)
777 vneg_inbuf.SecurityMode =
778 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
779 else
780 vneg_inbuf.SecurityMode = 0;
781
Steve French9764c022017-09-17 10:41:35 -0500782
783 if (strcmp(tcon->ses->server->vals->version_string,
784 SMB3ANY_VERSION_STRING) == 0) {
785 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
786 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
787 vneg_inbuf.DialectCount = cpu_to_le16(2);
788 /* structure is big enough for 3 dialects, sending only 2 */
789 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
790 } else if (strcmp(tcon->ses->server->vals->version_string,
791 SMBDEFAULT_VERSION_STRING) == 0) {
792 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
793 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
794 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
795 vneg_inbuf.DialectCount = cpu_to_le16(3);
796 /* structure is big enough for 3 dialects */
797 inbuflen = sizeof(struct validate_negotiate_info_req);
798 } else {
799 /* otherwise specific dialect was requested */
800 vneg_inbuf.Dialects[0] =
801 cpu_to_le16(tcon->ses->server->vals->protocol_id);
802 vneg_inbuf.DialectCount = cpu_to_le16(1);
803 /* structure is big enough for 3 dialects, sending only 1 */
804 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
805 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600806
807 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
808 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
809 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
810 (char **)&pneg_rsp, &rsplen);
811
812 if (rc != 0) {
813 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
814 return -EIO;
815 }
816
817 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500818 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
819 rsplen);
820
821 /* relax check since Mac returns max bufsize allowed on ioctl */
David Disseldorpa2d9daa2017-10-20 14:49:38 +0200822 if ((rsplen > CIFSMaxBufSize)
823 || (rsplen < sizeof(struct validate_negotiate_info_rsp)))
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200824 goto err_rsp_free;
Steve Frenchff1c0382013-11-19 23:44:46 -0600825 }
826
827 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +0100828 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600829 goto vneg_out;
830
831 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
832 goto vneg_out;
833
834 /* do not validate server guid because not saved at negprot time yet */
835
836 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
837 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
838 goto vneg_out;
839
840 /* validate negotiate successful */
841 cifs_dbg(FYI, "validate negotiate info successful\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200842 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600843 return 0;
844
845vneg_out:
846 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200847err_rsp_free:
848 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600849 return -EIO;
850}
851
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530852enum securityEnum
853smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
854{
855 switch (requested) {
856 case Kerberos:
857 case RawNTLMSSP:
858 return requested;
859 case NTLMv2:
860 return RawNTLMSSP;
861 case Unspecified:
862 if (server->sec_ntlmssp &&
863 (global_secflags & CIFSSEC_MAY_NTLMSSP))
864 return RawNTLMSSP;
865 if ((server->sec_kerberos || server->sec_mskerberos) &&
866 (global_secflags & CIFSSEC_MAY_KRB5))
867 return Kerberos;
868 /* Fallthrough */
869 default:
870 return Unspecified;
871 }
872}
873
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100874struct SMB2_sess_data {
875 unsigned int xid;
876 struct cifs_ses *ses;
877 struct nls_table *nls_cp;
878 void (*func)(struct SMB2_sess_data *);
879 int result;
880 u64 previous_session;
881
882 /* we will send the SMB in three pieces:
883 * a fixed length beginning part, an optional
884 * SPNEGO blob (which can be zero length), and a
885 * last part which will include the strings
886 * and rest of bcc area. This allows us to avoid
887 * a large buffer 17K allocation
888 */
889 int buf0_type;
890 struct kvec iov[2];
891};
892
893static int
894SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
895{
896 int rc;
897 struct cifs_ses *ses = sess_data->ses;
898 struct smb2_sess_setup_req *req;
899 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100900 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100901
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100902 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
903 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100904 if (rc)
905 return rc;
906
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700907 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100908 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100909
910 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
911 req->PreviousSessionId = sess_data->previous_session;
912
913 req->Flags = 0; /* MBZ */
914 /* to enable echos and oplocks */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100915 req->sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100916
917 /* only one of SMB2 signing flags may be set in SMB2 request */
918 if (server->sign)
919 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
920 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
921 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
922 else
923 req->SecurityMode = 0;
924
925 req->Capabilities = 0;
926 req->Channel = 0; /* MBZ */
927
928 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100929 /* 1 for pad */
930 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100931 /*
932 * This variable will be used to clear the buffer
933 * allocated above in case of any error in the calling function.
934 */
935 sess_data->buf0_type = CIFS_SMALL_BUFFER;
936
937 return 0;
938}
939
940static void
941SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
942{
943 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
944 sess_data->buf0_type = CIFS_NO_BUFFER;
945}
946
947static int
948SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
949{
950 int rc;
951 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700952 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100953
954 /* Testing shows that buffer offset must be at location of Buffer[0] */
955 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100956 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100957 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
958
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100959 /* BB add code to build os and lm fields */
960
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100961 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
962 sess_data->iov, 2,
963 &sess_data->buf0_type,
964 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700965 cifs_small_buf_release(sess_data->iov[0].iov_base);
966 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100967
968 return rc;
969}
970
971static int
972SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
973{
974 int rc = 0;
975 struct cifs_ses *ses = sess_data->ses;
976
977 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800978 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100979 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100980 if (rc) {
981 cifs_dbg(FYI,
982 "SMB3 session key generation failed\n");
983 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800984 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100985 }
986 }
987 if (!ses->server->session_estab) {
988 ses->server->sequence_number = 0x2;
989 ses->server->session_estab = true;
990 }
991 mutex_unlock(&ses->server->srv_mutex);
992
993 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
994 spin_lock(&GlobalMid_Lock);
995 ses->status = CifsGood;
996 ses->need_reconnect = false;
997 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100998 return rc;
999}
1000
1001#ifdef CONFIG_CIFS_UPCALL
1002static void
1003SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1004{
1005 int rc;
1006 struct cifs_ses *ses = sess_data->ses;
1007 struct cifs_spnego_msg *msg;
1008 struct key *spnego_key = NULL;
1009 struct smb2_sess_setup_rsp *rsp = NULL;
1010
1011 rc = SMB2_sess_alloc_buffer(sess_data);
1012 if (rc)
1013 goto out;
1014
1015 spnego_key = cifs_get_spnego_key(ses);
1016 if (IS_ERR(spnego_key)) {
1017 rc = PTR_ERR(spnego_key);
1018 spnego_key = NULL;
1019 goto out;
1020 }
1021
1022 msg = spnego_key->payload.data[0];
1023 /*
1024 * check version field to make sure that cifs.upcall is
1025 * sending us a response in an expected form
1026 */
1027 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1028 cifs_dbg(VFS,
1029 "bad cifs.upcall version. Expected %d got %d",
1030 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1031 rc = -EKEYREJECTED;
1032 goto out_put_spnego_key;
1033 }
1034
1035 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1036 GFP_KERNEL);
1037 if (!ses->auth_key.response) {
1038 cifs_dbg(VFS,
1039 "Kerberos can't allocate (%u bytes) memory",
1040 msg->sesskey_len);
1041 rc = -ENOMEM;
1042 goto out_put_spnego_key;
1043 }
1044 ses->auth_key.len = msg->sesskey_len;
1045
1046 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1047 sess_data->iov[1].iov_len = msg->secblob_len;
1048
1049 rc = SMB2_sess_sendreceive(sess_data);
1050 if (rc)
1051 goto out_put_spnego_key;
1052
1053 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001054 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001055
1056 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001057
1058 rc = SMB2_sess_establish_session(sess_data);
1059out_put_spnego_key:
1060 key_invalidate(spnego_key);
1061 key_put(spnego_key);
1062out:
1063 sess_data->result = rc;
1064 sess_data->func = NULL;
1065 SMB2_sess_free_buffer(sess_data);
1066}
1067#else
1068static void
1069SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1070{
1071 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1072 sess_data->result = -EOPNOTSUPP;
1073 sess_data->func = NULL;
1074}
1075#endif
1076
Sachin Prabhu166cea42016-10-07 19:11:22 +01001077static void
1078SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1079
1080static void
1081SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1082{
1083 int rc;
1084 struct cifs_ses *ses = sess_data->ses;
1085 struct smb2_sess_setup_rsp *rsp = NULL;
1086 char *ntlmssp_blob = NULL;
1087 bool use_spnego = false; /* else use raw ntlmssp */
1088 u16 blob_length = 0;
1089
1090 /*
1091 * If memory allocation is successful, caller of this function
1092 * frees it.
1093 */
1094 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1095 if (!ses->ntlmssp) {
1096 rc = -ENOMEM;
1097 goto out_err;
1098 }
1099 ses->ntlmssp->sesskey_per_smbsess = true;
1100
1101 rc = SMB2_sess_alloc_buffer(sess_data);
1102 if (rc)
1103 goto out_err;
1104
1105 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1106 GFP_KERNEL);
1107 if (ntlmssp_blob == NULL) {
1108 rc = -ENOMEM;
1109 goto out;
1110 }
1111
1112 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1113 if (use_spnego) {
1114 /* BB eventually need to add this */
1115 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1116 rc = -EOPNOTSUPP;
1117 goto out;
1118 } else {
1119 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1120 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1121 }
1122 sess_data->iov[1].iov_base = ntlmssp_blob;
1123 sess_data->iov[1].iov_len = blob_length;
1124
1125 rc = SMB2_sess_sendreceive(sess_data);
1126 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1127
1128 /* If true, rc here is expected and not an error */
1129 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001130 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001131 rc = 0;
1132
1133 if (rc)
1134 goto out;
1135
Ronnie Sahlberg9fdd2e02018-04-09 18:06:27 +10001136 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - ses->server->vals->header_preamble_size !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001137 le16_to_cpu(rsp->SecurityBufferOffset)) {
1138 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1139 le16_to_cpu(rsp->SecurityBufferOffset));
1140 rc = -EIO;
1141 goto out;
1142 }
1143 rc = decode_ntlmssp_challenge(rsp->Buffer,
1144 le16_to_cpu(rsp->SecurityBufferLength), ses);
1145 if (rc)
1146 goto out;
1147
1148 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1149
1150
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001151 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001152 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001153
1154out:
1155 kfree(ntlmssp_blob);
1156 SMB2_sess_free_buffer(sess_data);
1157 if (!rc) {
1158 sess_data->result = 0;
1159 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1160 return;
1161 }
1162out_err:
1163 kfree(ses->ntlmssp);
1164 ses->ntlmssp = NULL;
1165 sess_data->result = rc;
1166 sess_data->func = NULL;
1167}
1168
1169static void
1170SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1171{
1172 int rc;
1173 struct cifs_ses *ses = sess_data->ses;
1174 struct smb2_sess_setup_req *req;
1175 struct smb2_sess_setup_rsp *rsp = NULL;
1176 unsigned char *ntlmssp_blob = NULL;
1177 bool use_spnego = false; /* else use raw ntlmssp */
1178 u16 blob_length = 0;
1179
1180 rc = SMB2_sess_alloc_buffer(sess_data);
1181 if (rc)
1182 goto out;
1183
1184 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001185 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001186
1187 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1188 sess_data->nls_cp);
1189 if (rc) {
1190 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1191 goto out;
1192 }
1193
1194 if (use_spnego) {
1195 /* BB eventually need to add this */
1196 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1197 rc = -EOPNOTSUPP;
1198 goto out;
1199 }
1200 sess_data->iov[1].iov_base = ntlmssp_blob;
1201 sess_data->iov[1].iov_len = blob_length;
1202
1203 rc = SMB2_sess_sendreceive(sess_data);
1204 if (rc)
1205 goto out;
1206
1207 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1208
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001209 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001210 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001211
1212 rc = SMB2_sess_establish_session(sess_data);
1213out:
1214 kfree(ntlmssp_blob);
1215 SMB2_sess_free_buffer(sess_data);
1216 kfree(ses->ntlmssp);
1217 ses->ntlmssp = NULL;
1218 sess_data->result = rc;
1219 sess_data->func = NULL;
1220}
1221
1222static int
1223SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1224{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301225 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001226
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301227 type = smb2_select_sectype(ses->server, ses->sectype);
1228 cifs_dbg(FYI, "sess setup type %d\n", type);
1229 if (type == Unspecified) {
1230 cifs_dbg(VFS,
1231 "Unable to select appropriate authentication method!");
1232 return -EINVAL;
1233 }
1234
1235 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001236 case Kerberos:
1237 sess_data->func = SMB2_auth_kerberos;
1238 break;
1239 case RawNTLMSSP:
1240 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1241 break;
1242 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301243 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001244 return -EOPNOTSUPP;
1245 }
1246
1247 return 0;
1248}
1249
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001250int
1251SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1252 const struct nls_table *nls_cp)
1253{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001254 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001255 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001256 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001257
Joe Perchesf96637b2013-05-04 22:12:25 -05001258 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001259
Jeff Layton3534b852013-05-24 07:41:01 -04001260 if (!server) {
1261 WARN(1, "%s: server is NULL!\n", __func__);
1262 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001263 }
1264
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001265 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1266 if (!sess_data)
1267 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001268
1269 rc = SMB2_select_sec(ses, sess_data);
1270 if (rc)
1271 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001272 sess_data->xid = xid;
1273 sess_data->ses = ses;
1274 sess_data->buf0_type = CIFS_NO_BUFFER;
1275 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001276
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001277#ifdef CONFIG_CIFS_SMB311
1278 /*
1279 * Initialize the session hash with the server one.
1280 */
1281 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1282 SMB2_PREAUTH_HASH_SIZE);
1283#endif
1284
Sachin Prabhu166cea42016-10-07 19:11:22 +01001285 while (sess_data->func)
1286 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001287
Steve Frenchc721c382017-09-19 18:40:03 -05001288 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1289 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001290 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001291out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001292 kfree(sess_data);
1293 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001294}
1295
1296int
1297SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1298{
1299 struct smb2_logoff_req *req; /* response is also trivial struct */
1300 int rc = 0;
1301 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001302 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001303 unsigned int total_len;
1304 struct kvec iov[1];
1305 struct kvec rsp_iov;
1306 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001307
Joe Perchesf96637b2013-05-04 22:12:25 -05001308 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001309
1310 if (ses && (ses->server))
1311 server = ses->server;
1312 else
1313 return -EIO;
1314
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001315 /* no need to send SMB logoff if uid already closed due to reconnect */
1316 if (ses->need_reconnect)
1317 goto smb2_session_already_dead;
1318
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001319 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001320 if (rc)
1321 return rc;
1322
1323 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001324 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001325
1326 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1327 flags |= CIFS_TRANSFORM_REQ;
1328 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001329 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001330
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001331 flags |= CIFS_NO_RESP;
1332
1333 iov[0].iov_base = (char *)req;
1334 iov[0].iov_len = total_len;
1335
1336 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001337 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001338 /*
1339 * No tcon so can't do
1340 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1341 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001342
1343smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001344 return rc;
1345}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001346
1347static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1348{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001349 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001350}
1351
1352#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1353
Steve Frenchde9f68d2013-11-15 11:26:24 -06001354/* These are similar values to what Windows uses */
1355static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1356{
1357 tcon->max_chunks = 256;
1358 tcon->max_bytes_chunk = 1048576;
1359 tcon->max_bytes_copy = 16777216;
1360}
1361
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001362int
1363SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1364 struct cifs_tcon *tcon, const struct nls_table *cp)
1365{
1366 struct smb2_tree_connect_req *req;
1367 struct smb2_tree_connect_rsp *rsp = NULL;
1368 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001369 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001370 int rc = 0;
1371 int resp_buftype;
1372 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001373 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001374 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001375 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001376
Joe Perchesf96637b2013-05-04 22:12:25 -05001377 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001378
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001379 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001380 return -EIO;
1381
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001382 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1383 if (unc_path == NULL)
1384 return -ENOMEM;
1385
1386 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1387 unc_path_len *= 2;
1388 if (unc_path_len < 2) {
1389 kfree(unc_path);
1390 return -EINVAL;
1391 }
1392
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001393 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001394 tcon->tid = 0;
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001395
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001396 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1397 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001398 if (rc) {
1399 kfree(unc_path);
1400 return rc;
1401 }
1402
Aurelien Aptelb327a712018-01-24 13:46:10 +01001403 if (encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001404 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001405
1406 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001407 /* 1 for pad */
1408 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001409
1410 /* Testing shows that buffer offset must be at location of Buffer[0] */
1411 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001412 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001413 req->PathLength = cpu_to_le16(unc_path_len - 2);
1414 iov[1].iov_base = unc_path;
1415 iov[1].iov_len = unc_path_len;
1416
Steve French6188f282018-03-13 02:29:36 -05001417 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1418 if ((ses->server->dialect == SMB311_PROT_ID) &&
1419 !encryption_required(tcon))
1420 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1421
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001422 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001423 cifs_small_buf_release(req);
1424 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001425
1426 if (rc != 0) {
1427 if (tcon) {
1428 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1429 tcon->need_reconnect = true;
1430 }
1431 goto tcon_error_exit;
1432 }
1433
Christophe JAILLETcd123002017-05-12 17:59:32 +02001434 switch (rsp->ShareType) {
1435 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001436 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001437 break;
1438 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001439 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001440 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001441 break;
1442 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001443 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001444 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001445 break;
1446 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001447 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001448 rc = -EOPNOTSUPP;
1449 goto tcon_error_exit;
1450 }
1451
1452 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001453 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001454 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1455 tcon->tidStatus = CifsGood;
1456 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001457 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001458 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001459
1460 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1461 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001462 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001463
1464 if (tcon->seal &&
1465 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1466 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1467
Steve Frenchde9f68d2013-11-15 11:26:24 -06001468 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001469 if (tcon->ses->server->ops->validate_negotiate)
1470 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001471tcon_exit:
1472 free_rsp_buf(resp_buftype, rsp);
1473 kfree(unc_path);
1474 return rc;
1475
1476tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001477 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001478 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001479 }
1480 goto tcon_exit;
1481}
1482
1483int
1484SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1485{
1486 struct smb2_tree_disconnect_req *req; /* response is trivial */
1487 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001488 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001489 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001490 unsigned int total_len;
1491 struct kvec iov[1];
1492 struct kvec rsp_iov;
1493 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001494
Joe Perchesf96637b2013-05-04 22:12:25 -05001495 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001496
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001497 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001498 return -EIO;
1499
1500 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1501 return 0;
1502
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001503 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1504 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001505 if (rc)
1506 return rc;
1507
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001508 if (encryption_required(tcon))
1509 flags |= CIFS_TRANSFORM_REQ;
1510
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001511 flags |= CIFS_NO_RESP;
1512
1513 iov[0].iov_base = (char *)req;
1514 iov[0].iov_len = total_len;
1515
1516 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001517 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001518 if (rc)
1519 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1520
1521 return rc;
1522}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001523
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001524
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001525static struct create_durable *
1526create_durable_buf(void)
1527{
1528 struct create_durable *buf;
1529
1530 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1531 if (!buf)
1532 return NULL;
1533
1534 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001535 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001536 buf->ccontext.DataLength = cpu_to_le32(16);
1537 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1538 (struct create_durable, Name));
1539 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001540 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001541 buf->Name[0] = 'D';
1542 buf->Name[1] = 'H';
1543 buf->Name[2] = 'n';
1544 buf->Name[3] = 'Q';
1545 return buf;
1546}
1547
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001548static struct create_durable *
1549create_reconnect_durable_buf(struct cifs_fid *fid)
1550{
1551 struct create_durable *buf;
1552
1553 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1554 if (!buf)
1555 return NULL;
1556
1557 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1558 (struct create_durable, Data));
1559 buf->ccontext.DataLength = cpu_to_le32(16);
1560 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1561 (struct create_durable, Name));
1562 buf->ccontext.NameLength = cpu_to_le16(4);
1563 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1564 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001565 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001566 buf->Name[0] = 'D';
1567 buf->Name[1] = 'H';
1568 buf->Name[2] = 'n';
1569 buf->Name[3] = 'C';
1570 return buf;
1571}
1572
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001573static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001574parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1575 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001576{
1577 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001578 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001579 unsigned int next;
1580 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001581 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001582
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001583 data_offset = (char *)rsp + server->vals->header_preamble_size + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001584 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001585 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001586 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001587 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001588 if (le16_to_cpu(cc->NameLength) == 4 &&
1589 strncmp(name, "RqLs", 4) == 0)
1590 return server->ops->parse_lease_buf(cc, epoch);
1591
1592 next = le32_to_cpu(cc->Next);
1593 if (!next)
1594 break;
1595 remaining -= next;
1596 cc = (struct create_context *)((char *)cc + next);
1597 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001598
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001599 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001600}
1601
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001602static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001603add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1604 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001605{
1606 struct smb2_create_req *req = iov[0].iov_base;
1607 unsigned int num = *num_iovec;
1608
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001609 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001610 if (iov[num].iov_base == NULL)
1611 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001612 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001613 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1614 if (!req->CreateContextsOffset)
1615 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001616 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001617 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001618 le32_add_cpu(&req->CreateContextsLength,
1619 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001620 *num_iovec = num + 1;
1621 return 0;
1622}
1623
Steve Frenchb56eae42015-11-03 09:26:27 -06001624static struct create_durable_v2 *
1625create_durable_v2_buf(struct cifs_fid *pfid)
1626{
1627 struct create_durable_v2 *buf;
1628
1629 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1630 if (!buf)
1631 return NULL;
1632
1633 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1634 (struct create_durable_v2, dcontext));
1635 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1636 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1637 (struct create_durable_v2, Name));
1638 buf->ccontext.NameLength = cpu_to_le16(4);
1639
1640 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1641 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001642 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001643 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1644
1645 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1646 buf->Name[0] = 'D';
1647 buf->Name[1] = 'H';
1648 buf->Name[2] = '2';
1649 buf->Name[3] = 'Q';
1650 return buf;
1651}
1652
1653static struct create_durable_handle_reconnect_v2 *
1654create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1655{
1656 struct create_durable_handle_reconnect_v2 *buf;
1657
1658 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1659 GFP_KERNEL);
1660 if (!buf)
1661 return NULL;
1662
1663 buf->ccontext.DataOffset =
1664 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1665 dcontext));
1666 buf->ccontext.DataLength =
1667 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1668 buf->ccontext.NameOffset =
1669 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1670 Name));
1671 buf->ccontext.NameLength = cpu_to_le16(4);
1672
1673 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1674 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1675 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1676 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1677
1678 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1679 buf->Name[0] = 'D';
1680 buf->Name[1] = 'H';
1681 buf->Name[2] = '2';
1682 buf->Name[3] = 'C';
1683 return buf;
1684}
1685
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001686static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001687add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001688 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001689{
1690 struct smb2_create_req *req = iov[0].iov_base;
1691 unsigned int num = *num_iovec;
1692
Steve Frenchb56eae42015-11-03 09:26:27 -06001693 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1694 if (iov[num].iov_base == NULL)
1695 return -ENOMEM;
1696 iov[num].iov_len = sizeof(struct create_durable_v2);
1697 if (!req->CreateContextsOffset)
1698 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001699 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001700 iov[1].iov_len);
1701 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001702 *num_iovec = num + 1;
1703 return 0;
1704}
1705
1706static int
1707add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1708 struct cifs_open_parms *oparms)
1709{
1710 struct smb2_create_req *req = iov[0].iov_base;
1711 unsigned int num = *num_iovec;
1712
1713 /* indicate that we don't need to relock the file */
1714 oparms->reconnect = false;
1715
1716 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1717 if (iov[num].iov_base == NULL)
1718 return -ENOMEM;
1719 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1720 if (!req->CreateContextsOffset)
1721 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001722 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001723 iov[1].iov_len);
1724 le32_add_cpu(&req->CreateContextsLength,
1725 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001726 *num_iovec = num + 1;
1727 return 0;
1728}
1729
1730static int
1731add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1732 struct cifs_open_parms *oparms, bool use_persistent)
1733{
1734 struct smb2_create_req *req = iov[0].iov_base;
1735 unsigned int num = *num_iovec;
1736
1737 if (use_persistent) {
1738 if (oparms->reconnect)
1739 return add_durable_reconnect_v2_context(iov, num_iovec,
1740 oparms);
1741 else
1742 return add_durable_v2_context(iov, num_iovec, oparms);
1743 }
1744
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001745 if (oparms->reconnect) {
1746 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1747 /* indicate that we don't need to relock the file */
1748 oparms->reconnect = false;
1749 } else
1750 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001751 if (iov[num].iov_base == NULL)
1752 return -ENOMEM;
1753 iov[num].iov_len = sizeof(struct create_durable);
1754 if (!req->CreateContextsOffset)
1755 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001756 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001757 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001758 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001759 *num_iovec = num + 1;
1760 return 0;
1761}
1762
Aurelien Aptelf0712922017-02-22 14:47:17 +01001763static int
1764alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1765 const char *treename, const __le16 *path)
1766{
1767 int treename_len, path_len;
1768 struct nls_table *cp;
1769 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1770
1771 /*
1772 * skip leading "\\"
1773 */
1774 treename_len = strlen(treename);
1775 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1776 return -EINVAL;
1777
1778 treename += 2;
1779 treename_len -= 2;
1780
1781 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1782
1783 /*
1784 * make room for one path separator between the treename and
1785 * path
1786 */
1787 *out_len = treename_len + 1 + path_len;
1788
1789 /*
1790 * final path needs to be null-terminated UTF16 with a
1791 * size aligned to 8
1792 */
1793
1794 *out_size = roundup((*out_len+1)*2, 8);
1795 *out_path = kzalloc(*out_size, GFP_KERNEL);
1796 if (!*out_path)
1797 return -ENOMEM;
1798
1799 cp = load_nls_default();
1800 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1801 UniStrcat(*out_path, sep);
1802 UniStrcat(*out_path, path);
1803 unload_nls(cp);
1804
1805 return 0;
1806}
1807
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001808int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001809SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001810 __u8 *oplock, struct smb2_file_all_info *buf,
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001811 struct kvec *err_iov)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001812{
1813 struct smb2_create_req *req;
1814 struct smb2_create_rsp *rsp;
1815 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001816 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001817 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001818 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001819 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001820 int resp_buftype;
1821 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001822 __le16 *copy_path = NULL;
1823 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001824 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001825 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001826 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001827 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001828 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001829 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001830
Joe Perchesf96637b2013-05-04 22:12:25 -05001831 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001832
1833 if (ses && (ses->server))
1834 server = ses->server;
1835 else
1836 return -EIO;
1837
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001838 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1839
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001840 if (rc)
1841 return rc;
1842
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001843 if (encryption_required(tcon))
1844 flags |= CIFS_TRANSFORM_REQ;
1845
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001846 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001847 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001848 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1849 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001850
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001851 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001852 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001853 /* File attributes ignored on open (used in create though) */
1854 req->FileAttributes = cpu_to_le32(file_attributes);
1855 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001856 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1857 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001858
1859 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001860 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001861 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001862
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001863 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01001864
1865 /* [MS-SMB2] 2.2.13 NameOffset:
1866 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1867 * the SMB2 header, the file name includes a prefix that will
1868 * be processed during DFS name normalization as specified in
1869 * section 3.3.5.9. Otherwise, the file name is relative to
1870 * the share that is identified by the TreeId in the SMB2
1871 * header.
1872 */
1873 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1874 int name_len;
1875
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001876 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001877 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1878 &name_len,
1879 tcon->treeName, path);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001880 if (rc) {
1881 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001882 return rc;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001883 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001884 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001885 uni_path_len = copy_size;
1886 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001887 } else {
1888 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1889 /* MUST set path len (NameLength) to 0 opening root of share */
1890 req->NameLength = cpu_to_le16(uni_path_len - 2);
1891 if (uni_path_len % 8 != 0) {
1892 copy_size = roundup(uni_path_len, 8);
1893 copy_path = kzalloc(copy_size, GFP_KERNEL);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001894 if (!copy_path) {
1895 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001896 return -ENOMEM;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001897 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001898 memcpy((char *)copy_path, (const char *)path,
1899 uni_path_len);
1900 uni_path_len = copy_size;
1901 path = copy_path;
1902 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001903 }
1904
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001905 iov[1].iov_len = uni_path_len;
1906 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001907
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001908 if (!server->oplocks)
1909 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1910
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001911 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001912 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1913 req->RequestedOplockLevel = *oplock;
1914 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001915 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001916 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001917 cifs_small_buf_release(req);
1918 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001919 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001920 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001921 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001922 }
1923
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001924 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1925 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001926 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001927 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001928 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001929 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001930 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001931 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001932
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001933 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001934 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001935 if (rc) {
1936 cifs_small_buf_release(req);
1937 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001938 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001939 return rc;
1940 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001941 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001942 }
1943
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001944 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1945 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001946 cifs_small_buf_release(req);
1947 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001948
1949 if (rc != 0) {
1950 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001951 if (err_iov && rsp) {
1952 *err_iov = rsp_iov;
1953 resp_buftype = CIFS_NO_BUFFER;
1954 rsp = NULL;
1955 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001956 goto creat_exit;
1957 }
1958
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001959 oparms->fid->persistent_fid = rsp->PersistentFileId;
1960 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001961
1962 if (buf) {
1963 memcpy(buf, &rsp->CreationTime, 32);
1964 buf->AllocationSize = rsp->AllocationSize;
1965 buf->EndOfFile = rsp->EndofFile;
1966 buf->Attributes = rsp->FileAttributes;
1967 buf->NumberOfLinks = cpu_to_le32(1);
1968 buf->DeletePending = 0;
1969 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001970
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001971 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001972 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001973 else
1974 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001975creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001976 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001977 kfree(lc_buf);
1978 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001979 free_rsp_buf(resp_buftype, rsp);
1980 return rc;
1981}
1982
Steve French4a72daf2013-06-25 00:20:49 -05001983/*
1984 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1985 */
1986int
1987SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001988 u64 volatile_fid, u32 opcode, bool is_fsctl,
Aurelien Aptel51146622017-02-28 15:08:41 +01001989 char *in_data, u32 indatalen,
1990 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001991{
1992 struct smb2_ioctl_req *req;
1993 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001994 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001995 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001996 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001997 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001998 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001999 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002000 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002001 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002002 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05002003
2004 cifs_dbg(FYI, "SMB2 IOCTL\n");
2005
Steve French3d1a3742014-08-11 21:05:25 -05002006 if (out_data != NULL)
2007 *out_data = NULL;
2008
Steve French4a72daf2013-06-25 00:20:49 -05002009 /* zero out returned data len, in case of error */
2010 if (plen)
2011 *plen = 0;
2012
Steve French8e353102015-03-26 19:47:02 -05002013 if (tcon)
2014 ses = tcon->ses;
2015 else
2016 return -EIO;
2017
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002018 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05002019 return -EIO;
2020
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002021 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002022 if (rc)
2023 return rc;
2024
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002025 if (encryption_required(tcon))
2026 flags |= CIFS_TRANSFORM_REQ;
2027
Steve French4a72daf2013-06-25 00:20:49 -05002028 req->CtlCode = cpu_to_le32(opcode);
2029 req->PersistentFileId = persistent_fid;
2030 req->VolatileFileId = volatile_fid;
2031
2032 if (indatalen) {
2033 req->InputCount = cpu_to_le32(indatalen);
2034 /* do not set InputOffset if no input data */
2035 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002036 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05002037 iov[1].iov_base = in_data;
2038 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002039 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05002040 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002041 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05002042
2043 req->OutputOffset = 0;
2044 req->OutputCount = 0; /* MBZ */
2045
2046 /*
2047 * Could increase MaxOutputResponse, but that would require more
2048 * than one credit. Windows typically sets this smaller, but for some
2049 * ioctls it may be useful to allow server to send more. No point
2050 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05002051 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2052 * (by default, note that it can be overridden to make max larger)
2053 * in responses (except for read responses which can be bigger.
2054 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05002055 */
Steve French7db0a6e2017-05-03 21:12:20 -05002056 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05002057
2058 if (is_fsctl)
2059 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2060 else
2061 req->Flags = 0;
2062
2063 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05002064
Steve French7ff8d452013-10-14 00:44:19 -05002065 /*
2066 * If no input data, the size of ioctl struct in
2067 * protocol spec still includes a 1 byte data buffer,
2068 * but if input data passed to ioctl, we do not
2069 * want to double count this, so we do not send
2070 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002071 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05002072 */
2073
2074 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002075 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05002076 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002077 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05002078
Steve French4587eee2017-10-25 15:58:31 -05002079 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2080 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002081 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002082
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002083 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2084 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002085 cifs_small_buf_release(req);
2086 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002087
Steve French9bf0c9c2013-11-16 18:05:28 -06002088 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002089 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002090 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002091 } else if (rc == -EINVAL) {
2092 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2093 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002094 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002095 goto ioctl_exit;
2096 }
Steve French4a72daf2013-06-25 00:20:49 -05002097 }
2098
2099 /* check if caller wants to look at return data or just return rc */
2100 if ((plen == NULL) || (out_data == NULL))
2101 goto ioctl_exit;
2102
2103 *plen = le32_to_cpu(rsp->OutputCount);
2104
2105 /* We check for obvious errors in the output buffer length and offset */
2106 if (*plen == 0)
2107 goto ioctl_exit; /* server returned no data */
2108 else if (*plen > 0xFF00) {
2109 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2110 *plen = 0;
2111 rc = -EIO;
2112 goto ioctl_exit;
2113 }
2114
2115 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2116 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2117 le32_to_cpu(rsp->OutputOffset));
2118 *plen = 0;
2119 rc = -EIO;
2120 goto ioctl_exit;
2121 }
2122
2123 *out_data = kmalloc(*plen, GFP_KERNEL);
2124 if (*out_data == NULL) {
2125 rc = -ENOMEM;
2126 goto ioctl_exit;
2127 }
2128
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002129 shdr = get_sync_hdr(rsp);
2130 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002131ioctl_exit:
2132 free_rsp_buf(resp_buftype, rsp);
2133 return rc;
2134}
2135
Steve French64a5cfa2013-10-14 15:31:32 -05002136/*
2137 * Individual callers to ioctl worker function follow
2138 */
2139
2140int
2141SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2142 u64 persistent_fid, u64 volatile_fid)
2143{
2144 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002145 struct compress_ioctl fsctl_input;
2146 char *ret_data = NULL;
2147
2148 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002149 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002150
2151 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2152 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2153 (char *)&fsctl_input /* data input */,
2154 2 /* in data len */, &ret_data /* out data */, NULL);
2155
2156 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002157
2158 return rc;
2159}
2160
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002161int
2162SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2163 u64 persistent_fid, u64 volatile_fid)
2164{
2165 struct smb2_close_req *req;
2166 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002167 struct cifs_ses *ses = tcon->ses;
2168 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002169 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002170 int resp_buftype;
2171 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002172 int flags = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002173 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002174
Joe Perchesf96637b2013-05-04 22:12:25 -05002175 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002176
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002177 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002178 return -EIO;
2179
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002180 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002181 if (rc)
2182 return rc;
2183
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002184 if (encryption_required(tcon))
2185 flags |= CIFS_TRANSFORM_REQ;
2186
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002187 req->PersistentFileId = persistent_fid;
2188 req->VolatileFileId = volatile_fid;
2189
2190 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002191 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002192
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002193 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002194 cifs_small_buf_release(req);
2195 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002196
2197 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002198 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002199 goto close_exit;
2200 }
2201
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002202 /* BB FIXME - decode close response, update inode for caching */
2203
2204close_exit:
2205 free_rsp_buf(resp_buftype, rsp);
2206 return rc;
2207}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002208
2209static int
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002210validate_iov(struct TCP_Server_Info *server,
2211 unsigned int offset, unsigned int buffer_length,
2212 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002213{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002214 unsigned int smb_len = iov->iov_len;
2215 char *end_of_smb = smb_len + server->vals->header_preamble_size + (char *)iov->iov_base;
2216 char *begin_of_buf = server->vals->header_preamble_size + offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002217 char *end_of_buf = begin_of_buf + buffer_length;
2218
2219
2220 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002221 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2222 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002223 return -EINVAL;
2224 }
2225
2226 /* check if beyond RFC1001 maximum length */
2227 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002228 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2229 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002230 return -EINVAL;
2231 }
2232
2233 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002234 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002235 return -EINVAL;
2236 }
2237
2238 return 0;
2239}
2240
2241/*
2242 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2243 * Caller must free buffer.
2244 */
2245static int
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002246validate_and_copy_iov(struct TCP_Server_Info *server,
2247 unsigned int offset, unsigned int buffer_length,
2248 struct kvec *iov, unsigned int minbufsize,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002249 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002250{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002251 char *begin_of_buf = server->vals->header_preamble_size + offset + (char *)(iov->iov_base);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002252 int rc;
2253
2254 if (!data)
2255 return -EINVAL;
2256
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002257 rc = validate_iov(server, offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002258 if (rc)
2259 return rc;
2260
2261 memcpy(data, begin_of_buf, buffer_length);
2262
2263 return 0;
2264}
2265
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002266static int
2267query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002268 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2269 u32 additional_info, size_t output_len, size_t min_len, void **data,
2270 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002271{
2272 struct smb2_query_info_req *req;
2273 struct smb2_query_info_rsp *rsp = NULL;
2274 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002275 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002276 int rc = 0;
2277 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002278 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002279 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002280 unsigned int total_len;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002281
Joe Perchesf96637b2013-05-04 22:12:25 -05002282 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002283
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002284 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002285 return -EIO;
2286
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002287 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2288 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002289 if (rc)
2290 return rc;
2291
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002292 if (encryption_required(tcon))
2293 flags |= CIFS_TRANSFORM_REQ;
2294
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002295 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002296 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002297 req->PersistentFileId = persistent_fid;
2298 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002299 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002300
2301 /*
2302 * We do not use the input buffer (do not send extra byte)
2303 */
2304 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002305
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002306 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002307
2308 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002309 /* 1 for Buffer */
2310 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002311
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002312 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002313 cifs_small_buf_release(req);
2314 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002315
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002316 if (rc) {
2317 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2318 goto qinf_exit;
2319 }
2320
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002321 if (dlen) {
2322 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2323 if (!*data) {
2324 *data = kmalloc(*dlen, GFP_KERNEL);
2325 if (!*data) {
2326 cifs_dbg(VFS,
2327 "Error %d allocating memory for acl\n",
2328 rc);
2329 *dlen = 0;
2330 goto qinf_exit;
2331 }
2332 }
2333 }
2334
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002335 rc = validate_and_copy_iov(ses->server,
2336 le16_to_cpu(rsp->OutputBufferOffset),
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002337 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002338 &rsp_iov, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002339
2340qinf_exit:
2341 free_rsp_buf(resp_buftype, rsp);
2342 return rc;
2343}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002344
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002345int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002346 u64 persistent_fid, u64 volatile_fid,
2347 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002348{
2349 return query_info(xid, tcon, persistent_fid, volatile_fid,
2350 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002351 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002352 sizeof(struct smb2_file_full_ea_info),
2353 (void **)&data,
2354 NULL);
2355}
2356
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002357int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2358 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002359{
2360 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002361 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002362 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002363 sizeof(struct smb2_file_all_info), (void **)&data,
2364 NULL);
2365}
2366
2367int
2368SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2369 u64 persistent_fid, u64 volatile_fid,
2370 void **data, u32 *plen)
2371{
2372 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2373 *plen = 0;
2374
2375 return query_info(xid, tcon, persistent_fid, volatile_fid,
2376 0, SMB2_O_INFO_SECURITY, additional_info,
2377 SMB2_MAX_BUFFER_SIZE,
2378 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002379}
2380
2381int
2382SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2383 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2384{
2385 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002386 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002387 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002388 sizeof(struct smb2_file_internal_info),
2389 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002390}
2391
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002392/*
2393 * This is a no-op for now. We're not really interested in the reply, but
2394 * rather in the fact that the server sent one and that server->lstrp
2395 * gets updated.
2396 *
2397 * FIXME: maybe we should consider checking that the reply matches request?
2398 */
2399static void
2400smb2_echo_callback(struct mid_q_entry *mid)
2401{
2402 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002403 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002404 unsigned int credits_received = 1;
2405
2406 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002407 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002408
2409 DeleteMidQEntry(mid);
2410 add_credits(server, credits_received, CIFS_ECHO_OP);
2411}
2412
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002413void smb2_reconnect_server(struct work_struct *work)
2414{
2415 struct TCP_Server_Info *server = container_of(work,
2416 struct TCP_Server_Info, reconnect.work);
2417 struct cifs_ses *ses;
2418 struct cifs_tcon *tcon, *tcon2;
2419 struct list_head tmp_list;
2420 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002421 int rc;
2422 int resched = false;
2423
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002424
2425 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2426 mutex_lock(&server->reconnect_mutex);
2427
2428 INIT_LIST_HEAD(&tmp_list);
2429 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2430
2431 spin_lock(&cifs_tcp_ses_lock);
2432 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2433 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002434 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002435 tcon->tc_count++;
2436 list_add_tail(&tcon->rlist, &tmp_list);
2437 tcon_exist = true;
2438 }
2439 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002440 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2441 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2442 tcon_exist = true;
2443 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002444 }
2445 /*
2446 * Get the reference to server struct to be sure that the last call of
2447 * cifs_put_tcon() in the loop below won't release the server pointer.
2448 */
2449 if (tcon_exist)
2450 server->srv_count++;
2451
2452 spin_unlock(&cifs_tcp_ses_lock);
2453
2454 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002455 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2456 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002457 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002458 else
2459 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002460 list_del_init(&tcon->rlist);
2461 cifs_put_tcon(tcon);
2462 }
2463
2464 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002465 if (resched)
2466 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002467 mutex_unlock(&server->reconnect_mutex);
2468
2469 /* now we can safely release srv struct */
2470 if (tcon_exist)
2471 cifs_put_tcp_session(server, 1);
2472}
2473
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002474int
2475SMB2_echo(struct TCP_Server_Info *server)
2476{
2477 struct smb2_echo_req *req;
2478 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002479 struct kvec iov[2];
2480 struct smb_rqst rqst = { .rq_iov = iov,
2481 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002482 unsigned int total_len;
2483 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002484
Joe Perchesf96637b2013-05-04 22:12:25 -05002485 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002486
Steve French4fcd1812016-06-22 20:12:05 -05002487 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002488 /* No need to send echo on newly established connections */
2489 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2490 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002491 }
2492
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002493 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002494 if (rc)
2495 return rc;
2496
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002497 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002498
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002499 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002500 rfc1002_marker = cpu_to_be32(total_len);
2501 iov[0].iov_base = &rfc1002_marker;
2502 iov[1].iov_len = total_len;
2503 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002504
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002505 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2506 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002507 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002508 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002509
2510 cifs_small_buf_release(req);
2511 return rc;
2512}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002513
2514int
2515SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2516 u64 volatile_fid)
2517{
2518 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002519 struct cifs_ses *ses = tcon->ses;
2520 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002521 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002522 int resp_buftype;
2523 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002524 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002525 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002526
Joe Perchesf96637b2013-05-04 22:12:25 -05002527 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002528
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002529 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002530 return -EIO;
2531
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002532 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002533 if (rc)
2534 return rc;
2535
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002536 if (encryption_required(tcon))
2537 flags |= CIFS_TRANSFORM_REQ;
2538
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002539 req->PersistentFileId = persistent_fid;
2540 req->VolatileFileId = volatile_fid;
2541
2542 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002543 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002544
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002545 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002546 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002547
Steve Frenchdfebe402015-03-27 01:00:06 -05002548 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002549 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2550
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002551 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002552 return rc;
2553}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002554
2555/*
2556 * To form a chain of read requests, any read requests after the first should
2557 * have the end_of_chain boolean set to true.
2558 */
2559static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002560smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002561 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2562 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002563{
2564 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002565 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002566 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002567 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002568
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002569 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2570 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002571 if (rc)
2572 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002573
2574 server = io_parms->tcon->ses->server;
2575 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002576 return -ECONNABORTED;
2577
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002578 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002579 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002580
2581 req->PersistentFileId = io_parms->persistent_fid;
2582 req->VolatileFileId = io_parms->volatile_fid;
2583 req->ReadChannelInfoOffset = 0; /* reserved */
2584 req->ReadChannelInfoLength = 0; /* reserved */
2585 req->Channel = 0; /* reserved */
2586 req->MinimumCount = 0;
2587 req->Length = cpu_to_le32(io_parms->length);
2588 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07002589#ifdef CONFIG_CIFS_SMB_DIRECT
2590 /*
2591 * If we want to do a RDMA write, fill in and append
2592 * smbd_buffer_descriptor_v1 to the end of read request
2593 */
2594 if (server->rdma && rdata &&
2595 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002596
Long Libd3dcc62017-11-22 17:38:47 -07002597 struct smbd_buffer_descriptor_v1 *v1;
2598 bool need_invalidate =
2599 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2600
2601 rdata->mr = smbd_register_mr(
2602 server->smbd_conn, rdata->pages,
2603 rdata->nr_pages, rdata->tailsz,
2604 true, need_invalidate);
2605 if (!rdata->mr)
2606 return -ENOBUFS;
2607
2608 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2609 if (need_invalidate)
2610 req->Channel = SMB2_CHANNEL_RDMA_V1;
2611 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002612 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07002613 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002614 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07002615 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002616 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2617 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2618 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07002619
2620 *total_len += sizeof(*v1) - 1;
2621 }
2622#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002623 if (request_type & CHAINED_REQUEST) {
2624 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002625 /* next 8-byte aligned request */
2626 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2627 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002628 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002629 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002630 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002631 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002632 /*
2633 * Related requests use info from previous read request
2634 * in chain.
2635 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002636 shdr->SessionId = 0xFFFFFFFF;
2637 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002638 req->PersistentFileId = 0xFFFFFFFF;
2639 req->VolatileFileId = 0xFFFFFFFF;
2640 }
2641 }
2642 if (remaining_bytes > io_parms->length)
2643 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2644 else
2645 req->RemainingBytes = 0;
2646
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002647 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002648 return rc;
2649}
2650
2651static void
2652smb2_readv_callback(struct mid_q_entry *mid)
2653{
2654 struct cifs_readdata *rdata = mid->callback_data;
2655 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2656 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002657 struct smb2_sync_hdr *shdr =
2658 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002659 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002660 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2661 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002662 .rq_pages = rdata->pages,
2663 .rq_npages = rdata->nr_pages,
2664 .rq_pagesz = rdata->pagesz,
2665 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002666
Joe Perchesf96637b2013-05-04 22:12:25 -05002667 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2668 __func__, mid->mid, mid->mid_state, rdata->result,
2669 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002670
2671 switch (mid->mid_state) {
2672 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002673 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002674 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002675 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002676 int rc;
2677
Jeff Layton0b688cf2012-09-18 16:20:34 -07002678 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002679 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002680 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2681 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002682 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002683 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002684 task_io_account_read(rdata->got_bytes);
2685 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002686 break;
2687 case MID_REQUEST_SUBMITTED:
2688 case MID_RETRY_NEEDED:
2689 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002690 if (server->sign && rdata->got_bytes)
2691 /* reset bytes number since we can not check a sign */
2692 rdata->got_bytes = 0;
2693 /* FIXME: should this be counted toward the initiating task? */
2694 task_io_account_read(rdata->got_bytes);
2695 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002696 break;
2697 default:
2698 if (rdata->result != -ENODATA)
2699 rdata->result = -EIO;
2700 }
Long Libd3dcc62017-11-22 17:38:47 -07002701#ifdef CONFIG_CIFS_SMB_DIRECT
2702 /*
2703 * If this rdata has a memmory registered, the MR can be freed
2704 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2705 * because they have limited number and are used for future I/Os
2706 */
2707 if (rdata->mr) {
2708 smbd_deregister_mr(rdata->mr);
2709 rdata->mr = NULL;
2710 }
2711#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002712 if (rdata->result)
2713 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2714
2715 queue_work(cifsiod_wq, &rdata->work);
2716 DeleteMidQEntry(mid);
2717 add_credits(server, credits_received, 0);
2718}
2719
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002720/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002721int
2722smb2_async_readv(struct cifs_readdata *rdata)
2723{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002724 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002725 char *buf;
2726 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002727 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002728 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2729 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002730 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002731 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002732 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002733
Joe Perchesf96637b2013-05-04 22:12:25 -05002734 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2735 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002736
2737 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2738 io_parms.offset = rdata->offset;
2739 io_parms.length = rdata->bytes;
2740 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2741 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2742 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002743
2744 server = io_parms.tcon->ses->server;
2745
Long Li2dabfd52017-11-07 01:54:53 -07002746 rc = smb2_new_read_req(
2747 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002748 if (rc) {
2749 if (rc == -EAGAIN && rdata->credits) {
2750 /* credits was reset by reconnect */
2751 rdata->credits = 0;
2752 /* reduce in_flight value since we won't send the req */
2753 spin_lock(&server->req_lock);
2754 server->in_flight--;
2755 spin_unlock(&server->req_lock);
2756 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002757 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002758 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002759
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002760 if (encryption_required(io_parms.tcon))
2761 flags |= CIFS_TRANSFORM_REQ;
2762
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002763 req_len = cpu_to_be32(total_len);
2764
2765 rdata->iov[0].iov_base = &req_len;
2766 rdata->iov[0].iov_len = sizeof(__be32);
2767 rdata->iov[1].iov_base = buf;
2768 rdata->iov[1].iov_len = total_len;
2769
2770 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002771
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002772 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002773 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002774 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002775 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002776 spin_lock(&server->req_lock);
2777 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002778 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002779 spin_unlock(&server->req_lock);
2780 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002781 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002782 }
2783
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002784 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002785 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002786 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002787 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002788 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002789 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002790 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2791 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002792
2793 cifs_small_buf_release(buf);
2794 return rc;
2795}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002796
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002797int
2798SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2799 unsigned int *nbytes, char **buf, int *buf_type)
2800{
2801 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002802 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002803 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002804 struct smb2_sync_hdr *shdr;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002805 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002806 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002807 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002808 int flags = CIFS_LOG_ERROR;
2809 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002810
2811 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07002812 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002813 if (rc)
2814 return rc;
2815
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002816 if (encryption_required(io_parms->tcon))
2817 flags |= CIFS_TRANSFORM_REQ;
2818
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002819 iov[0].iov_base = (char *)req;
2820 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002821
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002822 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002823 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002824
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002825 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002826
2827 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002828 if (rc != -ENODATA) {
2829 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2830 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002831 }
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002832 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2833 return rc == -ENODATA ? 0 : rc;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002834 }
2835
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002836 *nbytes = le32_to_cpu(rsp->DataLength);
2837 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2838 (*nbytes > io_parms->length)) {
2839 cifs_dbg(FYI, "bad length %d for count %d\n",
2840 *nbytes, io_parms->length);
2841 rc = -EIO;
2842 *nbytes = 0;
2843 }
2844
2845 shdr = get_sync_hdr(rsp);
2846
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002847 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002848 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002849 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002850 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002851 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002852 if (resp_buftype == CIFS_SMALL_BUFFER)
2853 *buf_type = CIFS_SMALL_BUFFER;
2854 else if (resp_buftype == CIFS_LARGE_BUFFER)
2855 *buf_type = CIFS_LARGE_BUFFER;
2856 }
2857 return rc;
2858}
2859
Pavel Shilovsky33319142012-09-18 16:20:29 -07002860/*
2861 * Check the mid_state and signature on received buffer (if any), and queue the
2862 * workqueue completion task.
2863 */
2864static void
2865smb2_writev_callback(struct mid_q_entry *mid)
2866{
2867 struct cifs_writedata *wdata = mid->callback_data;
2868 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2869 unsigned int written;
2870 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2871 unsigned int credits_received = 1;
2872
2873 switch (mid->mid_state) {
2874 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002875 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002876 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2877 if (wdata->result != 0)
2878 break;
2879
2880 written = le32_to_cpu(rsp->DataLength);
2881 /*
2882 * Mask off high 16 bits when bytes written as returned
2883 * by the server is greater than bytes requested by the
2884 * client. OS/2 servers are known to set incorrect
2885 * CountHigh values.
2886 */
2887 if (written > wdata->bytes)
2888 written &= 0xFFFF;
2889
2890 if (written < wdata->bytes)
2891 wdata->result = -ENOSPC;
2892 else
2893 wdata->bytes = written;
2894 break;
2895 case MID_REQUEST_SUBMITTED:
2896 case MID_RETRY_NEEDED:
2897 wdata->result = -EAGAIN;
2898 break;
2899 default:
2900 wdata->result = -EIO;
2901 break;
2902 }
Long Lidb223a52017-11-22 17:38:45 -07002903#ifdef CONFIG_CIFS_SMB_DIRECT
2904 /*
2905 * If this wdata has a memory registered, the MR can be freed
2906 * The number of MRs available is limited, it's important to recover
2907 * used MR as soon as I/O is finished. Hold MR longer in the later
2908 * I/O process can possibly result in I/O deadlock due to lack of MR
2909 * to send request on I/O retry
2910 */
2911 if (wdata->mr) {
2912 smbd_deregister_mr(wdata->mr);
2913 wdata->mr = NULL;
2914 }
2915#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002916 if (wdata->result)
2917 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2918
2919 queue_work(cifsiod_wq, &wdata->work);
2920 DeleteMidQEntry(mid);
2921 add_credits(tcon->ses->server, credits_received, 0);
2922}
2923
2924/* smb2_async_writev - send an async write, and set up mid to handle result */
2925int
Steve French4a5c80d2014-02-07 20:45:12 -06002926smb2_async_writev(struct cifs_writedata *wdata,
2927 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002928{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002929 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002930 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002931 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002932 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002933 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002934 struct kvec iov[2];
2935 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002936 unsigned int total_len;
2937 __be32 rfc1002_marker;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002938
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002939 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002940 if (rc) {
2941 if (rc == -EAGAIN && wdata->credits) {
2942 /* credits was reset by reconnect */
2943 wdata->credits = 0;
2944 /* reduce in_flight value since we won't send the req */
2945 spin_lock(&server->req_lock);
2946 server->in_flight--;
2947 spin_unlock(&server->req_lock);
2948 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002949 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002950 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002951
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002952 if (encryption_required(tcon))
2953 flags |= CIFS_TRANSFORM_REQ;
2954
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002955 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002956 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002957
2958 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2959 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2960 req->WriteChannelInfoOffset = 0;
2961 req->WriteChannelInfoLength = 0;
2962 req->Channel = 0;
2963 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002964 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002965 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07002966 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07002967#ifdef CONFIG_CIFS_SMB_DIRECT
2968 /*
2969 * If we want to do a server RDMA read, fill in and append
2970 * smbd_buffer_descriptor_v1 to the end of write request
2971 */
2972 if (server->rdma && wdata->bytes >=
2973 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07002974
Long Lidb223a52017-11-22 17:38:45 -07002975 struct smbd_buffer_descriptor_v1 *v1;
2976 bool need_invalidate = server->dialect == SMB30_PROT_ID;
2977
2978 wdata->mr = smbd_register_mr(
2979 server->smbd_conn, wdata->pages,
2980 wdata->nr_pages, wdata->tailsz,
2981 false, need_invalidate);
2982 if (!wdata->mr) {
2983 rc = -ENOBUFS;
2984 goto async_writev_out;
2985 }
2986 req->Length = 0;
2987 req->DataOffset = 0;
2988 req->RemainingBytes =
Steve French2026b062018-01-24 23:07:41 -06002989 cpu_to_le32((wdata->nr_pages-1)*PAGE_SIZE + wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07002990 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2991 if (need_invalidate)
2992 req->Channel = SMB2_CHANNEL_RDMA_V1;
2993 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002994 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07002995 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002996 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07002997 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002998 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
2999 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3000 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003001 }
3002#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003003 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003004 iov[0].iov_len = 4;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003005 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
3006 iov[0].iov_base = &rfc1002_marker;
3007 iov[1].iov_len = total_len - 1;
3008 iov[1].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003009
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003010 rqst.rq_iov = iov;
3011 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003012 rqst.rq_pages = wdata->pages;
3013 rqst.rq_npages = wdata->nr_pages;
3014 rqst.rq_pagesz = wdata->pagesz;
3015 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003016#ifdef CONFIG_CIFS_SMB_DIRECT
3017 if (wdata->mr) {
3018 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3019 rqst.rq_npages = 0;
3020 }
3021#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003022 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3023 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003024
Long Lidb223a52017-11-22 17:38:45 -07003025#ifdef CONFIG_CIFS_SMB_DIRECT
3026 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3027 if (!wdata->mr)
3028 req->Length = cpu_to_le32(wdata->bytes);
3029#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003030 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003031#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003032
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003033 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003034 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003035 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003036 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003037 spin_lock(&server->req_lock);
3038 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003039 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003040 spin_unlock(&server->req_lock);
3041 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003042 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003043 }
3044
Pavel Shilovsky33319142012-09-18 16:20:29 -07003045 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003046 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3047 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003048
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003049 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06003050 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003051 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3052 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003053
Pavel Shilovsky33319142012-09-18 16:20:29 -07003054async_writev_out:
3055 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003056 return rc;
3057}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003058
3059/*
3060 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3061 * The length field from io_parms must be at least 1 and indicates a number of
3062 * elements with data to write that begins with position 1 in iov array. All
3063 * data length is specified by count.
3064 */
3065int
3066SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3067 unsigned int *nbytes, struct kvec *iov, int n_vec)
3068{
3069 int rc = 0;
3070 struct smb2_write_req *req = NULL;
3071 struct smb2_write_rsp *rsp = NULL;
3072 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003073 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003074 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003075 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003076
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003077 *nbytes = 0;
3078
3079 if (n_vec < 1)
3080 return rc;
3081
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003082 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3083 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003084 if (rc)
3085 return rc;
3086
3087 if (io_parms->tcon->ses->server == NULL)
3088 return -ECONNABORTED;
3089
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003090 if (encryption_required(io_parms->tcon))
3091 flags |= CIFS_TRANSFORM_REQ;
3092
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003093 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003094
3095 req->PersistentFileId = io_parms->persistent_fid;
3096 req->VolatileFileId = io_parms->volatile_fid;
3097 req->WriteChannelInfoOffset = 0;
3098 req->WriteChannelInfoLength = 0;
3099 req->Channel = 0;
3100 req->Length = cpu_to_le32(io_parms->length);
3101 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003102 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003103 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003104 req->RemainingBytes = 0;
3105
3106 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003107 /* 1 for Buffer */
3108 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003109
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003110 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
3111 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003112 cifs_small_buf_release(req);
3113 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003114
3115 if (rc) {
3116 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003117 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003118 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003119 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003120
3121 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003122 return rc;
3123}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003124
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003125static unsigned int
3126num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3127{
3128 int len;
3129 unsigned int entrycount = 0;
3130 unsigned int next_offset = 0;
3131 FILE_DIRECTORY_INFO *entryptr;
3132
3133 if (bufstart == NULL)
3134 return 0;
3135
3136 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3137
3138 while (1) {
3139 entryptr = (FILE_DIRECTORY_INFO *)
3140 ((char *)entryptr + next_offset);
3141
3142 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003143 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003144 break;
3145 }
3146
3147 len = le32_to_cpu(entryptr->FileNameLength);
3148 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003149 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3150 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003151 break;
3152 }
3153
3154 *lastentry = (char *)entryptr;
3155 entrycount++;
3156
3157 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3158 if (!next_offset)
3159 break;
3160 }
3161
3162 return entrycount;
3163}
3164
3165/*
3166 * Readdir/FindFirst
3167 */
3168int
3169SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3170 u64 persistent_fid, u64 volatile_fid, int index,
3171 struct cifs_search_info *srch_inf)
3172{
3173 struct smb2_query_directory_req *req;
3174 struct smb2_query_directory_rsp *rsp = NULL;
3175 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003176 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003177 int rc = 0;
3178 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003179 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003180 unsigned char *bufptr;
3181 struct TCP_Server_Info *server;
3182 struct cifs_ses *ses = tcon->ses;
3183 __le16 asteriks = cpu_to_le16('*');
3184 char *end_of_smb;
3185 unsigned int output_size = CIFSMaxBufSize;
3186 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003187 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003188 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003189
3190 if (ses && (ses->server))
3191 server = ses->server;
3192 else
3193 return -EIO;
3194
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003195 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3196 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003197 if (rc)
3198 return rc;
3199
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003200 if (encryption_required(tcon))
3201 flags |= CIFS_TRANSFORM_REQ;
3202
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003203 switch (srch_inf->info_level) {
3204 case SMB_FIND_FILE_DIRECTORY_INFO:
3205 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3206 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3207 break;
3208 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3209 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3210 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3211 break;
3212 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003213 cifs_dbg(VFS, "info level %u isn't supported\n",
3214 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003215 rc = -EINVAL;
3216 goto qdir_exit;
3217 }
3218
3219 req->FileIndex = cpu_to_le32(index);
3220 req->PersistentFileId = persistent_fid;
3221 req->VolatileFileId = volatile_fid;
3222
3223 len = 0x2;
3224 bufptr = req->Buffer;
3225 memcpy(bufptr, &asteriks, len);
3226
3227 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003228 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003229 req->FileNameLength = cpu_to_le16(len);
3230 /*
3231 * BB could be 30 bytes or so longer if we used SMB2 specific
3232 * buffer lengths, but this is safe and close enough.
3233 */
3234 output_size = min_t(unsigned int, output_size, server->maxBuf);
3235 output_size = min_t(unsigned int, output_size, 2 << 15);
3236 req->OutputBufferLength = cpu_to_le32(output_size);
3237
3238 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003239 /* 1 for Buffer */
3240 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003241
3242 iov[1].iov_base = (char *)(req->Buffer);
3243 iov[1].iov_len = len;
3244
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003245 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003246 cifs_small_buf_release(req);
3247 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003248
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003249 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003250 if (rc == -ENODATA &&
3251 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003252 srch_inf->endOfSearch = true;
3253 rc = 0;
3254 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003255 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3256 goto qdir_exit;
3257 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003258
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003259 rc = validate_iov(server,
3260 le16_to_cpu(rsp->OutputBufferOffset),
3261 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003262 info_buf_size);
3263 if (rc)
3264 goto qdir_exit;
3265
3266 srch_inf->unicode = true;
3267
3268 if (srch_inf->ntwrk_buf_start) {
3269 if (srch_inf->smallBuf)
3270 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3271 else
3272 cifs_buf_release(srch_inf->ntwrk_buf_start);
3273 }
3274 srch_inf->ntwrk_buf_start = (char *)rsp;
3275 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3276 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3277 /* 4 for rfc1002 length field */
3278 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3279 srch_inf->entries_in_buffer =
3280 num_entries(srch_inf->srch_entries_start, end_of_smb,
3281 &srch_inf->last_entry, info_buf_size);
3282 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003283 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3284 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3285 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003286 if (resp_buftype == CIFS_LARGE_BUFFER)
3287 srch_inf->smallBuf = false;
3288 else if (resp_buftype == CIFS_SMALL_BUFFER)
3289 srch_inf->smallBuf = true;
3290 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003291 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003292
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003293 return rc;
3294
3295qdir_exit:
3296 free_rsp_buf(resp_buftype, rsp);
3297 return rc;
3298}
3299
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003300static int
3301send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003302 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3303 u8 info_type, u32 additional_info, unsigned int num,
3304 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003305{
3306 struct smb2_set_info_req *req;
3307 struct smb2_set_info_rsp *rsp = NULL;
3308 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003309 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003310 int rc = 0;
3311 int resp_buftype;
3312 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003313 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003314 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003315 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003316
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003317 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003318 return -EIO;
3319
3320 if (!num)
3321 return -EINVAL;
3322
3323 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3324 if (!iov)
3325 return -ENOMEM;
3326
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003327 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003328 if (rc) {
3329 kfree(iov);
3330 return rc;
3331 }
3332
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003333 if (encryption_required(tcon))
3334 flags |= CIFS_TRANSFORM_REQ;
3335
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003336 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003337
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003338 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003339 req->FileInfoClass = info_class;
3340 req->PersistentFileId = persistent_fid;
3341 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003342 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003343
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003344 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003345 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003346 req->BufferLength = cpu_to_le32(*size);
3347
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003348 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003349 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003350
3351 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003352 /* 1 for Buffer */
3353 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003354
3355 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003356 le32_add_cpu(&req->BufferLength, size[i]);
3357 iov[i].iov_base = (char *)data[i];
3358 iov[i].iov_len = size[i];
3359 }
3360
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003361 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3362 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003363 cifs_small_buf_release(req);
3364 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003365
Steve French7d3fb242013-11-18 09:56:28 -06003366 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003367 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003368
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003369 free_rsp_buf(resp_buftype, rsp);
3370 kfree(iov);
3371 return rc;
3372}
3373
3374int
3375SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3376 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3377{
3378 struct smb2_file_rename_info info;
3379 void **data;
3380 unsigned int size[2];
3381 int rc;
3382 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3383
3384 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3385 if (!data)
3386 return -ENOMEM;
3387
3388 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3389 /* 0 = fail if target already exists */
3390 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3391 info.FileNameLength = cpu_to_le32(len);
3392
3393 data[0] = &info;
3394 size[0] = sizeof(struct smb2_file_rename_info);
3395
3396 data[1] = target_file;
3397 size[1] = len + 2 /* null */;
3398
3399 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003400 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3401 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003402 kfree(data);
3403 return rc;
3404}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003405
3406int
Steve French897fba12016-05-12 21:20:36 -05003407SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3408 u64 persistent_fid, u64 volatile_fid)
3409{
3410 __u8 delete_pending = 1;
3411 void *data;
3412 unsigned int size;
3413
3414 data = &delete_pending;
3415 size = 1; /* sizeof __u8 */
3416
3417 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003418 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3419 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003420}
3421
3422int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003423SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3424 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3425{
3426 struct smb2_file_link_info info;
3427 void **data;
3428 unsigned int size[2];
3429 int rc;
3430 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3431
3432 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3433 if (!data)
3434 return -ENOMEM;
3435
3436 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3437 /* 0 = fail if link already exists */
3438 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3439 info.FileNameLength = cpu_to_le32(len);
3440
3441 data[0] = &info;
3442 size[0] = sizeof(struct smb2_file_link_info);
3443
3444 data[1] = target_file;
3445 size[1] = len + 2 /* null */;
3446
3447 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003448 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3449 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003450 kfree(data);
3451 return rc;
3452}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003453
3454int
3455SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003456 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003457{
3458 struct smb2_file_eof_info info;
3459 void *data;
3460 unsigned int size;
3461
3462 info.EndOfFile = *eof;
3463
3464 data = &info;
3465 size = sizeof(struct smb2_file_eof_info);
3466
Steve Frenchf29ebb42014-07-19 21:44:58 -05003467 if (is_falloc)
3468 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003469 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3470 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003471 else
3472 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003473 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3474 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003475}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003476
3477int
3478SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3479 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3480{
3481 unsigned int size;
3482 size = sizeof(FILE_BASIC_INFO);
3483 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003484 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3485 0, 1, (void **)&buf, &size);
3486}
3487
3488int
3489SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3490 u64 persistent_fid, u64 volatile_fid,
3491 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3492{
3493 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3494 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3495 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003496}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003497
3498int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003499SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3500 u64 persistent_fid, u64 volatile_fid,
3501 struct smb2_file_full_ea_info *buf, int len)
3502{
3503 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3504 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3505 0, 1, (void **)&buf, &len);
3506}
3507
3508int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003509SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3510 const u64 persistent_fid, const u64 volatile_fid,
3511 __u8 oplock_level)
3512{
3513 int rc;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003514 struct smb2_oplock_break_req *req = NULL;
3515 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003516 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003517 unsigned int total_len;
3518 struct kvec iov[1];
3519 struct kvec rsp_iov;
3520 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003521
Joe Perchesf96637b2013-05-04 22:12:25 -05003522 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003523 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3524 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003525 if (rc)
3526 return rc;
3527
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003528 if (encryption_required(tcon))
3529 flags |= CIFS_TRANSFORM_REQ;
3530
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003531 req->VolatileFid = volatile_fid;
3532 req->PersistentFid = persistent_fid;
3533 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003534 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003535
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003536 flags |= CIFS_NO_RESP;
3537
3538 iov[0].iov_base = (char *)req;
3539 iov[0].iov_len = total_len;
3540
3541 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003542 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003543
3544 if (rc) {
3545 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003546 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003547 }
3548
3549 return rc;
3550}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003551
3552static void
3553copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3554 struct kstatfs *kst)
3555{
3556 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3557 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3558 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303559 kst->f_bfree = kst->f_bavail =
3560 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003561 return;
3562}
3563
3564static int
3565build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3566 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3567{
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003568 struct TCP_Server_Info *server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003569 int rc;
3570 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003571 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003572
Joe Perchesf96637b2013-05-04 22:12:25 -05003573 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003574
3575 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3576 return -EIO;
3577
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003578 server = tcon->ses->server;
3579
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003580 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3581 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003582 if (rc)
3583 return rc;
3584
3585 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3586 req->FileInfoClass = level;
3587 req->PersistentFileId = persistent_fid;
3588 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003589 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003590 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003591 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003592 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003593 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - server->vals->header_preamble_size);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003594
3595 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003596 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003597 return 0;
3598}
3599
3600int
3601SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3602 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3603{
3604 struct smb2_query_info_rsp *rsp = NULL;
3605 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003606 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003607 int rc = 0;
3608 int resp_buftype;
3609 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003610 struct TCP_Server_Info *server = ses->server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003611 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003612 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003613
3614 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3615 sizeof(struct smb2_fs_full_size_info),
3616 persistent_fid, volatile_fid);
3617 if (rc)
3618 return rc;
3619
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003620 if (encryption_required(tcon))
3621 flags |= CIFS_TRANSFORM_REQ;
3622
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003623 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003624 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003625 if (rc) {
3626 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003627 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003628 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003629 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003630
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003631 info = (struct smb2_fs_full_size_info *)(server->vals->header_preamble_size +
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003632 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003633 rc = validate_iov(server,
3634 le16_to_cpu(rsp->OutputBufferOffset),
3635 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003636 sizeof(struct smb2_fs_full_size_info));
3637 if (!rc)
3638 copy_fs_info_to_kstatfs(info, fsdata);
3639
Steve French34f62642013-10-09 02:07:00 -05003640qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003641 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003642 return rc;
3643}
3644
3645int
3646SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003647 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003648{
3649 struct smb2_query_info_rsp *rsp = NULL;
3650 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003651 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003652 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003653 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003654 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003655 struct TCP_Server_Info *server = ses->server;
Steve French34f62642013-10-09 02:07:00 -05003656 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003657 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003658
Steven French21671142013-10-09 13:36:35 -05003659 if (level == FS_DEVICE_INFORMATION) {
3660 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3661 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3662 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3663 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3664 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003665 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3666 max_len = sizeof(struct smb3_fs_ss_info);
3667 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003668 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003669 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003670 return -EINVAL;
3671 }
3672
3673 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003674 persistent_fid, volatile_fid);
3675 if (rc)
3676 return rc;
3677
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003678 if (encryption_required(tcon))
3679 flags |= CIFS_TRANSFORM_REQ;
3680
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003681 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003682 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003683 if (rc) {
3684 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3685 goto qfsattr_exit;
3686 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003687 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003688
3689 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3690 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003691 rc = validate_iov(server, offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05003692 if (rc)
3693 goto qfsattr_exit;
3694
3695 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003696 memcpy(&tcon->fsAttrInfo, server->vals->header_preamble_size + offset
Steve French34f62642013-10-09 02:07:00 -05003697 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003698 rsp_len, max_len));
3699 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003700 memcpy(&tcon->fsDevInfo, server->vals->header_preamble_size + offset
Steven French21671142013-10-09 13:36:35 -05003701 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003702 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3703 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003704 (server->vals->header_preamble_size + offset + (char *)&rsp->hdr);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003705 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3706 tcon->perf_sector_size =
3707 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3708 }
Steve French34f62642013-10-09 02:07:00 -05003709
3710qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003711 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003712 return rc;
3713}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003714
3715int
3716smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3717 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3718 const __u32 num_lock, struct smb2_lock_element *buf)
3719{
3720 int rc = 0;
3721 struct smb2_lock_req *req = NULL;
3722 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003723 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003724 int resp_buf_type;
3725 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003726 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003727 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003728
Joe Perchesf96637b2013-05-04 22:12:25 -05003729 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003730
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003731 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003732 if (rc)
3733 return rc;
3734
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003735 if (encryption_required(tcon))
3736 flags |= CIFS_TRANSFORM_REQ;
3737
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003738 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003739 req->LockCount = cpu_to_le16(num_lock);
3740
3741 req->PersistentFileId = persist_fid;
3742 req->VolatileFileId = volatile_fid;
3743
3744 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003745
3746 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003747 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003748 iov[1].iov_base = (char *)buf;
3749 iov[1].iov_len = count;
3750
3751 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003752 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3753 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003754 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003755 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003756 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003757 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3758 }
3759
3760 return rc;
3761}
3762
3763int
3764SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3765 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3766 const __u64 length, const __u64 offset, const __u32 lock_flags,
3767 const bool wait)
3768{
3769 struct smb2_lock_element lock;
3770
3771 lock.Offset = cpu_to_le64(offset);
3772 lock.Length = cpu_to_le64(length);
3773 lock.Flags = cpu_to_le32(lock_flags);
3774 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3775 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3776
3777 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3778}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003779
3780int
3781SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3782 __u8 *lease_key, const __le32 lease_state)
3783{
3784 int rc;
3785 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003786 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003787 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003788 unsigned int total_len;
3789 struct kvec iov[1];
3790 struct kvec rsp_iov;
3791 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003792
Joe Perchesf96637b2013-05-04 22:12:25 -05003793 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003794 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3795 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003796 if (rc)
3797 return rc;
3798
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003799 if (encryption_required(tcon))
3800 flags |= CIFS_TRANSFORM_REQ;
3801
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003802 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003803 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003804 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003805
3806 memcpy(req->LeaseKey, lease_key, 16);
3807 req->LeaseState = lease_state;
3808
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003809 flags |= CIFS_NO_RESP;
3810
3811 iov[0].iov_base = (char *)req;
3812 iov[0].iov_len = total_len;
3813
3814 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003815 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003816
3817 if (rc) {
3818 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003819 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003820 }
3821
3822 return rc;
3823}