blob: 0f48741a0130d35286fec1e031b5eea45e367699 [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{
Long Li2796d302018-04-25 11:30:04 -0700733 int rc;
734 struct validate_negotiate_info_req *pneg_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
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100741 /* In SMB3.11 preauth integrity supersedes validate negotiate */
742 if (tcon->ses->server->dialect == SMB311_PROT_ID)
743 return 0;
744
Steve Frenchff1c0382013-11-19 23:44:46 -0600745 /*
746 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500747 * can not sign it (ie are not known user). Even if signing is not
748 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600749 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500750 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600751 */
Steve French0603c962017-09-20 19:57:18 -0500752 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600753 return 0; /* validation requires signing */
754
Steve French0603c962017-09-20 19:57:18 -0500755 if (tcon->ses->user_name == NULL) {
756 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
757 return 0; /* validation requires signing */
758 }
759
760 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
761 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
762
Long Li2796d302018-04-25 11:30:04 -0700763 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
764 if (!pneg_inbuf)
765 return -ENOMEM;
766
767 pneg_inbuf->Capabilities =
Steve Frenchff1c0382013-11-19 23:44:46 -0600768 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Long Li2796d302018-04-25 11:30:04 -0700769 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100770 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600771
772 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -0700773 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600774 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
775 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -0700776 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600777 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
778 else
Long Li2796d302018-04-25 11:30:04 -0700779 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600780
Steve French9764c022017-09-17 10:41:35 -0500781
782 if (strcmp(tcon->ses->server->vals->version_string,
783 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700784 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
785 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
786 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -0500787 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -0700788 inbuflen = sizeof(*pneg_inbuf) -
789 sizeof(pneg_inbuf->Dialects[0]);
Steve French9764c022017-09-17 10:41:35 -0500790 } else if (strcmp(tcon->ses->server->vals->version_string,
791 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700792 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
793 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
794 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
795 pneg_inbuf->DialectCount = cpu_to_le16(3);
Steve French9764c022017-09-17 10:41:35 -0500796 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -0700797 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -0500798 } else {
799 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -0700800 pneg_inbuf->Dialects[0] =
Steve French9764c022017-09-17 10:41:35 -0500801 cpu_to_le16(tcon->ses->server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -0700802 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -0500803 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -0700804 inbuflen = sizeof(*pneg_inbuf) -
805 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -0500806 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600807
808 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
809 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Long Li2796d302018-04-25 11:30:04 -0700810 (char *)pneg_inbuf, inbuflen, (char **)&pneg_rsp, &rsplen);
Steve Frenchff1c0382013-11-19 23:44:46 -0600811
812 if (rc != 0) {
813 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -0700814 rc = -EIO;
815 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -0600816 }
817
Long Li2796d302018-04-25 11:30:04 -0700818 rc = -EIO;
819 if (rsplen != sizeof(*pneg_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500820 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
821 rsplen);
822
823 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -0700824 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
825 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -0600826 }
827
828 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +0100829 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600830 goto vneg_out;
831
832 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
833 goto vneg_out;
834
835 /* do not validate server guid because not saved at negprot time yet */
836
837 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
838 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
839 goto vneg_out;
840
841 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -0700842 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600843 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -0700844 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -0600845
846vneg_out:
847 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -0700848out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200849 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -0700850out_free_inbuf:
851 kfree(pneg_inbuf);
852 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -0600853}
854
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530855enum securityEnum
856smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
857{
858 switch (requested) {
859 case Kerberos:
860 case RawNTLMSSP:
861 return requested;
862 case NTLMv2:
863 return RawNTLMSSP;
864 case Unspecified:
865 if (server->sec_ntlmssp &&
866 (global_secflags & CIFSSEC_MAY_NTLMSSP))
867 return RawNTLMSSP;
868 if ((server->sec_kerberos || server->sec_mskerberos) &&
869 (global_secflags & CIFSSEC_MAY_KRB5))
870 return Kerberos;
871 /* Fallthrough */
872 default:
873 return Unspecified;
874 }
875}
876
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100877struct SMB2_sess_data {
878 unsigned int xid;
879 struct cifs_ses *ses;
880 struct nls_table *nls_cp;
881 void (*func)(struct SMB2_sess_data *);
882 int result;
883 u64 previous_session;
884
885 /* we will send the SMB in three pieces:
886 * a fixed length beginning part, an optional
887 * SPNEGO blob (which can be zero length), and a
888 * last part which will include the strings
889 * and rest of bcc area. This allows us to avoid
890 * a large buffer 17K allocation
891 */
892 int buf0_type;
893 struct kvec iov[2];
894};
895
896static int
897SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
898{
899 int rc;
900 struct cifs_ses *ses = sess_data->ses;
901 struct smb2_sess_setup_req *req;
902 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100903 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100904
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100905 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
906 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100907 if (rc)
908 return rc;
909
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700910 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100911 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100912
913 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
914 req->PreviousSessionId = sess_data->previous_session;
915
916 req->Flags = 0; /* MBZ */
917 /* to enable echos and oplocks */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100918 req->sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100919
920 /* only one of SMB2 signing flags may be set in SMB2 request */
921 if (server->sign)
922 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
923 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
924 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
925 else
926 req->SecurityMode = 0;
927
928 req->Capabilities = 0;
929 req->Channel = 0; /* MBZ */
930
931 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100932 /* 1 for pad */
933 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100934 /*
935 * This variable will be used to clear the buffer
936 * allocated above in case of any error in the calling function.
937 */
938 sess_data->buf0_type = CIFS_SMALL_BUFFER;
939
940 return 0;
941}
942
943static void
944SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
945{
946 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
947 sess_data->buf0_type = CIFS_NO_BUFFER;
948}
949
950static int
951SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
952{
953 int rc;
954 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700955 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100956
957 /* Testing shows that buffer offset must be at location of Buffer[0] */
958 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100959 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100960 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
961
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100962 /* BB add code to build os and lm fields */
963
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100964 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
965 sess_data->iov, 2,
966 &sess_data->buf0_type,
967 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700968 cifs_small_buf_release(sess_data->iov[0].iov_base);
969 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100970
971 return rc;
972}
973
974static int
975SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
976{
977 int rc = 0;
978 struct cifs_ses *ses = sess_data->ses;
979
980 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800981 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100982 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100983 if (rc) {
984 cifs_dbg(FYI,
985 "SMB3 session key generation failed\n");
986 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800987 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100988 }
989 }
990 if (!ses->server->session_estab) {
991 ses->server->sequence_number = 0x2;
992 ses->server->session_estab = true;
993 }
994 mutex_unlock(&ses->server->srv_mutex);
995
996 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
997 spin_lock(&GlobalMid_Lock);
998 ses->status = CifsGood;
999 ses->need_reconnect = false;
1000 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001001 return rc;
1002}
1003
1004#ifdef CONFIG_CIFS_UPCALL
1005static void
1006SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1007{
1008 int rc;
1009 struct cifs_ses *ses = sess_data->ses;
1010 struct cifs_spnego_msg *msg;
1011 struct key *spnego_key = NULL;
1012 struct smb2_sess_setup_rsp *rsp = NULL;
1013
1014 rc = SMB2_sess_alloc_buffer(sess_data);
1015 if (rc)
1016 goto out;
1017
1018 spnego_key = cifs_get_spnego_key(ses);
1019 if (IS_ERR(spnego_key)) {
1020 rc = PTR_ERR(spnego_key);
1021 spnego_key = NULL;
1022 goto out;
1023 }
1024
1025 msg = spnego_key->payload.data[0];
1026 /*
1027 * check version field to make sure that cifs.upcall is
1028 * sending us a response in an expected form
1029 */
1030 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1031 cifs_dbg(VFS,
1032 "bad cifs.upcall version. Expected %d got %d",
1033 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1034 rc = -EKEYREJECTED;
1035 goto out_put_spnego_key;
1036 }
1037
1038 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1039 GFP_KERNEL);
1040 if (!ses->auth_key.response) {
1041 cifs_dbg(VFS,
1042 "Kerberos can't allocate (%u bytes) memory",
1043 msg->sesskey_len);
1044 rc = -ENOMEM;
1045 goto out_put_spnego_key;
1046 }
1047 ses->auth_key.len = msg->sesskey_len;
1048
1049 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1050 sess_data->iov[1].iov_len = msg->secblob_len;
1051
1052 rc = SMB2_sess_sendreceive(sess_data);
1053 if (rc)
1054 goto out_put_spnego_key;
1055
1056 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001057 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001058
1059 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001060
1061 rc = SMB2_sess_establish_session(sess_data);
1062out_put_spnego_key:
1063 key_invalidate(spnego_key);
1064 key_put(spnego_key);
1065out:
1066 sess_data->result = rc;
1067 sess_data->func = NULL;
1068 SMB2_sess_free_buffer(sess_data);
1069}
1070#else
1071static void
1072SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1073{
1074 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1075 sess_data->result = -EOPNOTSUPP;
1076 sess_data->func = NULL;
1077}
1078#endif
1079
Sachin Prabhu166cea42016-10-07 19:11:22 +01001080static void
1081SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1082
1083static void
1084SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1085{
1086 int rc;
1087 struct cifs_ses *ses = sess_data->ses;
1088 struct smb2_sess_setup_rsp *rsp = NULL;
1089 char *ntlmssp_blob = NULL;
1090 bool use_spnego = false; /* else use raw ntlmssp */
1091 u16 blob_length = 0;
1092
1093 /*
1094 * If memory allocation is successful, caller of this function
1095 * frees it.
1096 */
1097 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1098 if (!ses->ntlmssp) {
1099 rc = -ENOMEM;
1100 goto out_err;
1101 }
1102 ses->ntlmssp->sesskey_per_smbsess = true;
1103
1104 rc = SMB2_sess_alloc_buffer(sess_data);
1105 if (rc)
1106 goto out_err;
1107
1108 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1109 GFP_KERNEL);
1110 if (ntlmssp_blob == NULL) {
1111 rc = -ENOMEM;
1112 goto out;
1113 }
1114
1115 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1116 if (use_spnego) {
1117 /* BB eventually need to add this */
1118 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1119 rc = -EOPNOTSUPP;
1120 goto out;
1121 } else {
1122 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1123 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1124 }
1125 sess_data->iov[1].iov_base = ntlmssp_blob;
1126 sess_data->iov[1].iov_len = blob_length;
1127
1128 rc = SMB2_sess_sendreceive(sess_data);
1129 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1130
1131 /* If true, rc here is expected and not an error */
1132 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001133 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001134 rc = 0;
1135
1136 if (rc)
1137 goto out;
1138
Ronnie Sahlberg9fdd2e02018-04-09 18:06:27 +10001139 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - ses->server->vals->header_preamble_size !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001140 le16_to_cpu(rsp->SecurityBufferOffset)) {
1141 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1142 le16_to_cpu(rsp->SecurityBufferOffset));
1143 rc = -EIO;
1144 goto out;
1145 }
1146 rc = decode_ntlmssp_challenge(rsp->Buffer,
1147 le16_to_cpu(rsp->SecurityBufferLength), ses);
1148 if (rc)
1149 goto out;
1150
1151 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1152
1153
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001154 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001155 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001156
1157out:
1158 kfree(ntlmssp_blob);
1159 SMB2_sess_free_buffer(sess_data);
1160 if (!rc) {
1161 sess_data->result = 0;
1162 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1163 return;
1164 }
1165out_err:
1166 kfree(ses->ntlmssp);
1167 ses->ntlmssp = NULL;
1168 sess_data->result = rc;
1169 sess_data->func = NULL;
1170}
1171
1172static void
1173SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1174{
1175 int rc;
1176 struct cifs_ses *ses = sess_data->ses;
1177 struct smb2_sess_setup_req *req;
1178 struct smb2_sess_setup_rsp *rsp = NULL;
1179 unsigned char *ntlmssp_blob = NULL;
1180 bool use_spnego = false; /* else use raw ntlmssp */
1181 u16 blob_length = 0;
1182
1183 rc = SMB2_sess_alloc_buffer(sess_data);
1184 if (rc)
1185 goto out;
1186
1187 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001188 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001189
1190 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1191 sess_data->nls_cp);
1192 if (rc) {
1193 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1194 goto out;
1195 }
1196
1197 if (use_spnego) {
1198 /* BB eventually need to add this */
1199 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1200 rc = -EOPNOTSUPP;
1201 goto out;
1202 }
1203 sess_data->iov[1].iov_base = ntlmssp_blob;
1204 sess_data->iov[1].iov_len = blob_length;
1205
1206 rc = SMB2_sess_sendreceive(sess_data);
1207 if (rc)
1208 goto out;
1209
1210 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1211
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001212 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001213 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001214
1215 rc = SMB2_sess_establish_session(sess_data);
1216out:
1217 kfree(ntlmssp_blob);
1218 SMB2_sess_free_buffer(sess_data);
1219 kfree(ses->ntlmssp);
1220 ses->ntlmssp = NULL;
1221 sess_data->result = rc;
1222 sess_data->func = NULL;
1223}
1224
1225static int
1226SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1227{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301228 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001229
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301230 type = smb2_select_sectype(ses->server, ses->sectype);
1231 cifs_dbg(FYI, "sess setup type %d\n", type);
1232 if (type == Unspecified) {
1233 cifs_dbg(VFS,
1234 "Unable to select appropriate authentication method!");
1235 return -EINVAL;
1236 }
1237
1238 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001239 case Kerberos:
1240 sess_data->func = SMB2_auth_kerberos;
1241 break;
1242 case RawNTLMSSP:
1243 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1244 break;
1245 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301246 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001247 return -EOPNOTSUPP;
1248 }
1249
1250 return 0;
1251}
1252
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001253int
1254SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1255 const struct nls_table *nls_cp)
1256{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001257 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001258 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001259 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001260
Joe Perchesf96637b2013-05-04 22:12:25 -05001261 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001262
Jeff Layton3534b852013-05-24 07:41:01 -04001263 if (!server) {
1264 WARN(1, "%s: server is NULL!\n", __func__);
1265 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001266 }
1267
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001268 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1269 if (!sess_data)
1270 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001271
1272 rc = SMB2_select_sec(ses, sess_data);
1273 if (rc)
1274 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001275 sess_data->xid = xid;
1276 sess_data->ses = ses;
1277 sess_data->buf0_type = CIFS_NO_BUFFER;
1278 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001279
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001280#ifdef CONFIG_CIFS_SMB311
1281 /*
1282 * Initialize the session hash with the server one.
1283 */
1284 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1285 SMB2_PREAUTH_HASH_SIZE);
1286#endif
1287
Sachin Prabhu166cea42016-10-07 19:11:22 +01001288 while (sess_data->func)
1289 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001290
Steve Frenchc721c382017-09-19 18:40:03 -05001291 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1292 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001293 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001294out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001295 kfree(sess_data);
1296 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001297}
1298
1299int
1300SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1301{
1302 struct smb2_logoff_req *req; /* response is also trivial struct */
1303 int rc = 0;
1304 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001305 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001306 unsigned int total_len;
1307 struct kvec iov[1];
1308 struct kvec rsp_iov;
1309 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001310
Joe Perchesf96637b2013-05-04 22:12:25 -05001311 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001312
1313 if (ses && (ses->server))
1314 server = ses->server;
1315 else
1316 return -EIO;
1317
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001318 /* no need to send SMB logoff if uid already closed due to reconnect */
1319 if (ses->need_reconnect)
1320 goto smb2_session_already_dead;
1321
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001322 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001323 if (rc)
1324 return rc;
1325
1326 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001327 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001328
1329 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1330 flags |= CIFS_TRANSFORM_REQ;
1331 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001332 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001333
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001334 flags |= CIFS_NO_RESP;
1335
1336 iov[0].iov_base = (char *)req;
1337 iov[0].iov_len = total_len;
1338
1339 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001340 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001341 /*
1342 * No tcon so can't do
1343 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1344 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001345
1346smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001347 return rc;
1348}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001349
1350static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1351{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001352 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001353}
1354
1355#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1356
Steve Frenchde9f68d2013-11-15 11:26:24 -06001357/* These are similar values to what Windows uses */
1358static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1359{
1360 tcon->max_chunks = 256;
1361 tcon->max_bytes_chunk = 1048576;
1362 tcon->max_bytes_copy = 16777216;
1363}
1364
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001365int
1366SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1367 struct cifs_tcon *tcon, const struct nls_table *cp)
1368{
1369 struct smb2_tree_connect_req *req;
1370 struct smb2_tree_connect_rsp *rsp = NULL;
1371 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001372 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001373 int rc = 0;
1374 int resp_buftype;
1375 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001376 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001377 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001378 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001379
Joe Perchesf96637b2013-05-04 22:12:25 -05001380 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001381
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001382 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001383 return -EIO;
1384
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001385 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1386 if (unc_path == NULL)
1387 return -ENOMEM;
1388
1389 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1390 unc_path_len *= 2;
1391 if (unc_path_len < 2) {
1392 kfree(unc_path);
1393 return -EINVAL;
1394 }
1395
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001396 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001397 tcon->tid = 0;
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001398
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001399 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1400 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001401 if (rc) {
1402 kfree(unc_path);
1403 return rc;
1404 }
1405
Aurelien Aptelb327a712018-01-24 13:46:10 +01001406 if (encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001407 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001408
1409 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001410 /* 1 for pad */
1411 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001412
1413 /* Testing shows that buffer offset must be at location of Buffer[0] */
1414 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001415 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001416 req->PathLength = cpu_to_le16(unc_path_len - 2);
1417 iov[1].iov_base = unc_path;
1418 iov[1].iov_len = unc_path_len;
1419
Steve French6188f282018-03-13 02:29:36 -05001420 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1421 if ((ses->server->dialect == SMB311_PROT_ID) &&
1422 !encryption_required(tcon))
1423 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1424
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001425 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001426 cifs_small_buf_release(req);
1427 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001428
1429 if (rc != 0) {
1430 if (tcon) {
1431 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1432 tcon->need_reconnect = true;
1433 }
1434 goto tcon_error_exit;
1435 }
1436
Christophe JAILLETcd123002017-05-12 17:59:32 +02001437 switch (rsp->ShareType) {
1438 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001439 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001440 break;
1441 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001442 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001443 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001444 break;
1445 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001446 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001447 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001448 break;
1449 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001450 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001451 rc = -EOPNOTSUPP;
1452 goto tcon_error_exit;
1453 }
1454
1455 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001456 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001457 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1458 tcon->tidStatus = CifsGood;
1459 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001460 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001461 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001462
1463 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1464 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001465 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001466
1467 if (tcon->seal &&
1468 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1469 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1470
Steve Frenchde9f68d2013-11-15 11:26:24 -06001471 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001472 if (tcon->ses->server->ops->validate_negotiate)
1473 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001474tcon_exit:
1475 free_rsp_buf(resp_buftype, rsp);
1476 kfree(unc_path);
1477 return rc;
1478
1479tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001480 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001481 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001482 }
1483 goto tcon_exit;
1484}
1485
1486int
1487SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1488{
1489 struct smb2_tree_disconnect_req *req; /* response is trivial */
1490 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001491 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001492 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001493 unsigned int total_len;
1494 struct kvec iov[1];
1495 struct kvec rsp_iov;
1496 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001497
Joe Perchesf96637b2013-05-04 22:12:25 -05001498 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001499
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001500 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001501 return -EIO;
1502
1503 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1504 return 0;
1505
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001506 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1507 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001508 if (rc)
1509 return rc;
1510
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001511 if (encryption_required(tcon))
1512 flags |= CIFS_TRANSFORM_REQ;
1513
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001514 flags |= CIFS_NO_RESP;
1515
1516 iov[0].iov_base = (char *)req;
1517 iov[0].iov_len = total_len;
1518
1519 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001520 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001521 if (rc)
1522 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1523
1524 return rc;
1525}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001526
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001527
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001528static struct create_durable *
1529create_durable_buf(void)
1530{
1531 struct create_durable *buf;
1532
1533 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1534 if (!buf)
1535 return NULL;
1536
1537 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001538 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001539 buf->ccontext.DataLength = cpu_to_le32(16);
1540 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1541 (struct create_durable, Name));
1542 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001543 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001544 buf->Name[0] = 'D';
1545 buf->Name[1] = 'H';
1546 buf->Name[2] = 'n';
1547 buf->Name[3] = 'Q';
1548 return buf;
1549}
1550
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001551static struct create_durable *
1552create_reconnect_durable_buf(struct cifs_fid *fid)
1553{
1554 struct create_durable *buf;
1555
1556 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1557 if (!buf)
1558 return NULL;
1559
1560 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1561 (struct create_durable, Data));
1562 buf->ccontext.DataLength = cpu_to_le32(16);
1563 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1564 (struct create_durable, Name));
1565 buf->ccontext.NameLength = cpu_to_le16(4);
1566 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1567 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001568 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001569 buf->Name[0] = 'D';
1570 buf->Name[1] = 'H';
1571 buf->Name[2] = 'n';
1572 buf->Name[3] = 'C';
1573 return buf;
1574}
1575
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001576static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001577parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1578 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001579{
1580 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001581 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001582 unsigned int next;
1583 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001584 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001585
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001586 data_offset = (char *)rsp + server->vals->header_preamble_size + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001587 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001588 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001589 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001590 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001591 if (le16_to_cpu(cc->NameLength) == 4 &&
1592 strncmp(name, "RqLs", 4) == 0)
1593 return server->ops->parse_lease_buf(cc, epoch);
1594
1595 next = le32_to_cpu(cc->Next);
1596 if (!next)
1597 break;
1598 remaining -= next;
1599 cc = (struct create_context *)((char *)cc + next);
1600 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001601
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001602 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001603}
1604
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001605static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001606add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1607 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001608{
1609 struct smb2_create_req *req = iov[0].iov_base;
1610 unsigned int num = *num_iovec;
1611
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001612 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001613 if (iov[num].iov_base == NULL)
1614 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001615 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001616 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1617 if (!req->CreateContextsOffset)
1618 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001619 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001620 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001621 le32_add_cpu(&req->CreateContextsLength,
1622 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001623 *num_iovec = num + 1;
1624 return 0;
1625}
1626
Steve Frenchb56eae42015-11-03 09:26:27 -06001627static struct create_durable_v2 *
1628create_durable_v2_buf(struct cifs_fid *pfid)
1629{
1630 struct create_durable_v2 *buf;
1631
1632 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1633 if (!buf)
1634 return NULL;
1635
1636 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1637 (struct create_durable_v2, dcontext));
1638 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1639 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1640 (struct create_durable_v2, Name));
1641 buf->ccontext.NameLength = cpu_to_le16(4);
1642
1643 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1644 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001645 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001646 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1647
1648 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1649 buf->Name[0] = 'D';
1650 buf->Name[1] = 'H';
1651 buf->Name[2] = '2';
1652 buf->Name[3] = 'Q';
1653 return buf;
1654}
1655
1656static struct create_durable_handle_reconnect_v2 *
1657create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1658{
1659 struct create_durable_handle_reconnect_v2 *buf;
1660
1661 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1662 GFP_KERNEL);
1663 if (!buf)
1664 return NULL;
1665
1666 buf->ccontext.DataOffset =
1667 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1668 dcontext));
1669 buf->ccontext.DataLength =
1670 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1671 buf->ccontext.NameOffset =
1672 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1673 Name));
1674 buf->ccontext.NameLength = cpu_to_le16(4);
1675
1676 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1677 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1678 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1679 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1680
1681 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1682 buf->Name[0] = 'D';
1683 buf->Name[1] = 'H';
1684 buf->Name[2] = '2';
1685 buf->Name[3] = 'C';
1686 return buf;
1687}
1688
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001689static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001690add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001691 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001692{
1693 struct smb2_create_req *req = iov[0].iov_base;
1694 unsigned int num = *num_iovec;
1695
Steve Frenchb56eae42015-11-03 09:26:27 -06001696 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1697 if (iov[num].iov_base == NULL)
1698 return -ENOMEM;
1699 iov[num].iov_len = sizeof(struct create_durable_v2);
1700 if (!req->CreateContextsOffset)
1701 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001702 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001703 iov[1].iov_len);
1704 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001705 *num_iovec = num + 1;
1706 return 0;
1707}
1708
1709static int
1710add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1711 struct cifs_open_parms *oparms)
1712{
1713 struct smb2_create_req *req = iov[0].iov_base;
1714 unsigned int num = *num_iovec;
1715
1716 /* indicate that we don't need to relock the file */
1717 oparms->reconnect = false;
1718
1719 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1720 if (iov[num].iov_base == NULL)
1721 return -ENOMEM;
1722 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1723 if (!req->CreateContextsOffset)
1724 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001725 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001726 iov[1].iov_len);
1727 le32_add_cpu(&req->CreateContextsLength,
1728 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001729 *num_iovec = num + 1;
1730 return 0;
1731}
1732
1733static int
1734add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1735 struct cifs_open_parms *oparms, bool use_persistent)
1736{
1737 struct smb2_create_req *req = iov[0].iov_base;
1738 unsigned int num = *num_iovec;
1739
1740 if (use_persistent) {
1741 if (oparms->reconnect)
1742 return add_durable_reconnect_v2_context(iov, num_iovec,
1743 oparms);
1744 else
1745 return add_durable_v2_context(iov, num_iovec, oparms);
1746 }
1747
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001748 if (oparms->reconnect) {
1749 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1750 /* indicate that we don't need to relock the file */
1751 oparms->reconnect = false;
1752 } else
1753 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001754 if (iov[num].iov_base == NULL)
1755 return -ENOMEM;
1756 iov[num].iov_len = sizeof(struct create_durable);
1757 if (!req->CreateContextsOffset)
1758 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001759 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001760 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001761 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001762 *num_iovec = num + 1;
1763 return 0;
1764}
1765
Aurelien Aptelf0712922017-02-22 14:47:17 +01001766static int
1767alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1768 const char *treename, const __le16 *path)
1769{
1770 int treename_len, path_len;
1771 struct nls_table *cp;
1772 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1773
1774 /*
1775 * skip leading "\\"
1776 */
1777 treename_len = strlen(treename);
1778 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1779 return -EINVAL;
1780
1781 treename += 2;
1782 treename_len -= 2;
1783
1784 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1785
1786 /*
1787 * make room for one path separator between the treename and
1788 * path
1789 */
1790 *out_len = treename_len + 1 + path_len;
1791
1792 /*
1793 * final path needs to be null-terminated UTF16 with a
1794 * size aligned to 8
1795 */
1796
1797 *out_size = roundup((*out_len+1)*2, 8);
1798 *out_path = kzalloc(*out_size, GFP_KERNEL);
1799 if (!*out_path)
1800 return -ENOMEM;
1801
1802 cp = load_nls_default();
1803 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1804 UniStrcat(*out_path, sep);
1805 UniStrcat(*out_path, path);
1806 unload_nls(cp);
1807
1808 return 0;
1809}
1810
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001811int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001812SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001813 __u8 *oplock, struct smb2_file_all_info *buf,
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001814 struct kvec *err_iov)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001815{
1816 struct smb2_create_req *req;
1817 struct smb2_create_rsp *rsp;
1818 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001819 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001820 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001821 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001822 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001823 int resp_buftype;
1824 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001825 __le16 *copy_path = NULL;
1826 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001827 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001828 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001829 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001830 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001831 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001832 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001833
Joe Perchesf96637b2013-05-04 22:12:25 -05001834 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001835
1836 if (ses && (ses->server))
1837 server = ses->server;
1838 else
1839 return -EIO;
1840
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001841 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1842
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001843 if (rc)
1844 return rc;
1845
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001846 if (encryption_required(tcon))
1847 flags |= CIFS_TRANSFORM_REQ;
1848
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001849 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001850 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001851 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1852 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001853
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001854 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001855 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001856 /* File attributes ignored on open (used in create though) */
1857 req->FileAttributes = cpu_to_le32(file_attributes);
1858 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001859 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1860 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001861
1862 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001863 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001864 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001865
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001866 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01001867
1868 /* [MS-SMB2] 2.2.13 NameOffset:
1869 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1870 * the SMB2 header, the file name includes a prefix that will
1871 * be processed during DFS name normalization as specified in
1872 * section 3.3.5.9. Otherwise, the file name is relative to
1873 * the share that is identified by the TreeId in the SMB2
1874 * header.
1875 */
1876 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1877 int name_len;
1878
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001879 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001880 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1881 &name_len,
1882 tcon->treeName, path);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001883 if (rc) {
1884 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001885 return rc;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001886 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001887 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001888 uni_path_len = copy_size;
1889 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001890 } else {
1891 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1892 /* MUST set path len (NameLength) to 0 opening root of share */
1893 req->NameLength = cpu_to_le16(uni_path_len - 2);
1894 if (uni_path_len % 8 != 0) {
1895 copy_size = roundup(uni_path_len, 8);
1896 copy_path = kzalloc(copy_size, GFP_KERNEL);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001897 if (!copy_path) {
1898 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001899 return -ENOMEM;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001900 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001901 memcpy((char *)copy_path, (const char *)path,
1902 uni_path_len);
1903 uni_path_len = copy_size;
1904 path = copy_path;
1905 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001906 }
1907
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001908 iov[1].iov_len = uni_path_len;
1909 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001910
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001911 if (!server->oplocks)
1912 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1913
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001914 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001915 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1916 req->RequestedOplockLevel = *oplock;
1917 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001918 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001919 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001920 cifs_small_buf_release(req);
1921 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001922 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001923 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001924 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001925 }
1926
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001927 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1928 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001929 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001930 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001931 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001932 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001933 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001934 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001935
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001936 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001937 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001938 if (rc) {
1939 cifs_small_buf_release(req);
1940 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001941 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001942 return rc;
1943 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001944 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001945 }
1946
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001947 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1948 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001949 cifs_small_buf_release(req);
1950 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001951
1952 if (rc != 0) {
1953 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001954 if (err_iov && rsp) {
1955 *err_iov = rsp_iov;
1956 resp_buftype = CIFS_NO_BUFFER;
1957 rsp = NULL;
1958 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001959 goto creat_exit;
1960 }
1961
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001962 oparms->fid->persistent_fid = rsp->PersistentFileId;
1963 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001964
1965 if (buf) {
1966 memcpy(buf, &rsp->CreationTime, 32);
1967 buf->AllocationSize = rsp->AllocationSize;
1968 buf->EndOfFile = rsp->EndofFile;
1969 buf->Attributes = rsp->FileAttributes;
1970 buf->NumberOfLinks = cpu_to_le32(1);
1971 buf->DeletePending = 0;
1972 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001973
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001974 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001975 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001976 else
1977 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001978creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001979 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001980 kfree(lc_buf);
1981 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001982 free_rsp_buf(resp_buftype, rsp);
1983 return rc;
1984}
1985
Steve French4a72daf2013-06-25 00:20:49 -05001986/*
1987 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1988 */
1989int
1990SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001991 u64 volatile_fid, u32 opcode, bool is_fsctl,
Aurelien Aptel51146622017-02-28 15:08:41 +01001992 char *in_data, u32 indatalen,
1993 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001994{
1995 struct smb2_ioctl_req *req;
1996 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001997 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001998 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001999 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002000 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002001 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002002 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002003 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002004 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002005 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05002006
2007 cifs_dbg(FYI, "SMB2 IOCTL\n");
2008
Steve French3d1a3742014-08-11 21:05:25 -05002009 if (out_data != NULL)
2010 *out_data = NULL;
2011
Steve French4a72daf2013-06-25 00:20:49 -05002012 /* zero out returned data len, in case of error */
2013 if (plen)
2014 *plen = 0;
2015
Steve French8e353102015-03-26 19:47:02 -05002016 if (tcon)
2017 ses = tcon->ses;
2018 else
2019 return -EIO;
2020
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002021 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05002022 return -EIO;
2023
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002024 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002025 if (rc)
2026 return rc;
2027
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002028 if (encryption_required(tcon))
2029 flags |= CIFS_TRANSFORM_REQ;
2030
Steve French4a72daf2013-06-25 00:20:49 -05002031 req->CtlCode = cpu_to_le32(opcode);
2032 req->PersistentFileId = persistent_fid;
2033 req->VolatileFileId = volatile_fid;
2034
2035 if (indatalen) {
2036 req->InputCount = cpu_to_le32(indatalen);
2037 /* do not set InputOffset if no input data */
2038 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002039 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05002040 iov[1].iov_base = in_data;
2041 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002042 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05002043 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002044 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05002045
2046 req->OutputOffset = 0;
2047 req->OutputCount = 0; /* MBZ */
2048
2049 /*
2050 * Could increase MaxOutputResponse, but that would require more
2051 * than one credit. Windows typically sets this smaller, but for some
2052 * ioctls it may be useful to allow server to send more. No point
2053 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05002054 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2055 * (by default, note that it can be overridden to make max larger)
2056 * in responses (except for read responses which can be bigger.
2057 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05002058 */
Steve French7db0a6e2017-05-03 21:12:20 -05002059 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05002060
2061 if (is_fsctl)
2062 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2063 else
2064 req->Flags = 0;
2065
2066 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05002067
Steve French7ff8d452013-10-14 00:44:19 -05002068 /*
2069 * If no input data, the size of ioctl struct in
2070 * protocol spec still includes a 1 byte data buffer,
2071 * but if input data passed to ioctl, we do not
2072 * want to double count this, so we do not send
2073 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002074 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05002075 */
2076
2077 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002078 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05002079 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002080 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05002081
Steve French4587eee2017-10-25 15:58:31 -05002082 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2083 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002084 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002085
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002086 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2087 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002088 cifs_small_buf_release(req);
2089 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002090
Steve French9bf0c9c2013-11-16 18:05:28 -06002091 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002092 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002093 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002094 } else if (rc == -EINVAL) {
2095 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2096 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002097 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002098 goto ioctl_exit;
2099 }
Steve French4a72daf2013-06-25 00:20:49 -05002100 }
2101
2102 /* check if caller wants to look at return data or just return rc */
2103 if ((plen == NULL) || (out_data == NULL))
2104 goto ioctl_exit;
2105
2106 *plen = le32_to_cpu(rsp->OutputCount);
2107
2108 /* We check for obvious errors in the output buffer length and offset */
2109 if (*plen == 0)
2110 goto ioctl_exit; /* server returned no data */
2111 else if (*plen > 0xFF00) {
2112 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2113 *plen = 0;
2114 rc = -EIO;
2115 goto ioctl_exit;
2116 }
2117
2118 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2119 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2120 le32_to_cpu(rsp->OutputOffset));
2121 *plen = 0;
2122 rc = -EIO;
2123 goto ioctl_exit;
2124 }
2125
2126 *out_data = kmalloc(*plen, GFP_KERNEL);
2127 if (*out_data == NULL) {
2128 rc = -ENOMEM;
2129 goto ioctl_exit;
2130 }
2131
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002132 shdr = get_sync_hdr(rsp);
2133 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002134ioctl_exit:
2135 free_rsp_buf(resp_buftype, rsp);
2136 return rc;
2137}
2138
Steve French64a5cfa2013-10-14 15:31:32 -05002139/*
2140 * Individual callers to ioctl worker function follow
2141 */
2142
2143int
2144SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2145 u64 persistent_fid, u64 volatile_fid)
2146{
2147 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002148 struct compress_ioctl fsctl_input;
2149 char *ret_data = NULL;
2150
2151 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002152 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002153
2154 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2155 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2156 (char *)&fsctl_input /* data input */,
2157 2 /* in data len */, &ret_data /* out data */, NULL);
2158
2159 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002160
2161 return rc;
2162}
2163
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002164int
2165SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2166 u64 persistent_fid, u64 volatile_fid)
2167{
2168 struct smb2_close_req *req;
2169 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002170 struct cifs_ses *ses = tcon->ses;
2171 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002172 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002173 int resp_buftype;
2174 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002175 int flags = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002176 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002177
Joe Perchesf96637b2013-05-04 22:12:25 -05002178 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002179
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002180 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002181 return -EIO;
2182
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002183 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002184 if (rc)
2185 return rc;
2186
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002187 if (encryption_required(tcon))
2188 flags |= CIFS_TRANSFORM_REQ;
2189
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002190 req->PersistentFileId = persistent_fid;
2191 req->VolatileFileId = volatile_fid;
2192
2193 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002194 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002195
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002196 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002197 cifs_small_buf_release(req);
2198 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002199
2200 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002201 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002202 goto close_exit;
2203 }
2204
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002205 /* BB FIXME - decode close response, update inode for caching */
2206
2207close_exit:
2208 free_rsp_buf(resp_buftype, rsp);
2209 return rc;
2210}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002211
2212static int
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002213validate_iov(struct TCP_Server_Info *server,
2214 unsigned int offset, unsigned int buffer_length,
2215 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002216{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002217 unsigned int smb_len = iov->iov_len;
2218 char *end_of_smb = smb_len + server->vals->header_preamble_size + (char *)iov->iov_base;
2219 char *begin_of_buf = server->vals->header_preamble_size + offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002220 char *end_of_buf = begin_of_buf + buffer_length;
2221
2222
2223 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002224 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2225 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002226 return -EINVAL;
2227 }
2228
2229 /* check if beyond RFC1001 maximum length */
2230 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002231 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2232 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002233 return -EINVAL;
2234 }
2235
2236 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002237 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002238 return -EINVAL;
2239 }
2240
2241 return 0;
2242}
2243
2244/*
2245 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2246 * Caller must free buffer.
2247 */
2248static int
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002249validate_and_copy_iov(struct TCP_Server_Info *server,
2250 unsigned int offset, unsigned int buffer_length,
2251 struct kvec *iov, unsigned int minbufsize,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002252 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002253{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002254 char *begin_of_buf = server->vals->header_preamble_size + offset + (char *)(iov->iov_base);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002255 int rc;
2256
2257 if (!data)
2258 return -EINVAL;
2259
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002260 rc = validate_iov(server, offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002261 if (rc)
2262 return rc;
2263
2264 memcpy(data, begin_of_buf, buffer_length);
2265
2266 return 0;
2267}
2268
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002269static int
2270query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002271 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2272 u32 additional_info, size_t output_len, size_t min_len, void **data,
2273 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002274{
2275 struct smb2_query_info_req *req;
2276 struct smb2_query_info_rsp *rsp = NULL;
2277 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002278 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002279 int rc = 0;
2280 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002281 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002282 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002283 unsigned int total_len;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002284
Joe Perchesf96637b2013-05-04 22:12:25 -05002285 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002286
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002287 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002288 return -EIO;
2289
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002290 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2291 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002292 if (rc)
2293 return rc;
2294
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002295 if (encryption_required(tcon))
2296 flags |= CIFS_TRANSFORM_REQ;
2297
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002298 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002299 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002300 req->PersistentFileId = persistent_fid;
2301 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002302 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002303
2304 /*
2305 * We do not use the input buffer (do not send extra byte)
2306 */
2307 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002308
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002309 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002310
2311 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002312 /* 1 for Buffer */
2313 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002314
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002315 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002316 cifs_small_buf_release(req);
2317 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002318
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002319 if (rc) {
2320 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2321 goto qinf_exit;
2322 }
2323
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002324 if (dlen) {
2325 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2326 if (!*data) {
2327 *data = kmalloc(*dlen, GFP_KERNEL);
2328 if (!*data) {
2329 cifs_dbg(VFS,
2330 "Error %d allocating memory for acl\n",
2331 rc);
2332 *dlen = 0;
2333 goto qinf_exit;
2334 }
2335 }
2336 }
2337
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002338 rc = validate_and_copy_iov(ses->server,
2339 le16_to_cpu(rsp->OutputBufferOffset),
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002340 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002341 &rsp_iov, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002342
2343qinf_exit:
2344 free_rsp_buf(resp_buftype, rsp);
2345 return rc;
2346}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002347
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002348int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002349 u64 persistent_fid, u64 volatile_fid,
2350 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002351{
2352 return query_info(xid, tcon, persistent_fid, volatile_fid,
2353 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002354 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002355 sizeof(struct smb2_file_full_ea_info),
2356 (void **)&data,
2357 NULL);
2358}
2359
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002360int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2361 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002362{
2363 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002364 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002365 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002366 sizeof(struct smb2_file_all_info), (void **)&data,
2367 NULL);
2368}
2369
2370int
2371SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2372 u64 persistent_fid, u64 volatile_fid,
2373 void **data, u32 *plen)
2374{
2375 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2376 *plen = 0;
2377
2378 return query_info(xid, tcon, persistent_fid, volatile_fid,
2379 0, SMB2_O_INFO_SECURITY, additional_info,
2380 SMB2_MAX_BUFFER_SIZE,
2381 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002382}
2383
2384int
2385SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2386 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2387{
2388 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002389 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002390 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002391 sizeof(struct smb2_file_internal_info),
2392 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002393}
2394
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002395/*
2396 * This is a no-op for now. We're not really interested in the reply, but
2397 * rather in the fact that the server sent one and that server->lstrp
2398 * gets updated.
2399 *
2400 * FIXME: maybe we should consider checking that the reply matches request?
2401 */
2402static void
2403smb2_echo_callback(struct mid_q_entry *mid)
2404{
2405 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002406 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002407 unsigned int credits_received = 1;
2408
2409 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002410 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002411
2412 DeleteMidQEntry(mid);
2413 add_credits(server, credits_received, CIFS_ECHO_OP);
2414}
2415
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002416void smb2_reconnect_server(struct work_struct *work)
2417{
2418 struct TCP_Server_Info *server = container_of(work,
2419 struct TCP_Server_Info, reconnect.work);
2420 struct cifs_ses *ses;
2421 struct cifs_tcon *tcon, *tcon2;
2422 struct list_head tmp_list;
2423 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002424 int rc;
2425 int resched = false;
2426
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002427
2428 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2429 mutex_lock(&server->reconnect_mutex);
2430
2431 INIT_LIST_HEAD(&tmp_list);
2432 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2433
2434 spin_lock(&cifs_tcp_ses_lock);
2435 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2436 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002437 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002438 tcon->tc_count++;
2439 list_add_tail(&tcon->rlist, &tmp_list);
2440 tcon_exist = true;
2441 }
2442 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002443 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2444 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2445 tcon_exist = true;
2446 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002447 }
2448 /*
2449 * Get the reference to server struct to be sure that the last call of
2450 * cifs_put_tcon() in the loop below won't release the server pointer.
2451 */
2452 if (tcon_exist)
2453 server->srv_count++;
2454
2455 spin_unlock(&cifs_tcp_ses_lock);
2456
2457 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002458 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2459 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002460 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002461 else
2462 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002463 list_del_init(&tcon->rlist);
2464 cifs_put_tcon(tcon);
2465 }
2466
2467 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002468 if (resched)
2469 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002470 mutex_unlock(&server->reconnect_mutex);
2471
2472 /* now we can safely release srv struct */
2473 if (tcon_exist)
2474 cifs_put_tcp_session(server, 1);
2475}
2476
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002477int
2478SMB2_echo(struct TCP_Server_Info *server)
2479{
2480 struct smb2_echo_req *req;
2481 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002482 struct kvec iov[2];
2483 struct smb_rqst rqst = { .rq_iov = iov,
2484 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002485 unsigned int total_len;
2486 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002487
Joe Perchesf96637b2013-05-04 22:12:25 -05002488 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002489
Steve French4fcd1812016-06-22 20:12:05 -05002490 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002491 /* No need to send echo on newly established connections */
2492 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2493 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002494 }
2495
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002496 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002497 if (rc)
2498 return rc;
2499
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002500 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002501
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002502 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002503 rfc1002_marker = cpu_to_be32(total_len);
2504 iov[0].iov_base = &rfc1002_marker;
2505 iov[1].iov_len = total_len;
2506 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002507
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002508 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2509 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002510 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002511 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002512
2513 cifs_small_buf_release(req);
2514 return rc;
2515}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002516
2517int
2518SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2519 u64 volatile_fid)
2520{
2521 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002522 struct cifs_ses *ses = tcon->ses;
2523 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002524 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002525 int resp_buftype;
2526 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002527 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002528 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002529
Joe Perchesf96637b2013-05-04 22:12:25 -05002530 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002531
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002532 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002533 return -EIO;
2534
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002535 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002536 if (rc)
2537 return rc;
2538
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002539 if (encryption_required(tcon))
2540 flags |= CIFS_TRANSFORM_REQ;
2541
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002542 req->PersistentFileId = persistent_fid;
2543 req->VolatileFileId = volatile_fid;
2544
2545 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002546 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002547
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002548 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002549 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002550
Steve Frenchdfebe402015-03-27 01:00:06 -05002551 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002552 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2553
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002554 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002555 return rc;
2556}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002557
2558/*
2559 * To form a chain of read requests, any read requests after the first should
2560 * have the end_of_chain boolean set to true.
2561 */
2562static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002563smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002564 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2565 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002566{
2567 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002568 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002569 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002570 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002571
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002572 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2573 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002574 if (rc)
2575 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002576
2577 server = io_parms->tcon->ses->server;
2578 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002579 return -ECONNABORTED;
2580
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002581 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002582 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002583
2584 req->PersistentFileId = io_parms->persistent_fid;
2585 req->VolatileFileId = io_parms->volatile_fid;
2586 req->ReadChannelInfoOffset = 0; /* reserved */
2587 req->ReadChannelInfoLength = 0; /* reserved */
2588 req->Channel = 0; /* reserved */
2589 req->MinimumCount = 0;
2590 req->Length = cpu_to_le32(io_parms->length);
2591 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07002592#ifdef CONFIG_CIFS_SMB_DIRECT
2593 /*
2594 * If we want to do a RDMA write, fill in and append
2595 * smbd_buffer_descriptor_v1 to the end of read request
2596 */
Long Libb4c0412018-04-17 12:17:08 -07002597 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07002598 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002599
Long Libd3dcc62017-11-22 17:38:47 -07002600 struct smbd_buffer_descriptor_v1 *v1;
2601 bool need_invalidate =
2602 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2603
2604 rdata->mr = smbd_register_mr(
2605 server->smbd_conn, rdata->pages,
2606 rdata->nr_pages, rdata->tailsz,
2607 true, need_invalidate);
2608 if (!rdata->mr)
2609 return -ENOBUFS;
2610
2611 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2612 if (need_invalidate)
2613 req->Channel = SMB2_CHANNEL_RDMA_V1;
2614 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002615 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07002616 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002617 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07002618 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002619 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2620 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2621 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07002622
2623 *total_len += sizeof(*v1) - 1;
2624 }
2625#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002626 if (request_type & CHAINED_REQUEST) {
2627 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002628 /* next 8-byte aligned request */
2629 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2630 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002631 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002632 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002633 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002634 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002635 /*
2636 * Related requests use info from previous read request
2637 * in chain.
2638 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002639 shdr->SessionId = 0xFFFFFFFF;
2640 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002641 req->PersistentFileId = 0xFFFFFFFF;
2642 req->VolatileFileId = 0xFFFFFFFF;
2643 }
2644 }
2645 if (remaining_bytes > io_parms->length)
2646 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2647 else
2648 req->RemainingBytes = 0;
2649
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002650 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002651 return rc;
2652}
2653
2654static void
2655smb2_readv_callback(struct mid_q_entry *mid)
2656{
2657 struct cifs_readdata *rdata = mid->callback_data;
2658 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2659 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002660 struct smb2_sync_hdr *shdr =
2661 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002662 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002663 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2664 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002665 .rq_pages = rdata->pages,
2666 .rq_npages = rdata->nr_pages,
2667 .rq_pagesz = rdata->pagesz,
2668 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002669
Joe Perchesf96637b2013-05-04 22:12:25 -05002670 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2671 __func__, mid->mid, mid->mid_state, rdata->result,
2672 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002673
2674 switch (mid->mid_state) {
2675 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002676 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002677 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002678 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002679 int rc;
2680
Jeff Layton0b688cf2012-09-18 16:20:34 -07002681 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002682 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002683 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2684 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002685 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002686 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002687 task_io_account_read(rdata->got_bytes);
2688 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002689 break;
2690 case MID_REQUEST_SUBMITTED:
2691 case MID_RETRY_NEEDED:
2692 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002693 if (server->sign && rdata->got_bytes)
2694 /* reset bytes number since we can not check a sign */
2695 rdata->got_bytes = 0;
2696 /* FIXME: should this be counted toward the initiating task? */
2697 task_io_account_read(rdata->got_bytes);
2698 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002699 break;
2700 default:
2701 if (rdata->result != -ENODATA)
2702 rdata->result = -EIO;
2703 }
Long Libd3dcc62017-11-22 17:38:47 -07002704#ifdef CONFIG_CIFS_SMB_DIRECT
2705 /*
2706 * If this rdata has a memmory registered, the MR can be freed
2707 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2708 * because they have limited number and are used for future I/Os
2709 */
2710 if (rdata->mr) {
2711 smbd_deregister_mr(rdata->mr);
2712 rdata->mr = NULL;
2713 }
2714#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002715 if (rdata->result)
2716 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2717
2718 queue_work(cifsiod_wq, &rdata->work);
2719 DeleteMidQEntry(mid);
2720 add_credits(server, credits_received, 0);
2721}
2722
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002723/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002724int
2725smb2_async_readv(struct cifs_readdata *rdata)
2726{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002727 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002728 char *buf;
2729 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002730 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002731 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2732 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002733 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002734 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002735 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002736
Joe Perchesf96637b2013-05-04 22:12:25 -05002737 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2738 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002739
2740 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2741 io_parms.offset = rdata->offset;
2742 io_parms.length = rdata->bytes;
2743 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2744 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2745 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002746
2747 server = io_parms.tcon->ses->server;
2748
Long Li2dabfd52017-11-07 01:54:53 -07002749 rc = smb2_new_read_req(
2750 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002751 if (rc) {
2752 if (rc == -EAGAIN && rdata->credits) {
2753 /* credits was reset by reconnect */
2754 rdata->credits = 0;
2755 /* reduce in_flight value since we won't send the req */
2756 spin_lock(&server->req_lock);
2757 server->in_flight--;
2758 spin_unlock(&server->req_lock);
2759 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002760 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002761 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002762
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002763 if (encryption_required(io_parms.tcon))
2764 flags |= CIFS_TRANSFORM_REQ;
2765
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002766 req_len = cpu_to_be32(total_len);
2767
2768 rdata->iov[0].iov_base = &req_len;
2769 rdata->iov[0].iov_len = sizeof(__be32);
2770 rdata->iov[1].iov_base = buf;
2771 rdata->iov[1].iov_len = total_len;
2772
2773 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002774
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002775 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002776 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002777 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002778 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002779 spin_lock(&server->req_lock);
2780 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002781 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002782 spin_unlock(&server->req_lock);
2783 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002784 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002785 }
2786
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002787 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002788 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002789 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002790 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002791 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002792 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002793 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2794 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002795
2796 cifs_small_buf_release(buf);
2797 return rc;
2798}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002799
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002800int
2801SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2802 unsigned int *nbytes, char **buf, int *buf_type)
2803{
2804 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002805 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002806 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002807 struct smb2_sync_hdr *shdr;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002808 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002809 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002810 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002811 int flags = CIFS_LOG_ERROR;
2812 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002813
2814 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07002815 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002816 if (rc)
2817 return rc;
2818
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002819 if (encryption_required(io_parms->tcon))
2820 flags |= CIFS_TRANSFORM_REQ;
2821
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002822 iov[0].iov_base = (char *)req;
2823 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002824
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002825 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002826 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002827
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002828 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002829
2830 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002831 if (rc != -ENODATA) {
2832 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2833 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002834 }
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002835 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2836 return rc == -ENODATA ? 0 : rc;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002837 }
2838
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002839 *nbytes = le32_to_cpu(rsp->DataLength);
2840 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2841 (*nbytes > io_parms->length)) {
2842 cifs_dbg(FYI, "bad length %d for count %d\n",
2843 *nbytes, io_parms->length);
2844 rc = -EIO;
2845 *nbytes = 0;
2846 }
2847
2848 shdr = get_sync_hdr(rsp);
2849
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002850 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002851 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002852 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002853 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002854 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002855 if (resp_buftype == CIFS_SMALL_BUFFER)
2856 *buf_type = CIFS_SMALL_BUFFER;
2857 else if (resp_buftype == CIFS_LARGE_BUFFER)
2858 *buf_type = CIFS_LARGE_BUFFER;
2859 }
2860 return rc;
2861}
2862
Pavel Shilovsky33319142012-09-18 16:20:29 -07002863/*
2864 * Check the mid_state and signature on received buffer (if any), and queue the
2865 * workqueue completion task.
2866 */
2867static void
2868smb2_writev_callback(struct mid_q_entry *mid)
2869{
2870 struct cifs_writedata *wdata = mid->callback_data;
2871 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2872 unsigned int written;
2873 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2874 unsigned int credits_received = 1;
2875
2876 switch (mid->mid_state) {
2877 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002878 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002879 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2880 if (wdata->result != 0)
2881 break;
2882
2883 written = le32_to_cpu(rsp->DataLength);
2884 /*
2885 * Mask off high 16 bits when bytes written as returned
2886 * by the server is greater than bytes requested by the
2887 * client. OS/2 servers are known to set incorrect
2888 * CountHigh values.
2889 */
2890 if (written > wdata->bytes)
2891 written &= 0xFFFF;
2892
2893 if (written < wdata->bytes)
2894 wdata->result = -ENOSPC;
2895 else
2896 wdata->bytes = written;
2897 break;
2898 case MID_REQUEST_SUBMITTED:
2899 case MID_RETRY_NEEDED:
2900 wdata->result = -EAGAIN;
2901 break;
2902 default:
2903 wdata->result = -EIO;
2904 break;
2905 }
Long Lidb223a52017-11-22 17:38:45 -07002906#ifdef CONFIG_CIFS_SMB_DIRECT
2907 /*
2908 * If this wdata has a memory registered, the MR can be freed
2909 * The number of MRs available is limited, it's important to recover
2910 * used MR as soon as I/O is finished. Hold MR longer in the later
2911 * I/O process can possibly result in I/O deadlock due to lack of MR
2912 * to send request on I/O retry
2913 */
2914 if (wdata->mr) {
2915 smbd_deregister_mr(wdata->mr);
2916 wdata->mr = NULL;
2917 }
2918#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002919 if (wdata->result)
2920 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2921
2922 queue_work(cifsiod_wq, &wdata->work);
2923 DeleteMidQEntry(mid);
2924 add_credits(tcon->ses->server, credits_received, 0);
2925}
2926
2927/* smb2_async_writev - send an async write, and set up mid to handle result */
2928int
Steve French4a5c80d2014-02-07 20:45:12 -06002929smb2_async_writev(struct cifs_writedata *wdata,
2930 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002931{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002932 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002933 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002934 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002935 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002936 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002937 struct kvec iov[2];
2938 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002939 unsigned int total_len;
2940 __be32 rfc1002_marker;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002941
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002942 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002943 if (rc) {
2944 if (rc == -EAGAIN && wdata->credits) {
2945 /* credits was reset by reconnect */
2946 wdata->credits = 0;
2947 /* reduce in_flight value since we won't send the req */
2948 spin_lock(&server->req_lock);
2949 server->in_flight--;
2950 spin_unlock(&server->req_lock);
2951 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002952 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002953 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002954
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002955 if (encryption_required(tcon))
2956 flags |= CIFS_TRANSFORM_REQ;
2957
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002958 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002959 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002960
2961 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2962 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2963 req->WriteChannelInfoOffset = 0;
2964 req->WriteChannelInfoLength = 0;
2965 req->Channel = 0;
2966 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002967 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002968 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07002969 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07002970#ifdef CONFIG_CIFS_SMB_DIRECT
2971 /*
2972 * If we want to do a server RDMA read, fill in and append
2973 * smbd_buffer_descriptor_v1 to the end of write request
2974 */
Long Libb4c0412018-04-17 12:17:08 -07002975 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07002976 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07002977
Long Lidb223a52017-11-22 17:38:45 -07002978 struct smbd_buffer_descriptor_v1 *v1;
2979 bool need_invalidate = server->dialect == SMB30_PROT_ID;
2980
2981 wdata->mr = smbd_register_mr(
2982 server->smbd_conn, wdata->pages,
2983 wdata->nr_pages, wdata->tailsz,
2984 false, need_invalidate);
2985 if (!wdata->mr) {
2986 rc = -ENOBUFS;
2987 goto async_writev_out;
2988 }
2989 req->Length = 0;
2990 req->DataOffset = 0;
2991 req->RemainingBytes =
Steve French2026b062018-01-24 23:07:41 -06002992 cpu_to_le32((wdata->nr_pages-1)*PAGE_SIZE + wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07002993 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2994 if (need_invalidate)
2995 req->Channel = SMB2_CHANNEL_RDMA_V1;
2996 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002997 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07002998 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002999 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003000 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003001 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3002 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3003 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003004 }
3005#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003006 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003007 iov[0].iov_len = 4;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003008 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
3009 iov[0].iov_base = &rfc1002_marker;
3010 iov[1].iov_len = total_len - 1;
3011 iov[1].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003012
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003013 rqst.rq_iov = iov;
3014 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003015 rqst.rq_pages = wdata->pages;
3016 rqst.rq_npages = wdata->nr_pages;
3017 rqst.rq_pagesz = wdata->pagesz;
3018 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003019#ifdef CONFIG_CIFS_SMB_DIRECT
3020 if (wdata->mr) {
3021 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3022 rqst.rq_npages = 0;
3023 }
3024#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003025 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3026 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003027
Long Lidb223a52017-11-22 17:38:45 -07003028#ifdef CONFIG_CIFS_SMB_DIRECT
3029 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3030 if (!wdata->mr)
3031 req->Length = cpu_to_le32(wdata->bytes);
3032#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003033 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003034#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003035
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003036 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003037 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003038 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003039 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003040 spin_lock(&server->req_lock);
3041 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003042 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003043 spin_unlock(&server->req_lock);
3044 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003045 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003046 }
3047
Pavel Shilovsky33319142012-09-18 16:20:29 -07003048 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003049 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3050 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003051
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003052 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06003053 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003054 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3055 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003056
Pavel Shilovsky33319142012-09-18 16:20:29 -07003057async_writev_out:
3058 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003059 return rc;
3060}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003061
3062/*
3063 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3064 * The length field from io_parms must be at least 1 and indicates a number of
3065 * elements with data to write that begins with position 1 in iov array. All
3066 * data length is specified by count.
3067 */
3068int
3069SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3070 unsigned int *nbytes, struct kvec *iov, int n_vec)
3071{
3072 int rc = 0;
3073 struct smb2_write_req *req = NULL;
3074 struct smb2_write_rsp *rsp = NULL;
3075 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003076 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003077 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003078 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003079
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003080 *nbytes = 0;
3081
3082 if (n_vec < 1)
3083 return rc;
3084
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003085 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3086 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003087 if (rc)
3088 return rc;
3089
3090 if (io_parms->tcon->ses->server == NULL)
3091 return -ECONNABORTED;
3092
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003093 if (encryption_required(io_parms->tcon))
3094 flags |= CIFS_TRANSFORM_REQ;
3095
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003096 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003097
3098 req->PersistentFileId = io_parms->persistent_fid;
3099 req->VolatileFileId = io_parms->volatile_fid;
3100 req->WriteChannelInfoOffset = 0;
3101 req->WriteChannelInfoLength = 0;
3102 req->Channel = 0;
3103 req->Length = cpu_to_le32(io_parms->length);
3104 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003105 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003106 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003107 req->RemainingBytes = 0;
3108
3109 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003110 /* 1 for Buffer */
3111 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003112
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003113 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
3114 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003115 cifs_small_buf_release(req);
3116 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003117
3118 if (rc) {
3119 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003120 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003121 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003122 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003123
3124 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003125 return rc;
3126}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003127
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003128static unsigned int
3129num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3130{
3131 int len;
3132 unsigned int entrycount = 0;
3133 unsigned int next_offset = 0;
3134 FILE_DIRECTORY_INFO *entryptr;
3135
3136 if (bufstart == NULL)
3137 return 0;
3138
3139 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3140
3141 while (1) {
3142 entryptr = (FILE_DIRECTORY_INFO *)
3143 ((char *)entryptr + next_offset);
3144
3145 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003146 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003147 break;
3148 }
3149
3150 len = le32_to_cpu(entryptr->FileNameLength);
3151 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003152 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3153 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003154 break;
3155 }
3156
3157 *lastentry = (char *)entryptr;
3158 entrycount++;
3159
3160 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3161 if (!next_offset)
3162 break;
3163 }
3164
3165 return entrycount;
3166}
3167
3168/*
3169 * Readdir/FindFirst
3170 */
3171int
3172SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3173 u64 persistent_fid, u64 volatile_fid, int index,
3174 struct cifs_search_info *srch_inf)
3175{
3176 struct smb2_query_directory_req *req;
3177 struct smb2_query_directory_rsp *rsp = NULL;
3178 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003179 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003180 int rc = 0;
3181 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003182 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003183 unsigned char *bufptr;
3184 struct TCP_Server_Info *server;
3185 struct cifs_ses *ses = tcon->ses;
3186 __le16 asteriks = cpu_to_le16('*');
3187 char *end_of_smb;
3188 unsigned int output_size = CIFSMaxBufSize;
3189 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003190 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003191 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003192
3193 if (ses && (ses->server))
3194 server = ses->server;
3195 else
3196 return -EIO;
3197
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003198 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3199 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003200 if (rc)
3201 return rc;
3202
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003203 if (encryption_required(tcon))
3204 flags |= CIFS_TRANSFORM_REQ;
3205
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003206 switch (srch_inf->info_level) {
3207 case SMB_FIND_FILE_DIRECTORY_INFO:
3208 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3209 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3210 break;
3211 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3212 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3213 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3214 break;
3215 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003216 cifs_dbg(VFS, "info level %u isn't supported\n",
3217 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003218 rc = -EINVAL;
3219 goto qdir_exit;
3220 }
3221
3222 req->FileIndex = cpu_to_le32(index);
3223 req->PersistentFileId = persistent_fid;
3224 req->VolatileFileId = volatile_fid;
3225
3226 len = 0x2;
3227 bufptr = req->Buffer;
3228 memcpy(bufptr, &asteriks, len);
3229
3230 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003231 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003232 req->FileNameLength = cpu_to_le16(len);
3233 /*
3234 * BB could be 30 bytes or so longer if we used SMB2 specific
3235 * buffer lengths, but this is safe and close enough.
3236 */
3237 output_size = min_t(unsigned int, output_size, server->maxBuf);
3238 output_size = min_t(unsigned int, output_size, 2 << 15);
3239 req->OutputBufferLength = cpu_to_le32(output_size);
3240
3241 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003242 /* 1 for Buffer */
3243 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003244
3245 iov[1].iov_base = (char *)(req->Buffer);
3246 iov[1].iov_len = len;
3247
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003248 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003249 cifs_small_buf_release(req);
3250 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003251
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003252 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003253 if (rc == -ENODATA &&
3254 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003255 srch_inf->endOfSearch = true;
3256 rc = 0;
3257 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003258 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3259 goto qdir_exit;
3260 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003261
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003262 rc = validate_iov(server,
3263 le16_to_cpu(rsp->OutputBufferOffset),
3264 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003265 info_buf_size);
3266 if (rc)
3267 goto qdir_exit;
3268
3269 srch_inf->unicode = true;
3270
3271 if (srch_inf->ntwrk_buf_start) {
3272 if (srch_inf->smallBuf)
3273 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3274 else
3275 cifs_buf_release(srch_inf->ntwrk_buf_start);
3276 }
3277 srch_inf->ntwrk_buf_start = (char *)rsp;
3278 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3279 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3280 /* 4 for rfc1002 length field */
3281 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3282 srch_inf->entries_in_buffer =
3283 num_entries(srch_inf->srch_entries_start, end_of_smb,
3284 &srch_inf->last_entry, info_buf_size);
3285 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003286 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3287 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3288 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003289 if (resp_buftype == CIFS_LARGE_BUFFER)
3290 srch_inf->smallBuf = false;
3291 else if (resp_buftype == CIFS_SMALL_BUFFER)
3292 srch_inf->smallBuf = true;
3293 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003294 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003295
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003296 return rc;
3297
3298qdir_exit:
3299 free_rsp_buf(resp_buftype, rsp);
3300 return rc;
3301}
3302
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003303static int
3304send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003305 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3306 u8 info_type, u32 additional_info, unsigned int num,
3307 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003308{
3309 struct smb2_set_info_req *req;
3310 struct smb2_set_info_rsp *rsp = NULL;
3311 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003312 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003313 int rc = 0;
3314 int resp_buftype;
3315 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003316 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003317 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003318 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003319
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003320 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003321 return -EIO;
3322
3323 if (!num)
3324 return -EINVAL;
3325
3326 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3327 if (!iov)
3328 return -ENOMEM;
3329
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003330 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003331 if (rc) {
3332 kfree(iov);
3333 return rc;
3334 }
3335
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003336 if (encryption_required(tcon))
3337 flags |= CIFS_TRANSFORM_REQ;
3338
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003339 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003340
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003341 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003342 req->FileInfoClass = info_class;
3343 req->PersistentFileId = persistent_fid;
3344 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003345 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003346
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003347 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003348 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003349 req->BufferLength = cpu_to_le32(*size);
3350
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003351 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003352 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003353
3354 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003355 /* 1 for Buffer */
3356 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003357
3358 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003359 le32_add_cpu(&req->BufferLength, size[i]);
3360 iov[i].iov_base = (char *)data[i];
3361 iov[i].iov_len = size[i];
3362 }
3363
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003364 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3365 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003366 cifs_small_buf_release(req);
3367 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003368
Steve French7d3fb242013-11-18 09:56:28 -06003369 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003370 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003371
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003372 free_rsp_buf(resp_buftype, rsp);
3373 kfree(iov);
3374 return rc;
3375}
3376
3377int
3378SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3379 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3380{
3381 struct smb2_file_rename_info info;
3382 void **data;
3383 unsigned int size[2];
3384 int rc;
3385 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3386
3387 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3388 if (!data)
3389 return -ENOMEM;
3390
3391 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3392 /* 0 = fail if target already exists */
3393 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3394 info.FileNameLength = cpu_to_le32(len);
3395
3396 data[0] = &info;
3397 size[0] = sizeof(struct smb2_file_rename_info);
3398
3399 data[1] = target_file;
3400 size[1] = len + 2 /* null */;
3401
3402 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003403 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3404 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003405 kfree(data);
3406 return rc;
3407}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003408
3409int
Steve French897fba12016-05-12 21:20:36 -05003410SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3411 u64 persistent_fid, u64 volatile_fid)
3412{
3413 __u8 delete_pending = 1;
3414 void *data;
3415 unsigned int size;
3416
3417 data = &delete_pending;
3418 size = 1; /* sizeof __u8 */
3419
3420 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003421 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3422 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003423}
3424
3425int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003426SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3427 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3428{
3429 struct smb2_file_link_info info;
3430 void **data;
3431 unsigned int size[2];
3432 int rc;
3433 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3434
3435 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3436 if (!data)
3437 return -ENOMEM;
3438
3439 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3440 /* 0 = fail if link already exists */
3441 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3442 info.FileNameLength = cpu_to_le32(len);
3443
3444 data[0] = &info;
3445 size[0] = sizeof(struct smb2_file_link_info);
3446
3447 data[1] = target_file;
3448 size[1] = len + 2 /* null */;
3449
3450 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003451 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3452 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003453 kfree(data);
3454 return rc;
3455}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003456
3457int
3458SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003459 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003460{
3461 struct smb2_file_eof_info info;
3462 void *data;
3463 unsigned int size;
3464
3465 info.EndOfFile = *eof;
3466
3467 data = &info;
3468 size = sizeof(struct smb2_file_eof_info);
3469
Steve Frenchf29ebb42014-07-19 21:44:58 -05003470 if (is_falloc)
3471 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003472 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3473 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003474 else
3475 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003476 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3477 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003478}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003479
3480int
3481SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3482 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3483{
3484 unsigned int size;
3485 size = sizeof(FILE_BASIC_INFO);
3486 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003487 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3488 0, 1, (void **)&buf, &size);
3489}
3490
3491int
3492SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3493 u64 persistent_fid, u64 volatile_fid,
3494 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3495{
3496 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3497 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3498 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003499}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003500
3501int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003502SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3503 u64 persistent_fid, u64 volatile_fid,
3504 struct smb2_file_full_ea_info *buf, int len)
3505{
3506 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3507 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3508 0, 1, (void **)&buf, &len);
3509}
3510
3511int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003512SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3513 const u64 persistent_fid, const u64 volatile_fid,
3514 __u8 oplock_level)
3515{
3516 int rc;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003517 struct smb2_oplock_break_req *req = NULL;
3518 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003519 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003520 unsigned int total_len;
3521 struct kvec iov[1];
3522 struct kvec rsp_iov;
3523 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003524
Joe Perchesf96637b2013-05-04 22:12:25 -05003525 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003526 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3527 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003528 if (rc)
3529 return rc;
3530
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003531 if (encryption_required(tcon))
3532 flags |= CIFS_TRANSFORM_REQ;
3533
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003534 req->VolatileFid = volatile_fid;
3535 req->PersistentFid = persistent_fid;
3536 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003537 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003538
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003539 flags |= CIFS_NO_RESP;
3540
3541 iov[0].iov_base = (char *)req;
3542 iov[0].iov_len = total_len;
3543
3544 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003545 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003546
3547 if (rc) {
3548 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003549 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003550 }
3551
3552 return rc;
3553}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003554
3555static void
3556copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3557 struct kstatfs *kst)
3558{
3559 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3560 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3561 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303562 kst->f_bfree = kst->f_bavail =
3563 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003564 return;
3565}
3566
3567static int
3568build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3569 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3570{
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003571 struct TCP_Server_Info *server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003572 int rc;
3573 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003574 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003575
Joe Perchesf96637b2013-05-04 22:12:25 -05003576 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003577
3578 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3579 return -EIO;
3580
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003581 server = tcon->ses->server;
3582
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003583 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3584 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003585 if (rc)
3586 return rc;
3587
3588 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3589 req->FileInfoClass = level;
3590 req->PersistentFileId = persistent_fid;
3591 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003592 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003593 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003594 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003595 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003596 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - server->vals->header_preamble_size);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003597
3598 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003599 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003600 return 0;
3601}
3602
3603int
3604SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3605 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3606{
3607 struct smb2_query_info_rsp *rsp = NULL;
3608 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003609 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003610 int rc = 0;
3611 int resp_buftype;
3612 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003613 struct TCP_Server_Info *server = ses->server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003614 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003615 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003616
3617 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3618 sizeof(struct smb2_fs_full_size_info),
3619 persistent_fid, volatile_fid);
3620 if (rc)
3621 return rc;
3622
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003623 if (encryption_required(tcon))
3624 flags |= CIFS_TRANSFORM_REQ;
3625
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003626 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003627 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003628 if (rc) {
3629 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003630 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003631 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003632 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003633
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003634 info = (struct smb2_fs_full_size_info *)(server->vals->header_preamble_size +
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003635 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003636 rc = validate_iov(server,
3637 le16_to_cpu(rsp->OutputBufferOffset),
3638 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003639 sizeof(struct smb2_fs_full_size_info));
3640 if (!rc)
3641 copy_fs_info_to_kstatfs(info, fsdata);
3642
Steve French34f62642013-10-09 02:07:00 -05003643qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003644 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003645 return rc;
3646}
3647
3648int
3649SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003650 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003651{
3652 struct smb2_query_info_rsp *rsp = NULL;
3653 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003654 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003655 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003656 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003657 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003658 struct TCP_Server_Info *server = ses->server;
Steve French34f62642013-10-09 02:07:00 -05003659 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003660 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003661
Steven French21671142013-10-09 13:36:35 -05003662 if (level == FS_DEVICE_INFORMATION) {
3663 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3664 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3665 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3666 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3667 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003668 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3669 max_len = sizeof(struct smb3_fs_ss_info);
3670 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003671 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003672 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003673 return -EINVAL;
3674 }
3675
3676 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003677 persistent_fid, volatile_fid);
3678 if (rc)
3679 return rc;
3680
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003681 if (encryption_required(tcon))
3682 flags |= CIFS_TRANSFORM_REQ;
3683
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003684 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003685 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003686 if (rc) {
3687 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3688 goto qfsattr_exit;
3689 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003690 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003691
3692 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3693 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003694 rc = validate_iov(server, offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05003695 if (rc)
3696 goto qfsattr_exit;
3697
3698 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003699 memcpy(&tcon->fsAttrInfo, server->vals->header_preamble_size + offset
Steve French34f62642013-10-09 02:07:00 -05003700 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003701 rsp_len, max_len));
3702 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003703 memcpy(&tcon->fsDevInfo, server->vals->header_preamble_size + offset
Steven French21671142013-10-09 13:36:35 -05003704 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003705 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3706 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003707 (server->vals->header_preamble_size + offset + (char *)&rsp->hdr);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003708 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3709 tcon->perf_sector_size =
3710 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3711 }
Steve French34f62642013-10-09 02:07:00 -05003712
3713qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003714 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003715 return rc;
3716}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003717
3718int
3719smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3720 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3721 const __u32 num_lock, struct smb2_lock_element *buf)
3722{
3723 int rc = 0;
3724 struct smb2_lock_req *req = NULL;
3725 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003726 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003727 int resp_buf_type;
3728 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003729 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003730 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003731
Joe Perchesf96637b2013-05-04 22:12:25 -05003732 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003733
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003734 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003735 if (rc)
3736 return rc;
3737
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003738 if (encryption_required(tcon))
3739 flags |= CIFS_TRANSFORM_REQ;
3740
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003741 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003742 req->LockCount = cpu_to_le16(num_lock);
3743
3744 req->PersistentFileId = persist_fid;
3745 req->VolatileFileId = volatile_fid;
3746
3747 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003748
3749 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003750 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003751 iov[1].iov_base = (char *)buf;
3752 iov[1].iov_len = count;
3753
3754 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003755 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3756 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003757 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003758 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003759 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003760 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3761 }
3762
3763 return rc;
3764}
3765
3766int
3767SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3768 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3769 const __u64 length, const __u64 offset, const __u32 lock_flags,
3770 const bool wait)
3771{
3772 struct smb2_lock_element lock;
3773
3774 lock.Offset = cpu_to_le64(offset);
3775 lock.Length = cpu_to_le64(length);
3776 lock.Flags = cpu_to_le32(lock_flags);
3777 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3778 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3779
3780 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3781}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003782
3783int
3784SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3785 __u8 *lease_key, const __le32 lease_state)
3786{
3787 int rc;
3788 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003789 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003790 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003791 unsigned int total_len;
3792 struct kvec iov[1];
3793 struct kvec rsp_iov;
3794 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003795
Joe Perchesf96637b2013-05-04 22:12:25 -05003796 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003797 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3798 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003799 if (rc)
3800 return rc;
3801
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003802 if (encryption_required(tcon))
3803 flags |= CIFS_TRANSFORM_REQ;
3804
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003805 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003806 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003807 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003808
3809 memcpy(req->LeaseKey, lease_key, 16);
3810 req->LeaseState = lease_state;
3811
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003812 flags |= CIFS_NO_RESP;
3813
3814 iov[0].iov_base = (char *)req;
3815 iov[0].iov_len = total_len;
3816
3817 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003818 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003819
3820 if (rc) {
3821 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003822 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003823 }
3824
3825 return rc;
3826}