blob: 47d53314fc7f1c22e90f89ff9806416a02b7c430 [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"
Steve Frencheccb4422018-05-17 21:16:55 -050052#include "trace.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040053
54/*
55 * The following table defines the expected "StructureSize" of SMB2 requests
56 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
57 *
58 * Note that commands are defined in smb2pdu.h in le16 but the array below is
59 * indexed by command in host byte order.
60 */
61static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
62 /* SMB2_NEGOTIATE */ 36,
63 /* SMB2_SESSION_SETUP */ 25,
64 /* SMB2_LOGOFF */ 4,
65 /* SMB2_TREE_CONNECT */ 9,
66 /* SMB2_TREE_DISCONNECT */ 4,
67 /* SMB2_CREATE */ 57,
68 /* SMB2_CLOSE */ 24,
69 /* SMB2_FLUSH */ 24,
70 /* SMB2_READ */ 49,
71 /* SMB2_WRITE */ 49,
72 /* SMB2_LOCK */ 48,
73 /* SMB2_IOCTL */ 57,
74 /* SMB2_CANCEL */ 4,
75 /* SMB2_ECHO */ 4,
76 /* SMB2_QUERY_DIRECTORY */ 33,
77 /* SMB2_CHANGE_NOTIFY */ 32,
78 /* SMB2_QUERY_INFO */ 41,
79 /* SMB2_SET_INFO */ 33,
80 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
81};
82
Steve French5a77e752018-05-09 17:43:08 -050083static int smb3_encryption_required(const struct cifs_tcon *tcon)
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070084{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080085 if (!tcon)
86 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070087 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
88 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
89 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080090 if (tcon->seal &&
91 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
92 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070093 return 0;
94}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040095
96static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070097smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040098 const struct cifs_tcon *tcon)
99{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700100 shdr->ProtocolId = SMB2_PROTO_NUMBER;
101 shdr->StructureSize = cpu_to_le16(64);
102 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100103 if (tcon && tcon->ses && tcon->ses->server) {
104 struct TCP_Server_Info *server = tcon->ses->server;
105
106 spin_lock(&server->req_lock);
107 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500108 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700109 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100110 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700111 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500112 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100113 server->credits, 2));
114 spin_unlock(&server->req_lock);
115 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700116 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100117 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700118 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400119
120 if (!tcon)
121 goto out;
122
Steve French2b80d042013-06-23 18:43:37 -0500123 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
124 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500125 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500126 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700127 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500128 /* else CreditCharge MBZ */
129
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700130 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400131 /* Uid is not converted */
132 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700133 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500134
135 /*
136 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
137 * to pass the path on the Open SMB prefixed by \\server\share.
138 * Not sure when we would need to do the augmented path (if ever) and
139 * setting this flag breaks the SMB2 open operation since it is
140 * illegal to send an empty path name (without \\server\share prefix)
141 * when the DFS flag is set in the SMB open header. We could
142 * consider setting the flag on all operations other than open
143 * but it is safer to net set it for now.
144 */
145/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700146 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500147
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700148 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
Steve French5a77e752018-05-09 17:43:08 -0500149 !smb3_encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700150 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400151out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400152 return;
153}
154
155static int
156smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
157{
158 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400159 struct nls_table *nls_codepage;
160 struct cifs_ses *ses;
161 struct TCP_Server_Info *server;
162
163 /*
164 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
165 * check for tcp and smb session status done differently
166 * for those three - in the calling routine.
167 */
168 if (tcon == NULL)
169 return rc;
170
171 if (smb2_command == SMB2_TREE_CONNECT)
172 return rc;
173
174 if (tcon->tidStatus == CifsExiting) {
175 /*
176 * only tree disconnect, open, and write,
177 * (and ulogoff which does not have tcon)
178 * are allowed as we start force umount.
179 */
180 if ((smb2_command != SMB2_WRITE) &&
181 (smb2_command != SMB2_CREATE) &&
182 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500183 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
184 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400185 return -ENODEV;
186 }
187 }
188 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
189 (!tcon->ses->server))
190 return -EIO;
191
192 ses = tcon->ses;
193 server = ses->server;
194
195 /*
196 * Give demultiplex thread up to 10 seconds to reconnect, should be
197 * greater than cifs socket timeout which is 7 seconds
198 */
199 while (server->tcpStatus == CifsNeedReconnect) {
200 /*
201 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
202 * here since they are implicitly done when session drops.
203 */
204 switch (smb2_command) {
205 /*
206 * BB Should we keep oplock break and add flush to exceptions?
207 */
208 case SMB2_TREE_DISCONNECT:
209 case SMB2_CANCEL:
210 case SMB2_CLOSE:
211 case SMB2_OPLOCK_BREAK:
212 return -EAGAIN;
213 }
214
215 wait_event_interruptible_timeout(server->response_q,
216 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
217
218 /* are we still trying to reconnect? */
219 if (server->tcpStatus != CifsNeedReconnect)
220 break;
221
222 /*
223 * on "soft" mounts we wait once. Hard mounts keep
224 * retrying until process is killed or server comes
225 * back on-line
226 */
227 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500228 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400229 return -EHOSTDOWN;
230 }
231 }
232
233 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
234 return rc;
235
236 nls_codepage = load_nls_default();
237
238 /*
239 * need to prevent multiple threads trying to simultaneously reconnect
240 * the same SMB session
241 */
242 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200243
244 /*
245 * Recheck after acquire mutex. If another thread is negotiating
246 * and the server never sends an answer the socket will be closed
247 * and tcpStatus set to reconnect.
248 */
249 if (server->tcpStatus == CifsNeedReconnect) {
250 rc = -EHOSTDOWN;
251 mutex_unlock(&tcon->ses->session_mutex);
252 goto out;
253 }
254
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400255 rc = cifs_negotiate_protocol(0, tcon->ses);
256 if (!rc && tcon->ses->need_reconnect)
257 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
258
259 if (rc || !tcon->need_reconnect) {
260 mutex_unlock(&tcon->ses->session_mutex);
261 goto out;
262 }
263
264 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800265 if (tcon->use_persistent)
266 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500267
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400268 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
269 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500270
Joe Perchesf96637b2013-05-04 22:12:25 -0500271 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500272 if (rc) {
273 /* If sess reconnected but tcon didn't, something strange ... */
274 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400275 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500276 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800277
278 if (smb2_command != SMB2_INTERNAL_CMD)
279 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
280
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400281 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400282out:
283 /*
284 * Check if handle based operation so we know whether we can continue
285 * or not without returning to caller to reset file handle.
286 */
287 /*
288 * BB Is flush done by server on drop of tcp session? Should we special
289 * case it and skip above?
290 */
291 switch (smb2_command) {
292 case SMB2_FLUSH:
293 case SMB2_READ:
294 case SMB2_WRITE:
295 case SMB2_LOCK:
296 case SMB2_IOCTL:
297 case SMB2_QUERY_DIRECTORY:
298 case SMB2_CHANGE_NOTIFY:
299 case SMB2_QUERY_INFO:
300 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800301 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400302 }
303 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400304 return rc;
305}
306
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700307static void
308fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
309 unsigned int *total_len)
310{
311 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
312 /* lookup word count ie StructureSize from table */
313 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
314
315 /*
316 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
317 * largest operations (Create)
318 */
319 memset(buf, 0, 256);
320
321 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
322 spdu->StructureSize2 = cpu_to_le16(parmsize);
323
324 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
325}
326
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100327/*
328 * Allocate and return pointer to an SMB request hdr, and set basic
329 * SMB information in the SMB header. If the return code is zero, this
330 * function must have filled in request_buf pointer.
331 */
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800332static int
333smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
334 void **request_buf, unsigned int *total_len)
335{
336 int rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800337
338 rc = smb2_reconnect(smb2_command, tcon);
339 if (rc)
340 return rc;
341
342 /* BB eventually switch this to SMB2 specific small buf size */
343 *request_buf = cifs_small_buf_get();
344 if (*request_buf == NULL) {
345 /* BB should we add a retry in here if not a writepage? */
346 return -ENOMEM;
347 }
348
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100349 fill_small_buf(smb2_command, tcon,
350 (struct smb2_sync_hdr *)(*request_buf),
351 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400352
353 if (tcon != NULL) {
354#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400355 uint16_t com_code = le16_to_cpu(smb2_command);
356 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400357#endif
358 cifs_stats_inc(&tcon->num_smbs_sent);
359 }
360
361 return rc;
362}
363
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500364#ifdef CONFIG_CIFS_SMB311
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100365/* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
366#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500367
368
369#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
370#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
371
372static void
373build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
374{
375 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
376 pneg_ctxt->DataLength = cpu_to_le16(38);
377 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
378 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
379 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
380 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
381}
382
383static void
384build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
385{
386 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French23657ad2018-04-22 15:14:58 -0500387 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
388 pneg_ctxt->CipherCount = cpu_to_le16(1);
389/* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
390 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500391}
392
393static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100394assemble_neg_contexts(struct smb2_negotiate_req *req,
395 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500396{
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100397 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500398
399 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
400 /* Add 2 to size to round to 8 byte boundary */
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100401
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500402 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
403 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
404 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
405 req->NegotiateContextCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100406
407 *total_len += 4 + sizeof(struct smb2_preauth_neg_context)
408 + sizeof(struct smb2_encryption_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500409}
Steve French5100d8a2018-04-09 10:47:14 -0500410
411static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
412{
413 unsigned int len = le16_to_cpu(ctxt->DataLength);
414
415 /* If invalid preauth context warn but use what we requested, SHA-512 */
416 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
417 printk_once(KERN_WARNING "server sent bad preauth context\n");
418 return;
419 }
420 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
421 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
422 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
423 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
424}
425
426static int decode_encrypt_ctx(struct TCP_Server_Info *server,
427 struct smb2_encryption_neg_context *ctxt)
428{
429 unsigned int len = le16_to_cpu(ctxt->DataLength);
430
431 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
432 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
433 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
434 return -EINVAL;
435 }
436
437 if (le16_to_cpu(ctxt->CipherCount) != 1) {
438 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
439 return -EINVAL;
440 }
441 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
442 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
443 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
444 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
445 return -EINVAL;
446 }
447 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500448 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500449 return 0;
450}
451
452static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
453 struct TCP_Server_Info *server)
454{
455 struct smb2_neg_context *pctx;
456 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
457 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
458 unsigned int len_of_smb = be32_to_cpu(rsp->hdr.smb2_buf_length);
459 unsigned int len_of_ctxts, i;
460 int rc = 0;
461
462 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
463 if (len_of_smb <= offset) {
464 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
465 return -EINVAL;
466 }
467
468 len_of_ctxts = len_of_smb - offset;
469
470 for (i = 0; i < ctxt_cnt; i++) {
471 int clen;
472 /* check that offset is not beyond end of SMB */
473 if (len_of_ctxts == 0)
474 break;
475
476 if (len_of_ctxts < sizeof(struct smb2_neg_context))
477 break;
478
Steve French0d4b46b2018-04-12 20:32:13 -0500479 pctx = (struct smb2_neg_context *)(offset +
480 server->vals->header_preamble_size + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500481 clen = le16_to_cpu(pctx->DataLength);
482 if (clen > len_of_ctxts)
483 break;
484
485 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
486 decode_preauth_context(
487 (struct smb2_preauth_neg_context *)pctx);
488 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
489 rc = decode_encrypt_ctx(server,
490 (struct smb2_encryption_neg_context *)pctx);
491 else
492 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
493 le16_to_cpu(pctx->ContextType));
494
495 if (rc)
496 break;
497 /* offsets must be 8 byte aligned */
498 clen = (clen + 7) & ~0x7;
499 offset += clen + sizeof(struct smb2_neg_context);
500 len_of_ctxts -= clen;
501 }
502 return rc;
503}
504
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500505#else
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100506static void assemble_neg_contexts(struct smb2_negotiate_req *req,
507 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500508{
509 return;
510}
511#endif /* SMB311 */
512
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400513/*
514 *
515 * SMB2 Worker functions follow:
516 *
517 * The general structure of the worker functions is:
518 * 1) Call smb2_init (assembles SMB2 header)
519 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
520 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
521 * 4) Decode SMB2 command specific fields in the fixed length area
522 * 5) Decode variable length data area (if any for this SMB2 command type)
523 * 6) Call free smb buffer
524 * 7) return
525 *
526 */
527
528int
529SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
530{
531 struct smb2_negotiate_req *req;
532 struct smb2_negotiate_rsp *rsp;
533 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700534 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400535 int rc = 0;
536 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400537 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400538 int blob_offset, blob_length;
539 char *security_blob;
540 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100541 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400542
Joe Perchesf96637b2013-05-04 22:12:25 -0500543 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400544
Jeff Layton3534b852013-05-24 07:41:01 -0400545 if (!server) {
546 WARN(1, "%s: server is NULL!\n", __func__);
547 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400548 }
549
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100550 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400551 if (rc)
552 return rc;
553
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100554 req->sync_hdr.SessionId = 0;
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100555#ifdef CONFIG_CIFS_SMB311
556 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
557 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
558#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400559
Steve French9764c022017-09-17 10:41:35 -0500560 if (strcmp(ses->server->vals->version_string,
561 SMB3ANY_VERSION_STRING) == 0) {
562 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
563 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
564 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100565 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500566 } else if (strcmp(ses->server->vals->version_string,
567 SMBDEFAULT_VERSION_STRING) == 0) {
568 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
569 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
570 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
571 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100572 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500573 } else {
574 /* otherwise send specific dialect */
575 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
576 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100577 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500578 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400579
580 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400581 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500582 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400583 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500584 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400585 else
586 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400587
Steve Frenche4aa25e2012-10-01 12:26:22 -0500588 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400589
Steve French3c5f9be12014-05-13 13:37:45 -0700590 /* ClientGUID must be zero for SMB2.02 dialect */
591 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
592 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500593 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700594 memcpy(req->ClientGUID, server->client_guid,
595 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500596 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100597 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500598 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400599 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100600 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400601
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100602 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700603 cifs_small_buf_release(req);
604 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400605 /*
606 * No tcon so can't do
607 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
608 */
Steve French7e682f72017-08-31 21:34:24 -0500609 if (rc == -EOPNOTSUPP) {
610 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500611 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500612 " older servers\n");
613 goto neg_exit;
614 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400615 goto neg_exit;
616
Steve French9764c022017-09-17 10:41:35 -0500617 if (strcmp(ses->server->vals->version_string,
618 SMB3ANY_VERSION_STRING) == 0) {
619 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
620 cifs_dbg(VFS,
621 "SMB2 dialect returned but not requested\n");
622 return -EIO;
623 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
624 cifs_dbg(VFS,
625 "SMB2.1 dialect returned but not requested\n");
626 return -EIO;
627 }
628 } else if (strcmp(ses->server->vals->version_string,
629 SMBDEFAULT_VERSION_STRING) == 0) {
630 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
631 cifs_dbg(VFS,
632 "SMB2 dialect returned but not requested\n");
633 return -EIO;
634 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
635 /* ops set to 3.0 by default for default so update */
636 ses->server->ops = &smb21_operations;
637 }
Steve French590d08d2017-09-19 11:43:47 -0500638 } else if (le16_to_cpu(rsp->DialectRevision) !=
639 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500640 /* if requested single dialect ensure returned dialect matched */
641 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500642 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500643 return -EIO;
644 }
645
Joe Perchesf96637b2013-05-04 22:12:25 -0500646 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400647
Steve Frenche4aa25e2012-10-01 12:26:22 -0500648 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500649 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500650 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500651 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500652 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500653 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500654 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
655 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600656#ifdef CONFIG_CIFS_SMB311
657 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
658 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
659#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400660 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500661 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500662 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400663 rc = -EIO;
664 goto neg_exit;
665 }
666 server->dialect = le16_to_cpu(rsp->DialectRevision);
667
Steve French9764c022017-09-17 10:41:35 -0500668 /* BB: add check that dialect was valid given dialect(s) we asked for */
669
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100670#ifdef CONFIG_CIFS_SMB311
671 /*
672 * Keep a copy of the hash after negprot. This hash will be
673 * the starting hash value for all sessions made from this
674 * server.
675 */
676 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
677 SMB2_PREAUTH_HASH_SIZE);
678#endif
Jeff Laytone598d1d82013-05-26 07:00:59 -0400679 /* SMB2 only has an extended negflavor */
680 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400681 /* set it to the maximum buffer size value we can send with 1 credit */
682 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
683 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400684 server->max_read = le32_to_cpu(rsp->MaxReadSize);
685 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400686 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500687 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
688 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
689 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400690 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400691 /* Internal types */
692 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400693
694 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
695 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500696 /*
697 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
698 * for us will be
699 * ses->sectype = RawNTLMSSP;
700 * but for time being this is our only auth choice so doesn't matter.
701 * We just found a server which sets blob length to zero expecting raw.
702 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700703 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500704 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700705 server->sec_ntlmssp = true;
706 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700707
Jeff Layton38d77c52013-05-26 07:01:00 -0400708 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400709 if (rc)
710 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500711 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500712 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500713 if (rc == 1)
714 rc = 0;
715 else if (rc == 0)
716 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400717 }
Steve French5100d8a2018-04-09 10:47:14 -0500718
719#ifdef CONFIG_CIFS_SMB311
720 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
721 if (rsp->NegotiateContextCount)
722 rc = smb311_decode_neg_context(rsp, server);
723 else
724 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
725 }
726#endif /* CONFIG_CIFS_SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400727neg_exit:
728 free_rsp_buf(resp_buftype, rsp);
729 return rc;
730}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400731
Steve Frenchff1c0382013-11-19 23:44:46 -0600732int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
733{
Long Li2796d302018-04-25 11:30:04 -0700734 int rc;
735 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200736 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600737 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500738 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600739
740 cifs_dbg(FYI, "validate negotiate\n");
741
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100742 /* In SMB3.11 preauth integrity supersedes validate negotiate */
743 if (tcon->ses->server->dialect == SMB311_PROT_ID)
744 return 0;
745
Steve Frenchff1c0382013-11-19 23:44:46 -0600746 /*
747 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500748 * can not sign it (ie are not known user). Even if signing is not
749 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600750 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500751 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600752 */
Steve French0603c962017-09-20 19:57:18 -0500753 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600754 return 0; /* validation requires signing */
755
Steve French0603c962017-09-20 19:57:18 -0500756 if (tcon->ses->user_name == NULL) {
757 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
758 return 0; /* validation requires signing */
759 }
760
761 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
762 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
763
Long Li2796d302018-04-25 11:30:04 -0700764 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
765 if (!pneg_inbuf)
766 return -ENOMEM;
767
768 pneg_inbuf->Capabilities =
Steve Frenchff1c0382013-11-19 23:44:46 -0600769 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Long Li2796d302018-04-25 11:30:04 -0700770 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100771 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600772
773 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -0700774 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600775 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
776 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -0700777 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600778 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
779 else
Long Li2796d302018-04-25 11:30:04 -0700780 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600781
Steve French9764c022017-09-17 10:41:35 -0500782
783 if (strcmp(tcon->ses->server->vals->version_string,
784 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700785 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
786 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
787 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -0500788 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -0700789 inbuflen = sizeof(*pneg_inbuf) -
790 sizeof(pneg_inbuf->Dialects[0]);
Steve French9764c022017-09-17 10:41:35 -0500791 } else if (strcmp(tcon->ses->server->vals->version_string,
792 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700793 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
794 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
795 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
796 pneg_inbuf->DialectCount = cpu_to_le16(3);
Steve French9764c022017-09-17 10:41:35 -0500797 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -0700798 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -0500799 } else {
800 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -0700801 pneg_inbuf->Dialects[0] =
Steve French9764c022017-09-17 10:41:35 -0500802 cpu_to_le16(tcon->ses->server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -0700803 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -0500804 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -0700805 inbuflen = sizeof(*pneg_inbuf) -
806 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -0500807 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600808
809 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
810 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Long Li2796d302018-04-25 11:30:04 -0700811 (char *)pneg_inbuf, inbuflen, (char **)&pneg_rsp, &rsplen);
Steve Frenchff1c0382013-11-19 23:44:46 -0600812
813 if (rc != 0) {
814 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -0700815 rc = -EIO;
816 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -0600817 }
818
Long Li2796d302018-04-25 11:30:04 -0700819 rc = -EIO;
820 if (rsplen != sizeof(*pneg_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500821 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
822 rsplen);
823
824 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -0700825 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
826 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -0600827 }
828
829 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +0100830 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600831 goto vneg_out;
832
833 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
834 goto vneg_out;
835
836 /* do not validate server guid because not saved at negprot time yet */
837
838 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
839 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
840 goto vneg_out;
841
842 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -0700843 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600844 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -0700845 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -0600846
847vneg_out:
848 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -0700849out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200850 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -0700851out_free_inbuf:
852 kfree(pneg_inbuf);
853 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -0600854}
855
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530856enum securityEnum
857smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
858{
859 switch (requested) {
860 case Kerberos:
861 case RawNTLMSSP:
862 return requested;
863 case NTLMv2:
864 return RawNTLMSSP;
865 case Unspecified:
866 if (server->sec_ntlmssp &&
867 (global_secflags & CIFSSEC_MAY_NTLMSSP))
868 return RawNTLMSSP;
869 if ((server->sec_kerberos || server->sec_mskerberos) &&
870 (global_secflags & CIFSSEC_MAY_KRB5))
871 return Kerberos;
872 /* Fallthrough */
873 default:
874 return Unspecified;
875 }
876}
877
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100878struct SMB2_sess_data {
879 unsigned int xid;
880 struct cifs_ses *ses;
881 struct nls_table *nls_cp;
882 void (*func)(struct SMB2_sess_data *);
883 int result;
884 u64 previous_session;
885
886 /* we will send the SMB in three pieces:
887 * a fixed length beginning part, an optional
888 * SPNEGO blob (which can be zero length), and a
889 * last part which will include the strings
890 * and rest of bcc area. This allows us to avoid
891 * a large buffer 17K allocation
892 */
893 int buf0_type;
894 struct kvec iov[2];
895};
896
897static int
898SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
899{
900 int rc;
901 struct cifs_ses *ses = sess_data->ses;
902 struct smb2_sess_setup_req *req;
903 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100904 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100905
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100906 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
907 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100908 if (rc)
909 return rc;
910
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700911 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100912 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100913
914 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
915 req->PreviousSessionId = sess_data->previous_session;
916
917 req->Flags = 0; /* MBZ */
918 /* to enable echos and oplocks */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100919 req->sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100920
921 /* only one of SMB2 signing flags may be set in SMB2 request */
922 if (server->sign)
923 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
924 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
925 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
926 else
927 req->SecurityMode = 0;
928
929 req->Capabilities = 0;
930 req->Channel = 0; /* MBZ */
931
932 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100933 /* 1 for pad */
934 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100935 /*
936 * This variable will be used to clear the buffer
937 * allocated above in case of any error in the calling function.
938 */
939 sess_data->buf0_type = CIFS_SMALL_BUFFER;
940
941 return 0;
942}
943
944static void
945SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
946{
947 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
948 sess_data->buf0_type = CIFS_NO_BUFFER;
949}
950
951static int
952SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
953{
954 int rc;
955 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700956 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100957
958 /* Testing shows that buffer offset must be at location of Buffer[0] */
959 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100960 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100961 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
962
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100963 /* BB add code to build os and lm fields */
964
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100965 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
966 sess_data->iov, 2,
967 &sess_data->buf0_type,
968 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700969 cifs_small_buf_release(sess_data->iov[0].iov_base);
970 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100971
972 return rc;
973}
974
975static int
976SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
977{
978 int rc = 0;
979 struct cifs_ses *ses = sess_data->ses;
980
981 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800982 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100983 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100984 if (rc) {
985 cifs_dbg(FYI,
986 "SMB3 session key generation failed\n");
987 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800988 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100989 }
990 }
991 if (!ses->server->session_estab) {
992 ses->server->sequence_number = 0x2;
993 ses->server->session_estab = true;
994 }
995 mutex_unlock(&ses->server->srv_mutex);
996
997 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
998 spin_lock(&GlobalMid_Lock);
999 ses->status = CifsGood;
1000 ses->need_reconnect = false;
1001 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001002 return rc;
1003}
1004
1005#ifdef CONFIG_CIFS_UPCALL
1006static void
1007SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1008{
1009 int rc;
1010 struct cifs_ses *ses = sess_data->ses;
1011 struct cifs_spnego_msg *msg;
1012 struct key *spnego_key = NULL;
1013 struct smb2_sess_setup_rsp *rsp = NULL;
1014
1015 rc = SMB2_sess_alloc_buffer(sess_data);
1016 if (rc)
1017 goto out;
1018
1019 spnego_key = cifs_get_spnego_key(ses);
1020 if (IS_ERR(spnego_key)) {
1021 rc = PTR_ERR(spnego_key);
1022 spnego_key = NULL;
1023 goto out;
1024 }
1025
1026 msg = spnego_key->payload.data[0];
1027 /*
1028 * check version field to make sure that cifs.upcall is
1029 * sending us a response in an expected form
1030 */
1031 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1032 cifs_dbg(VFS,
1033 "bad cifs.upcall version. Expected %d got %d",
1034 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1035 rc = -EKEYREJECTED;
1036 goto out_put_spnego_key;
1037 }
1038
1039 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1040 GFP_KERNEL);
1041 if (!ses->auth_key.response) {
1042 cifs_dbg(VFS,
1043 "Kerberos can't allocate (%u bytes) memory",
1044 msg->sesskey_len);
1045 rc = -ENOMEM;
1046 goto out_put_spnego_key;
1047 }
1048 ses->auth_key.len = msg->sesskey_len;
1049
1050 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1051 sess_data->iov[1].iov_len = msg->secblob_len;
1052
1053 rc = SMB2_sess_sendreceive(sess_data);
1054 if (rc)
1055 goto out_put_spnego_key;
1056
1057 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001058 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001059
1060 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001061
1062 rc = SMB2_sess_establish_session(sess_data);
1063out_put_spnego_key:
1064 key_invalidate(spnego_key);
1065 key_put(spnego_key);
1066out:
1067 sess_data->result = rc;
1068 sess_data->func = NULL;
1069 SMB2_sess_free_buffer(sess_data);
1070}
1071#else
1072static void
1073SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1074{
1075 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1076 sess_data->result = -EOPNOTSUPP;
1077 sess_data->func = NULL;
1078}
1079#endif
1080
Sachin Prabhu166cea42016-10-07 19:11:22 +01001081static void
1082SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1083
1084static void
1085SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1086{
1087 int rc;
1088 struct cifs_ses *ses = sess_data->ses;
1089 struct smb2_sess_setup_rsp *rsp = NULL;
1090 char *ntlmssp_blob = NULL;
1091 bool use_spnego = false; /* else use raw ntlmssp */
1092 u16 blob_length = 0;
1093
1094 /*
1095 * If memory allocation is successful, caller of this function
1096 * frees it.
1097 */
1098 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1099 if (!ses->ntlmssp) {
1100 rc = -ENOMEM;
1101 goto out_err;
1102 }
1103 ses->ntlmssp->sesskey_per_smbsess = true;
1104
1105 rc = SMB2_sess_alloc_buffer(sess_data);
1106 if (rc)
1107 goto out_err;
1108
1109 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1110 GFP_KERNEL);
1111 if (ntlmssp_blob == NULL) {
1112 rc = -ENOMEM;
1113 goto out;
1114 }
1115
1116 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1117 if (use_spnego) {
1118 /* BB eventually need to add this */
1119 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1120 rc = -EOPNOTSUPP;
1121 goto out;
1122 } else {
1123 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1124 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1125 }
1126 sess_data->iov[1].iov_base = ntlmssp_blob;
1127 sess_data->iov[1].iov_len = blob_length;
1128
1129 rc = SMB2_sess_sendreceive(sess_data);
1130 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1131
1132 /* If true, rc here is expected and not an error */
1133 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001134 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001135 rc = 0;
1136
1137 if (rc)
1138 goto out;
1139
Ronnie Sahlberg9fdd2e02018-04-09 18:06:27 +10001140 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - ses->server->vals->header_preamble_size !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001141 le16_to_cpu(rsp->SecurityBufferOffset)) {
1142 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1143 le16_to_cpu(rsp->SecurityBufferOffset));
1144 rc = -EIO;
1145 goto out;
1146 }
1147 rc = decode_ntlmssp_challenge(rsp->Buffer,
1148 le16_to_cpu(rsp->SecurityBufferLength), ses);
1149 if (rc)
1150 goto out;
1151
1152 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1153
1154
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001155 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001156 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001157
1158out:
1159 kfree(ntlmssp_blob);
1160 SMB2_sess_free_buffer(sess_data);
1161 if (!rc) {
1162 sess_data->result = 0;
1163 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1164 return;
1165 }
1166out_err:
1167 kfree(ses->ntlmssp);
1168 ses->ntlmssp = NULL;
1169 sess_data->result = rc;
1170 sess_data->func = NULL;
1171}
1172
1173static void
1174SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1175{
1176 int rc;
1177 struct cifs_ses *ses = sess_data->ses;
1178 struct smb2_sess_setup_req *req;
1179 struct smb2_sess_setup_rsp *rsp = NULL;
1180 unsigned char *ntlmssp_blob = NULL;
1181 bool use_spnego = false; /* else use raw ntlmssp */
1182 u16 blob_length = 0;
1183
1184 rc = SMB2_sess_alloc_buffer(sess_data);
1185 if (rc)
1186 goto out;
1187
1188 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001189 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001190
1191 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1192 sess_data->nls_cp);
1193 if (rc) {
1194 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1195 goto out;
1196 }
1197
1198 if (use_spnego) {
1199 /* BB eventually need to add this */
1200 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1201 rc = -EOPNOTSUPP;
1202 goto out;
1203 }
1204 sess_data->iov[1].iov_base = ntlmssp_blob;
1205 sess_data->iov[1].iov_len = blob_length;
1206
1207 rc = SMB2_sess_sendreceive(sess_data);
1208 if (rc)
1209 goto out;
1210
1211 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1212
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001213 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001214 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001215
1216 rc = SMB2_sess_establish_session(sess_data);
1217out:
1218 kfree(ntlmssp_blob);
1219 SMB2_sess_free_buffer(sess_data);
1220 kfree(ses->ntlmssp);
1221 ses->ntlmssp = NULL;
1222 sess_data->result = rc;
1223 sess_data->func = NULL;
1224}
1225
1226static int
1227SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1228{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301229 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001230
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301231 type = smb2_select_sectype(ses->server, ses->sectype);
1232 cifs_dbg(FYI, "sess setup type %d\n", type);
1233 if (type == Unspecified) {
1234 cifs_dbg(VFS,
1235 "Unable to select appropriate authentication method!");
1236 return -EINVAL;
1237 }
1238
1239 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001240 case Kerberos:
1241 sess_data->func = SMB2_auth_kerberos;
1242 break;
1243 case RawNTLMSSP:
1244 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1245 break;
1246 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301247 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001248 return -EOPNOTSUPP;
1249 }
1250
1251 return 0;
1252}
1253
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001254int
1255SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1256 const struct nls_table *nls_cp)
1257{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001258 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001259 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001260 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001261
Joe Perchesf96637b2013-05-04 22:12:25 -05001262 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001263
Jeff Layton3534b852013-05-24 07:41:01 -04001264 if (!server) {
1265 WARN(1, "%s: server is NULL!\n", __func__);
1266 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001267 }
1268
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001269 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1270 if (!sess_data)
1271 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001272
1273 rc = SMB2_select_sec(ses, sess_data);
1274 if (rc)
1275 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001276 sess_data->xid = xid;
1277 sess_data->ses = ses;
1278 sess_data->buf0_type = CIFS_NO_BUFFER;
1279 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001280
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001281#ifdef CONFIG_CIFS_SMB311
1282 /*
1283 * Initialize the session hash with the server one.
1284 */
1285 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1286 SMB2_PREAUTH_HASH_SIZE);
1287#endif
1288
Sachin Prabhu166cea42016-10-07 19:11:22 +01001289 while (sess_data->func)
1290 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001291
Steve Frenchc721c382017-09-19 18:40:03 -05001292 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1293 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001294 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001295out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001296 kfree(sess_data);
1297 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001298}
1299
1300int
1301SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1302{
1303 struct smb2_logoff_req *req; /* response is also trivial struct */
1304 int rc = 0;
1305 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001306 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001307 unsigned int total_len;
1308 struct kvec iov[1];
1309 struct kvec rsp_iov;
1310 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001311
Joe Perchesf96637b2013-05-04 22:12:25 -05001312 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001313
1314 if (ses && (ses->server))
1315 server = ses->server;
1316 else
1317 return -EIO;
1318
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001319 /* no need to send SMB logoff if uid already closed due to reconnect */
1320 if (ses->need_reconnect)
1321 goto smb2_session_already_dead;
1322
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001323 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001324 if (rc)
1325 return rc;
1326
1327 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001328 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001329
1330 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1331 flags |= CIFS_TRANSFORM_REQ;
1332 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001333 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001334
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001335 flags |= CIFS_NO_RESP;
1336
1337 iov[0].iov_base = (char *)req;
1338 iov[0].iov_len = total_len;
1339
1340 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001341 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001342 /*
1343 * No tcon so can't do
1344 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1345 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001346
1347smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001348 return rc;
1349}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001350
1351static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1352{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001353 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001354}
1355
1356#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1357
Steve Frenchde9f68df2013-11-15 11:26:24 -06001358/* These are similar values to what Windows uses */
1359static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1360{
1361 tcon->max_chunks = 256;
1362 tcon->max_bytes_chunk = 1048576;
1363 tcon->max_bytes_copy = 16777216;
1364}
1365
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001366int
1367SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1368 struct cifs_tcon *tcon, const struct nls_table *cp)
1369{
1370 struct smb2_tree_connect_req *req;
1371 struct smb2_tree_connect_rsp *rsp = NULL;
1372 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001373 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001374 int rc = 0;
1375 int resp_buftype;
1376 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001377 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001378 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001379 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001380
Joe Perchesf96637b2013-05-04 22:12:25 -05001381 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001382
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001383 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001384 return -EIO;
1385
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001386 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1387 if (unc_path == NULL)
1388 return -ENOMEM;
1389
1390 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1391 unc_path_len *= 2;
1392 if (unc_path_len < 2) {
1393 kfree(unc_path);
1394 return -EINVAL;
1395 }
1396
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001397 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001398 tcon->tid = 0;
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001399
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001400 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1401 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001402 if (rc) {
1403 kfree(unc_path);
1404 return rc;
1405 }
1406
Steve French5a77e752018-05-09 17:43:08 -05001407 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001408 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001409
1410 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001411 /* 1 for pad */
1412 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001413
1414 /* Testing shows that buffer offset must be at location of Buffer[0] */
1415 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001416 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001417 req->PathLength = cpu_to_le16(unc_path_len - 2);
1418 iov[1].iov_base = unc_path;
1419 iov[1].iov_len = unc_path_len;
1420
Steve French6188f282018-03-13 02:29:36 -05001421 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1422 if ((ses->server->dialect == SMB311_PROT_ID) &&
Steve French5a77e752018-05-09 17:43:08 -05001423 !smb3_encryption_required(tcon))
Steve French6188f282018-03-13 02:29:36 -05001424 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1425
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001426 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001427 cifs_small_buf_release(req);
1428 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001429
1430 if (rc != 0) {
1431 if (tcon) {
1432 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1433 tcon->need_reconnect = true;
1434 }
1435 goto tcon_error_exit;
1436 }
1437
Christophe JAILLETcd123002017-05-12 17:59:32 +02001438 switch (rsp->ShareType) {
1439 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001440 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001441 break;
1442 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001443 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001444 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001445 break;
1446 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001447 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001448 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001449 break;
1450 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001451 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001452 rc = -EOPNOTSUPP;
1453 goto tcon_error_exit;
1454 }
1455
1456 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001457 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001458 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1459 tcon->tidStatus = CifsGood;
1460 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001461 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001462 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001463
1464 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1465 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001466 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001467
1468 if (tcon->seal &&
1469 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1470 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1471
Steve Frenchde9f68df2013-11-15 11:26:24 -06001472 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001473 if (tcon->ses->server->ops->validate_negotiate)
1474 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001475tcon_exit:
1476 free_rsp_buf(resp_buftype, rsp);
1477 kfree(unc_path);
1478 return rc;
1479
1480tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001481 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001482 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001483 }
1484 goto tcon_exit;
1485}
1486
1487int
1488SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1489{
1490 struct smb2_tree_disconnect_req *req; /* response is trivial */
1491 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001492 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001493 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001494 unsigned int total_len;
1495 struct kvec iov[1];
1496 struct kvec rsp_iov;
1497 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001498
Joe Perchesf96637b2013-05-04 22:12:25 -05001499 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001500
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001501 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001502 return -EIO;
1503
1504 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1505 return 0;
1506
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001507 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1508 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001509 if (rc)
1510 return rc;
1511
Steve French5a77e752018-05-09 17:43:08 -05001512 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001513 flags |= CIFS_TRANSFORM_REQ;
1514
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001515 flags |= CIFS_NO_RESP;
1516
1517 iov[0].iov_base = (char *)req;
1518 iov[0].iov_len = total_len;
1519
1520 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001521 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001522 if (rc)
1523 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1524
1525 return rc;
1526}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001527
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001528
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001529static struct create_durable *
1530create_durable_buf(void)
1531{
1532 struct create_durable *buf;
1533
1534 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1535 if (!buf)
1536 return NULL;
1537
1538 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001539 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001540 buf->ccontext.DataLength = cpu_to_le32(16);
1541 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1542 (struct create_durable, Name));
1543 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001544 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001545 buf->Name[0] = 'D';
1546 buf->Name[1] = 'H';
1547 buf->Name[2] = 'n';
1548 buf->Name[3] = 'Q';
1549 return buf;
1550}
1551
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001552static struct create_durable *
1553create_reconnect_durable_buf(struct cifs_fid *fid)
1554{
1555 struct create_durable *buf;
1556
1557 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1558 if (!buf)
1559 return NULL;
1560
1561 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1562 (struct create_durable, Data));
1563 buf->ccontext.DataLength = cpu_to_le32(16);
1564 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1565 (struct create_durable, Name));
1566 buf->ccontext.NameLength = cpu_to_le16(4);
1567 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1568 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001569 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001570 buf->Name[0] = 'D';
1571 buf->Name[1] = 'H';
1572 buf->Name[2] = 'n';
1573 buf->Name[3] = 'C';
1574 return buf;
1575}
1576
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001577static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001578parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001579 unsigned int *epoch, char *lease_key)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001580{
1581 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001582 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001583 unsigned int next;
1584 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001585 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001586
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001587 data_offset = (char *)rsp + server->vals->header_preamble_size + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001588 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001589 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001590 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001591 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001592 if (le16_to_cpu(cc->NameLength) == 4 &&
1593 strncmp(name, "RqLs", 4) == 0)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001594 return server->ops->parse_lease_buf(cc, epoch,
1595 lease_key);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001596
1597 next = le32_to_cpu(cc->Next);
1598 if (!next)
1599 break;
1600 remaining -= next;
1601 cc = (struct create_context *)((char *)cc + next);
1602 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001603
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001604 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001605}
1606
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001607static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001608add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1609 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001610{
1611 struct smb2_create_req *req = iov[0].iov_base;
1612 unsigned int num = *num_iovec;
1613
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001614 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001615 if (iov[num].iov_base == NULL)
1616 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001617 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001618 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1619 if (!req->CreateContextsOffset)
1620 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001621 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001622 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001623 le32_add_cpu(&req->CreateContextsLength,
1624 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001625 *num_iovec = num + 1;
1626 return 0;
1627}
1628
Steve Frenchb56eae42015-11-03 09:26:27 -06001629static struct create_durable_v2 *
1630create_durable_v2_buf(struct cifs_fid *pfid)
1631{
1632 struct create_durable_v2 *buf;
1633
1634 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1635 if (!buf)
1636 return NULL;
1637
1638 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1639 (struct create_durable_v2, dcontext));
1640 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1641 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1642 (struct create_durable_v2, Name));
1643 buf->ccontext.NameLength = cpu_to_le16(4);
1644
1645 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1646 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001647 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001648 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1649
1650 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1651 buf->Name[0] = 'D';
1652 buf->Name[1] = 'H';
1653 buf->Name[2] = '2';
1654 buf->Name[3] = 'Q';
1655 return buf;
1656}
1657
1658static struct create_durable_handle_reconnect_v2 *
1659create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1660{
1661 struct create_durable_handle_reconnect_v2 *buf;
1662
1663 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1664 GFP_KERNEL);
1665 if (!buf)
1666 return NULL;
1667
1668 buf->ccontext.DataOffset =
1669 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1670 dcontext));
1671 buf->ccontext.DataLength =
1672 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1673 buf->ccontext.NameOffset =
1674 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1675 Name));
1676 buf->ccontext.NameLength = cpu_to_le16(4);
1677
1678 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1679 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1680 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1681 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1682
1683 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1684 buf->Name[0] = 'D';
1685 buf->Name[1] = 'H';
1686 buf->Name[2] = '2';
1687 buf->Name[3] = 'C';
1688 return buf;
1689}
1690
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001691static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001692add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001693 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001694{
1695 struct smb2_create_req *req = iov[0].iov_base;
1696 unsigned int num = *num_iovec;
1697
Steve Frenchb56eae42015-11-03 09:26:27 -06001698 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1699 if (iov[num].iov_base == NULL)
1700 return -ENOMEM;
1701 iov[num].iov_len = sizeof(struct create_durable_v2);
1702 if (!req->CreateContextsOffset)
1703 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001704 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001705 iov[1].iov_len);
1706 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001707 *num_iovec = num + 1;
1708 return 0;
1709}
1710
1711static int
1712add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1713 struct cifs_open_parms *oparms)
1714{
1715 struct smb2_create_req *req = iov[0].iov_base;
1716 unsigned int num = *num_iovec;
1717
1718 /* indicate that we don't need to relock the file */
1719 oparms->reconnect = false;
1720
1721 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1722 if (iov[num].iov_base == NULL)
1723 return -ENOMEM;
1724 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1725 if (!req->CreateContextsOffset)
1726 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001727 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001728 iov[1].iov_len);
1729 le32_add_cpu(&req->CreateContextsLength,
1730 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001731 *num_iovec = num + 1;
1732 return 0;
1733}
1734
1735static int
1736add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1737 struct cifs_open_parms *oparms, bool use_persistent)
1738{
1739 struct smb2_create_req *req = iov[0].iov_base;
1740 unsigned int num = *num_iovec;
1741
1742 if (use_persistent) {
1743 if (oparms->reconnect)
1744 return add_durable_reconnect_v2_context(iov, num_iovec,
1745 oparms);
1746 else
1747 return add_durable_v2_context(iov, num_iovec, oparms);
1748 }
1749
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001750 if (oparms->reconnect) {
1751 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1752 /* indicate that we don't need to relock the file */
1753 oparms->reconnect = false;
1754 } else
1755 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001756 if (iov[num].iov_base == NULL)
1757 return -ENOMEM;
1758 iov[num].iov_len = sizeof(struct create_durable);
1759 if (!req->CreateContextsOffset)
1760 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001761 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001762 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001763 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001764 *num_iovec = num + 1;
1765 return 0;
1766}
1767
Aurelien Aptelf0712922017-02-22 14:47:17 +01001768static int
1769alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1770 const char *treename, const __le16 *path)
1771{
1772 int treename_len, path_len;
1773 struct nls_table *cp;
1774 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1775
1776 /*
1777 * skip leading "\\"
1778 */
1779 treename_len = strlen(treename);
1780 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1781 return -EINVAL;
1782
1783 treename += 2;
1784 treename_len -= 2;
1785
1786 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1787
1788 /*
1789 * make room for one path separator between the treename and
1790 * path
1791 */
1792 *out_len = treename_len + 1 + path_len;
1793
1794 /*
1795 * final path needs to be null-terminated UTF16 with a
1796 * size aligned to 8
1797 */
1798
1799 *out_size = roundup((*out_len+1)*2, 8);
1800 *out_path = kzalloc(*out_size, GFP_KERNEL);
1801 if (!*out_path)
1802 return -ENOMEM;
1803
1804 cp = load_nls_default();
1805 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1806 UniStrcat(*out_path, sep);
1807 UniStrcat(*out_path, path);
1808 unload_nls(cp);
1809
1810 return 0;
1811}
1812
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001813int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001814SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001815 __u8 *oplock, struct smb2_file_all_info *buf,
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001816 struct kvec *err_iov)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001817{
1818 struct smb2_create_req *req;
1819 struct smb2_create_rsp *rsp;
1820 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001821 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001822 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001823 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001824 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001825 int resp_buftype;
1826 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001827 __le16 *copy_path = NULL;
1828 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001829 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001830 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001831 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001832 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001833 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001834 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001835
Joe Perchesf96637b2013-05-04 22:12:25 -05001836 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001837
1838 if (ses && (ses->server))
1839 server = ses->server;
1840 else
1841 return -EIO;
1842
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001843 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1844
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001845 if (rc)
1846 return rc;
1847
Steve French5a77e752018-05-09 17:43:08 -05001848 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001849 flags |= CIFS_TRANSFORM_REQ;
1850
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001851 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001852 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001853 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1854 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001855
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001856 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001857 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001858 /* File attributes ignored on open (used in create though) */
1859 req->FileAttributes = cpu_to_le32(file_attributes);
1860 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001861 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1862 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001863
1864 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001865 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001866 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001867
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001868 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01001869
1870 /* [MS-SMB2] 2.2.13 NameOffset:
1871 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1872 * the SMB2 header, the file name includes a prefix that will
1873 * be processed during DFS name normalization as specified in
1874 * section 3.3.5.9. Otherwise, the file name is relative to
1875 * the share that is identified by the TreeId in the SMB2
1876 * header.
1877 */
1878 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1879 int name_len;
1880
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001881 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001882 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1883 &name_len,
1884 tcon->treeName, path);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001885 if (rc) {
1886 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001887 return rc;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001888 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001889 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001890 uni_path_len = copy_size;
1891 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001892 } else {
1893 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1894 /* MUST set path len (NameLength) to 0 opening root of share */
1895 req->NameLength = cpu_to_le16(uni_path_len - 2);
1896 if (uni_path_len % 8 != 0) {
1897 copy_size = roundup(uni_path_len, 8);
1898 copy_path = kzalloc(copy_size, GFP_KERNEL);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001899 if (!copy_path) {
1900 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001901 return -ENOMEM;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001902 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001903 memcpy((char *)copy_path, (const char *)path,
1904 uni_path_len);
1905 uni_path_len = copy_size;
1906 path = copy_path;
1907 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001908 }
1909
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001910 iov[1].iov_len = uni_path_len;
1911 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001912
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001913 if (!server->oplocks)
1914 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1915
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001916 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001917 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1918 req->RequestedOplockLevel = *oplock;
1919 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001920 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001921 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001922 cifs_small_buf_release(req);
1923 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001924 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001925 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001926 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001927 }
1928
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001929 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1930 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001931 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001932 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001933 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001934 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001935 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001936 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001937
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001938 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001939 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001940 if (rc) {
1941 cifs_small_buf_release(req);
1942 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001943 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001944 return rc;
1945 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001946 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001947 }
1948
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001949 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1950 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001951 cifs_small_buf_release(req);
1952 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001953
1954 if (rc != 0) {
1955 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001956 if (err_iov && rsp) {
1957 *err_iov = rsp_iov;
1958 resp_buftype = CIFS_NO_BUFFER;
1959 rsp = NULL;
1960 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001961 goto creat_exit;
1962 }
1963
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001964 oparms->fid->persistent_fid = rsp->PersistentFileId;
1965 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001966
1967 if (buf) {
1968 memcpy(buf, &rsp->CreationTime, 32);
1969 buf->AllocationSize = rsp->AllocationSize;
1970 buf->EndOfFile = rsp->EndofFile;
1971 buf->Attributes = rsp->FileAttributes;
1972 buf->NumberOfLinks = cpu_to_le32(1);
1973 buf->DeletePending = 0;
1974 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001975
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001976 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001977 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch,
1978 oparms->fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001979 else
1980 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001981creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001982 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001983 kfree(lc_buf);
1984 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001985 free_rsp_buf(resp_buftype, rsp);
1986 return rc;
1987}
1988
Steve French4a72daf2013-06-25 00:20:49 -05001989/*
1990 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1991 */
1992int
1993SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001994 u64 volatile_fid, u32 opcode, bool is_fsctl,
Aurelien Aptel51146622017-02-28 15:08:41 +01001995 char *in_data, u32 indatalen,
1996 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001997{
1998 struct smb2_ioctl_req *req;
1999 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002000 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05002001 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05002002 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002003 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002004 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002005 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002006 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002007 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002008 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05002009
2010 cifs_dbg(FYI, "SMB2 IOCTL\n");
2011
Steve French3d1a3742014-08-11 21:05:25 -05002012 if (out_data != NULL)
2013 *out_data = NULL;
2014
Steve French4a72daf2013-06-25 00:20:49 -05002015 /* zero out returned data len, in case of error */
2016 if (plen)
2017 *plen = 0;
2018
Steve French8e353102015-03-26 19:47:02 -05002019 if (tcon)
2020 ses = tcon->ses;
2021 else
2022 return -EIO;
2023
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002024 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05002025 return -EIO;
2026
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002027 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002028 if (rc)
2029 return rc;
2030
Steve French5a77e752018-05-09 17:43:08 -05002031 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002032 flags |= CIFS_TRANSFORM_REQ;
2033
Steve French4a72daf2013-06-25 00:20:49 -05002034 req->CtlCode = cpu_to_le32(opcode);
2035 req->PersistentFileId = persistent_fid;
2036 req->VolatileFileId = volatile_fid;
2037
2038 if (indatalen) {
2039 req->InputCount = cpu_to_le32(indatalen);
2040 /* do not set InputOffset if no input data */
2041 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002042 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05002043 iov[1].iov_base = in_data;
2044 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002045 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05002046 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002047 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05002048
2049 req->OutputOffset = 0;
2050 req->OutputCount = 0; /* MBZ */
2051
2052 /*
2053 * Could increase MaxOutputResponse, but that would require more
2054 * than one credit. Windows typically sets this smaller, but for some
2055 * ioctls it may be useful to allow server to send more. No point
2056 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05002057 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2058 * (by default, note that it can be overridden to make max larger)
2059 * in responses (except for read responses which can be bigger.
2060 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05002061 */
Steve French7db0a6e2017-05-03 21:12:20 -05002062 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05002063
2064 if (is_fsctl)
2065 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2066 else
2067 req->Flags = 0;
2068
2069 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05002070
Steve French7ff8d452013-10-14 00:44:19 -05002071 /*
2072 * If no input data, the size of ioctl struct in
2073 * protocol spec still includes a 1 byte data buffer,
2074 * but if input data passed to ioctl, we do not
2075 * want to double count this, so we do not send
2076 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002077 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05002078 */
2079
2080 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002081 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05002082 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002083 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05002084
Steve French4587eee2017-10-25 15:58:31 -05002085 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2086 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002087 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002088
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002089 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2090 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002091 cifs_small_buf_release(req);
2092 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002093
Steve Frencheccb4422018-05-17 21:16:55 -05002094 if (rc != 0)
2095 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2096 ses->Suid, 0, opcode, rc);
2097
Steve French9bf0c9c2013-11-16 18:05:28 -06002098 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002099 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002100 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002101 } else if (rc == -EINVAL) {
2102 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2103 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002104 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002105 goto ioctl_exit;
2106 }
Steve French4a72daf2013-06-25 00:20:49 -05002107 }
2108
2109 /* check if caller wants to look at return data or just return rc */
2110 if ((plen == NULL) || (out_data == NULL))
2111 goto ioctl_exit;
2112
2113 *plen = le32_to_cpu(rsp->OutputCount);
2114
2115 /* We check for obvious errors in the output buffer length and offset */
2116 if (*plen == 0)
2117 goto ioctl_exit; /* server returned no data */
2118 else if (*plen > 0xFF00) {
2119 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2120 *plen = 0;
2121 rc = -EIO;
2122 goto ioctl_exit;
2123 }
2124
2125 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2126 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2127 le32_to_cpu(rsp->OutputOffset));
2128 *plen = 0;
2129 rc = -EIO;
2130 goto ioctl_exit;
2131 }
2132
2133 *out_data = kmalloc(*plen, GFP_KERNEL);
2134 if (*out_data == NULL) {
2135 rc = -ENOMEM;
2136 goto ioctl_exit;
2137 }
2138
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002139 shdr = get_sync_hdr(rsp);
2140 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002141ioctl_exit:
2142 free_rsp_buf(resp_buftype, rsp);
2143 return rc;
2144}
2145
Steve French64a5cfa2013-10-14 15:31:32 -05002146/*
2147 * Individual callers to ioctl worker function follow
2148 */
2149
2150int
2151SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2152 u64 persistent_fid, u64 volatile_fid)
2153{
2154 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002155 struct compress_ioctl fsctl_input;
2156 char *ret_data = NULL;
2157
2158 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002159 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002160
2161 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2162 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2163 (char *)&fsctl_input /* data input */,
2164 2 /* in data len */, &ret_data /* out data */, NULL);
2165
2166 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002167
2168 return rc;
2169}
2170
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002171int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002172SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2173 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002174{
2175 struct smb2_close_req *req;
2176 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002177 struct cifs_ses *ses = tcon->ses;
2178 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002179 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002180 int resp_buftype;
2181 int rc = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002182 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002183
Joe Perchesf96637b2013-05-04 22:12:25 -05002184 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002185
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002186 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002187 return -EIO;
2188
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002189 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002190 if (rc)
2191 return rc;
2192
Steve French5a77e752018-05-09 17:43:08 -05002193 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002194 flags |= CIFS_TRANSFORM_REQ;
2195
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002196 req->PersistentFileId = persistent_fid;
2197 req->VolatileFileId = volatile_fid;
2198
2199 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002200 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002201
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002202 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002203 cifs_small_buf_release(req);
2204 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002205
2206 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002207 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002208 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2209 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002210 goto close_exit;
2211 }
2212
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002213 /* BB FIXME - decode close response, update inode for caching */
2214
2215close_exit:
2216 free_rsp_buf(resp_buftype, rsp);
2217 return rc;
2218}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002219
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002220int
2221SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2222 u64 persistent_fid, u64 volatile_fid)
2223{
2224 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2225}
2226
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002227static int
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002228validate_iov(struct TCP_Server_Info *server,
2229 unsigned int offset, unsigned int buffer_length,
2230 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002231{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002232 unsigned int smb_len = iov->iov_len;
2233 char *end_of_smb = smb_len + server->vals->header_preamble_size + (char *)iov->iov_base;
2234 char *begin_of_buf = server->vals->header_preamble_size + offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002235 char *end_of_buf = begin_of_buf + buffer_length;
2236
2237
2238 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002239 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2240 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002241 return -EINVAL;
2242 }
2243
2244 /* check if beyond RFC1001 maximum length */
2245 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002246 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2247 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002248 return -EINVAL;
2249 }
2250
2251 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002252 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002253 return -EINVAL;
2254 }
2255
2256 return 0;
2257}
2258
2259/*
2260 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2261 * Caller must free buffer.
2262 */
2263static int
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002264validate_and_copy_iov(struct TCP_Server_Info *server,
2265 unsigned int offset, unsigned int buffer_length,
2266 struct kvec *iov, unsigned int minbufsize,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002267 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002268{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002269 char *begin_of_buf = server->vals->header_preamble_size + offset + (char *)(iov->iov_base);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002270 int rc;
2271
2272 if (!data)
2273 return -EINVAL;
2274
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002275 rc = validate_iov(server, offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002276 if (rc)
2277 return rc;
2278
2279 memcpy(data, begin_of_buf, buffer_length);
2280
2281 return 0;
2282}
2283
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002284static int
2285query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002286 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2287 u32 additional_info, size_t output_len, size_t min_len, void **data,
2288 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002289{
2290 struct smb2_query_info_req *req;
2291 struct smb2_query_info_rsp *rsp = NULL;
2292 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002293 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002294 int rc = 0;
2295 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002296 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002297 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002298 unsigned int total_len;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002299
Joe Perchesf96637b2013-05-04 22:12:25 -05002300 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002301
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002302 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002303 return -EIO;
2304
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002305 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2306 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002307 if (rc)
2308 return rc;
2309
Steve French5a77e752018-05-09 17:43:08 -05002310 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002311 flags |= CIFS_TRANSFORM_REQ;
2312
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002313 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002314 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002315 req->PersistentFileId = persistent_fid;
2316 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002317 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002318
2319 /*
2320 * We do not use the input buffer (do not send extra byte)
2321 */
2322 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002323
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002324 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002325
2326 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002327 /* 1 for Buffer */
2328 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002329
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002330 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002331 cifs_small_buf_release(req);
2332 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002333
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002334 if (rc) {
2335 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002336 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2337 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002338 goto qinf_exit;
2339 }
2340
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002341 if (dlen) {
2342 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2343 if (!*data) {
2344 *data = kmalloc(*dlen, GFP_KERNEL);
2345 if (!*data) {
2346 cifs_dbg(VFS,
2347 "Error %d allocating memory for acl\n",
2348 rc);
2349 *dlen = 0;
2350 goto qinf_exit;
2351 }
2352 }
2353 }
2354
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002355 rc = validate_and_copy_iov(ses->server,
2356 le16_to_cpu(rsp->OutputBufferOffset),
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002357 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002358 &rsp_iov, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002359
2360qinf_exit:
2361 free_rsp_buf(resp_buftype, rsp);
2362 return rc;
2363}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002364
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002365int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002366 u64 persistent_fid, u64 volatile_fid,
2367 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002368{
2369 return query_info(xid, tcon, persistent_fid, volatile_fid,
2370 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002371 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002372 sizeof(struct smb2_file_full_ea_info),
2373 (void **)&data,
2374 NULL);
2375}
2376
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002377int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2378 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002379{
2380 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002381 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002382 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002383 sizeof(struct smb2_file_all_info), (void **)&data,
2384 NULL);
2385}
2386
2387int
2388SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2389 u64 persistent_fid, u64 volatile_fid,
2390 void **data, u32 *plen)
2391{
2392 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2393 *plen = 0;
2394
2395 return query_info(xid, tcon, persistent_fid, volatile_fid,
2396 0, SMB2_O_INFO_SECURITY, additional_info,
2397 SMB2_MAX_BUFFER_SIZE,
2398 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002399}
2400
2401int
2402SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2403 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2404{
2405 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002406 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002407 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002408 sizeof(struct smb2_file_internal_info),
2409 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002410}
2411
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002412/*
2413 * This is a no-op for now. We're not really interested in the reply, but
2414 * rather in the fact that the server sent one and that server->lstrp
2415 * gets updated.
2416 *
2417 * FIXME: maybe we should consider checking that the reply matches request?
2418 */
2419static void
2420smb2_echo_callback(struct mid_q_entry *mid)
2421{
2422 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002423 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002424 unsigned int credits_received = 1;
2425
2426 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002427 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002428
2429 DeleteMidQEntry(mid);
2430 add_credits(server, credits_received, CIFS_ECHO_OP);
2431}
2432
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002433void smb2_reconnect_server(struct work_struct *work)
2434{
2435 struct TCP_Server_Info *server = container_of(work,
2436 struct TCP_Server_Info, reconnect.work);
2437 struct cifs_ses *ses;
2438 struct cifs_tcon *tcon, *tcon2;
2439 struct list_head tmp_list;
2440 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002441 int rc;
2442 int resched = false;
2443
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002444
2445 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2446 mutex_lock(&server->reconnect_mutex);
2447
2448 INIT_LIST_HEAD(&tmp_list);
2449 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2450
2451 spin_lock(&cifs_tcp_ses_lock);
2452 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2453 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002454 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002455 tcon->tc_count++;
2456 list_add_tail(&tcon->rlist, &tmp_list);
2457 tcon_exist = true;
2458 }
2459 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002460 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2461 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2462 tcon_exist = true;
2463 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002464 }
2465 /*
2466 * Get the reference to server struct to be sure that the last call of
2467 * cifs_put_tcon() in the loop below won't release the server pointer.
2468 */
2469 if (tcon_exist)
2470 server->srv_count++;
2471
2472 spin_unlock(&cifs_tcp_ses_lock);
2473
2474 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002475 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2476 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002477 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002478 else
2479 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002480 list_del_init(&tcon->rlist);
2481 cifs_put_tcon(tcon);
2482 }
2483
2484 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002485 if (resched)
2486 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002487 mutex_unlock(&server->reconnect_mutex);
2488
2489 /* now we can safely release srv struct */
2490 if (tcon_exist)
2491 cifs_put_tcp_session(server, 1);
2492}
2493
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002494int
2495SMB2_echo(struct TCP_Server_Info *server)
2496{
2497 struct smb2_echo_req *req;
2498 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002499 struct kvec iov[2];
2500 struct smb_rqst rqst = { .rq_iov = iov,
2501 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002502 unsigned int total_len;
2503 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002504
Joe Perchesf96637b2013-05-04 22:12:25 -05002505 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002506
Steve French4fcd1812016-06-22 20:12:05 -05002507 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002508 /* No need to send echo on newly established connections */
2509 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2510 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002511 }
2512
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002513 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002514 if (rc)
2515 return rc;
2516
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002517 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002518
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002519 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002520 rfc1002_marker = cpu_to_be32(total_len);
2521 iov[0].iov_base = &rfc1002_marker;
2522 iov[1].iov_len = total_len;
2523 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002524
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002525 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2526 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002527 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002528 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002529
2530 cifs_small_buf_release(req);
2531 return rc;
2532}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002533
2534int
2535SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2536 u64 volatile_fid)
2537{
2538 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002539 struct cifs_ses *ses = tcon->ses;
2540 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002541 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002542 int resp_buftype;
2543 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002544 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002545 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002546
Joe Perchesf96637b2013-05-04 22:12:25 -05002547 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002548
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002549 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002550 return -EIO;
2551
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002552 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002553 if (rc)
2554 return rc;
2555
Steve French5a77e752018-05-09 17:43:08 -05002556 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002557 flags |= CIFS_TRANSFORM_REQ;
2558
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002559 req->PersistentFileId = persistent_fid;
2560 req->VolatileFileId = volatile_fid;
2561
2562 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002563 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002564
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002565 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002566 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002567
Steve Frencheccb4422018-05-17 21:16:55 -05002568 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002569 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002570 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
2571 rc);
2572 }
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002573
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002574 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002575 return rc;
2576}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002577
2578/*
2579 * To form a chain of read requests, any read requests after the first should
2580 * have the end_of_chain boolean set to true.
2581 */
2582static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002583smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002584 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2585 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002586{
2587 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002588 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002589 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002590 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002591
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002592 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2593 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002594 if (rc)
2595 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002596
2597 server = io_parms->tcon->ses->server;
2598 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002599 return -ECONNABORTED;
2600
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002601 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002602 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002603
2604 req->PersistentFileId = io_parms->persistent_fid;
2605 req->VolatileFileId = io_parms->volatile_fid;
2606 req->ReadChannelInfoOffset = 0; /* reserved */
2607 req->ReadChannelInfoLength = 0; /* reserved */
2608 req->Channel = 0; /* reserved */
2609 req->MinimumCount = 0;
2610 req->Length = cpu_to_le32(io_parms->length);
2611 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07002612#ifdef CONFIG_CIFS_SMB_DIRECT
2613 /*
2614 * If we want to do a RDMA write, fill in and append
2615 * smbd_buffer_descriptor_v1 to the end of read request
2616 */
Long Libb4c0412018-04-17 12:17:08 -07002617 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07002618 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002619
Long Libd3dcc62017-11-22 17:38:47 -07002620 struct smbd_buffer_descriptor_v1 *v1;
2621 bool need_invalidate =
2622 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2623
2624 rdata->mr = smbd_register_mr(
2625 server->smbd_conn, rdata->pages,
2626 rdata->nr_pages, rdata->tailsz,
2627 true, need_invalidate);
2628 if (!rdata->mr)
2629 return -ENOBUFS;
2630
2631 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2632 if (need_invalidate)
2633 req->Channel = SMB2_CHANNEL_RDMA_V1;
2634 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002635 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07002636 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002637 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07002638 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002639 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2640 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2641 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07002642
2643 *total_len += sizeof(*v1) - 1;
2644 }
2645#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002646 if (request_type & CHAINED_REQUEST) {
2647 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002648 /* next 8-byte aligned request */
2649 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2650 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002651 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002652 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002653 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002654 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002655 /*
2656 * Related requests use info from previous read request
2657 * in chain.
2658 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002659 shdr->SessionId = 0xFFFFFFFF;
2660 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002661 req->PersistentFileId = 0xFFFFFFFF;
2662 req->VolatileFileId = 0xFFFFFFFF;
2663 }
2664 }
2665 if (remaining_bytes > io_parms->length)
2666 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2667 else
2668 req->RemainingBytes = 0;
2669
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002670 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002671 return rc;
2672}
2673
2674static void
2675smb2_readv_callback(struct mid_q_entry *mid)
2676{
2677 struct cifs_readdata *rdata = mid->callback_data;
2678 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2679 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002680 struct smb2_sync_hdr *shdr =
2681 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002682 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002683 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2684 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002685 .rq_pages = rdata->pages,
2686 .rq_npages = rdata->nr_pages,
2687 .rq_pagesz = rdata->pagesz,
2688 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002689
Joe Perchesf96637b2013-05-04 22:12:25 -05002690 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2691 __func__, mid->mid, mid->mid_state, rdata->result,
2692 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002693
2694 switch (mid->mid_state) {
2695 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002696 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002697 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002698 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002699 int rc;
2700
Jeff Layton0b688cf2012-09-18 16:20:34 -07002701 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002702 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002703 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2704 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002705 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002706 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002707 task_io_account_read(rdata->got_bytes);
2708 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002709 break;
2710 case MID_REQUEST_SUBMITTED:
2711 case MID_RETRY_NEEDED:
2712 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002713 if (server->sign && rdata->got_bytes)
2714 /* reset bytes number since we can not check a sign */
2715 rdata->got_bytes = 0;
2716 /* FIXME: should this be counted toward the initiating task? */
2717 task_io_account_read(rdata->got_bytes);
2718 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002719 break;
2720 default:
2721 if (rdata->result != -ENODATA)
2722 rdata->result = -EIO;
2723 }
Long Libd3dcc62017-11-22 17:38:47 -07002724#ifdef CONFIG_CIFS_SMB_DIRECT
2725 /*
2726 * If this rdata has a memmory registered, the MR can be freed
2727 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2728 * because they have limited number and are used for future I/Os
2729 */
2730 if (rdata->mr) {
2731 smbd_deregister_mr(rdata->mr);
2732 rdata->mr = NULL;
2733 }
2734#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002735 if (rdata->result)
2736 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2737
2738 queue_work(cifsiod_wq, &rdata->work);
2739 DeleteMidQEntry(mid);
2740 add_credits(server, credits_received, 0);
2741}
2742
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002743/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002744int
2745smb2_async_readv(struct cifs_readdata *rdata)
2746{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002747 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002748 char *buf;
2749 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002750 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002751 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2752 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002753 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002754 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002755 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002756
Joe Perchesf96637b2013-05-04 22:12:25 -05002757 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2758 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002759
2760 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2761 io_parms.offset = rdata->offset;
2762 io_parms.length = rdata->bytes;
2763 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2764 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2765 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002766
2767 server = io_parms.tcon->ses->server;
2768
Long Li2dabfd52017-11-07 01:54:53 -07002769 rc = smb2_new_read_req(
2770 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002771 if (rc) {
2772 if (rc == -EAGAIN && rdata->credits) {
2773 /* credits was reset by reconnect */
2774 rdata->credits = 0;
2775 /* reduce in_flight value since we won't send the req */
2776 spin_lock(&server->req_lock);
2777 server->in_flight--;
2778 spin_unlock(&server->req_lock);
2779 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002780 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002781 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002782
Steve French5a77e752018-05-09 17:43:08 -05002783 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002784 flags |= CIFS_TRANSFORM_REQ;
2785
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002786 req_len = cpu_to_be32(total_len);
2787
2788 rdata->iov[0].iov_base = &req_len;
2789 rdata->iov[0].iov_len = sizeof(__be32);
2790 rdata->iov[1].iov_base = buf;
2791 rdata->iov[1].iov_len = total_len;
2792
2793 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002794
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002795 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002796 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002797 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002798 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002799 spin_lock(&server->req_lock);
2800 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002801 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002802 spin_unlock(&server->req_lock);
2803 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002804 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002805 }
2806
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002807 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002808 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002809 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002810 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002811 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002812 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002813 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002814 trace_smb3_read_err(rc, 0 /* xid */, io_parms.persistent_fid,
2815 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
2816 io_parms.offset, io_parms.length);
2817 } else
2818 trace_smb3_read_done(0 /* xid */, io_parms.persistent_fid,
2819 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
2820 io_parms.offset, io_parms.length);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002821
2822 cifs_small_buf_release(buf);
2823 return rc;
2824}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002825
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002826int
2827SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2828 unsigned int *nbytes, char **buf, int *buf_type)
2829{
2830 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002831 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002832 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002833 struct smb2_sync_hdr *shdr;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002834 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002835 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002836 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002837 int flags = CIFS_LOG_ERROR;
2838 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002839
2840 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07002841 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002842 if (rc)
2843 return rc;
2844
Steve French5a77e752018-05-09 17:43:08 -05002845 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002846 flags |= CIFS_TRANSFORM_REQ;
2847
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002848 iov[0].iov_base = (char *)req;
2849 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002850
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002851 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002852 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002853
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002854 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002855
2856 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002857 if (rc != -ENODATA) {
2858 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2859 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002860 }
Steve Frencheccb4422018-05-17 21:16:55 -05002861 trace_smb3_read_err(rc, xid, req->PersistentFileId,
2862 io_parms->tcon->tid, ses->Suid,
2863 io_parms->offset, io_parms->length);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002864 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2865 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05002866 } else
2867 trace_smb3_read_done(xid, req->PersistentFileId,
2868 io_parms->tcon->tid, ses->Suid,
2869 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002870
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002871 *nbytes = le32_to_cpu(rsp->DataLength);
2872 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2873 (*nbytes > io_parms->length)) {
2874 cifs_dbg(FYI, "bad length %d for count %d\n",
2875 *nbytes, io_parms->length);
2876 rc = -EIO;
2877 *nbytes = 0;
2878 }
2879
2880 shdr = get_sync_hdr(rsp);
2881
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002882 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002883 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002884 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002885 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002886 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002887 if (resp_buftype == CIFS_SMALL_BUFFER)
2888 *buf_type = CIFS_SMALL_BUFFER;
2889 else if (resp_buftype == CIFS_LARGE_BUFFER)
2890 *buf_type = CIFS_LARGE_BUFFER;
2891 }
2892 return rc;
2893}
2894
Pavel Shilovsky33319142012-09-18 16:20:29 -07002895/*
2896 * Check the mid_state and signature on received buffer (if any), and queue the
2897 * workqueue completion task.
2898 */
2899static void
2900smb2_writev_callback(struct mid_q_entry *mid)
2901{
2902 struct cifs_writedata *wdata = mid->callback_data;
2903 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2904 unsigned int written;
2905 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2906 unsigned int credits_received = 1;
2907
2908 switch (mid->mid_state) {
2909 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002910 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002911 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2912 if (wdata->result != 0)
2913 break;
2914
2915 written = le32_to_cpu(rsp->DataLength);
2916 /*
2917 * Mask off high 16 bits when bytes written as returned
2918 * by the server is greater than bytes requested by the
2919 * client. OS/2 servers are known to set incorrect
2920 * CountHigh values.
2921 */
2922 if (written > wdata->bytes)
2923 written &= 0xFFFF;
2924
2925 if (written < wdata->bytes)
2926 wdata->result = -ENOSPC;
2927 else
2928 wdata->bytes = written;
2929 break;
2930 case MID_REQUEST_SUBMITTED:
2931 case MID_RETRY_NEEDED:
2932 wdata->result = -EAGAIN;
2933 break;
2934 default:
2935 wdata->result = -EIO;
2936 break;
2937 }
Long Lidb223a52017-11-22 17:38:45 -07002938#ifdef CONFIG_CIFS_SMB_DIRECT
2939 /*
2940 * If this wdata has a memory registered, the MR can be freed
2941 * The number of MRs available is limited, it's important to recover
2942 * used MR as soon as I/O is finished. Hold MR longer in the later
2943 * I/O process can possibly result in I/O deadlock due to lack of MR
2944 * to send request on I/O retry
2945 */
2946 if (wdata->mr) {
2947 smbd_deregister_mr(wdata->mr);
2948 wdata->mr = NULL;
2949 }
2950#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002951 if (wdata->result)
2952 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2953
2954 queue_work(cifsiod_wq, &wdata->work);
2955 DeleteMidQEntry(mid);
2956 add_credits(tcon->ses->server, credits_received, 0);
2957}
2958
2959/* smb2_async_writev - send an async write, and set up mid to handle result */
2960int
Steve French4a5c80d2014-02-07 20:45:12 -06002961smb2_async_writev(struct cifs_writedata *wdata,
2962 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002963{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002964 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002965 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002966 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002967 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002968 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002969 struct kvec iov[2];
2970 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002971 unsigned int total_len;
2972 __be32 rfc1002_marker;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002973
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002974 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002975 if (rc) {
2976 if (rc == -EAGAIN && wdata->credits) {
2977 /* credits was reset by reconnect */
2978 wdata->credits = 0;
2979 /* reduce in_flight value since we won't send the req */
2980 spin_lock(&server->req_lock);
2981 server->in_flight--;
2982 spin_unlock(&server->req_lock);
2983 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002984 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002985 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002986
Steve French5a77e752018-05-09 17:43:08 -05002987 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002988 flags |= CIFS_TRANSFORM_REQ;
2989
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002990 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002991 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002992
2993 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2994 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2995 req->WriteChannelInfoOffset = 0;
2996 req->WriteChannelInfoLength = 0;
2997 req->Channel = 0;
2998 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002999 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003000 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003001 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07003002#ifdef CONFIG_CIFS_SMB_DIRECT
3003 /*
3004 * If we want to do a server RDMA read, fill in and append
3005 * smbd_buffer_descriptor_v1 to the end of write request
3006 */
Long Libb4c0412018-04-17 12:17:08 -07003007 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003008 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003009
Long Lidb223a52017-11-22 17:38:45 -07003010 struct smbd_buffer_descriptor_v1 *v1;
3011 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3012
3013 wdata->mr = smbd_register_mr(
3014 server->smbd_conn, wdata->pages,
3015 wdata->nr_pages, wdata->tailsz,
3016 false, need_invalidate);
3017 if (!wdata->mr) {
3018 rc = -ENOBUFS;
3019 goto async_writev_out;
3020 }
3021 req->Length = 0;
3022 req->DataOffset = 0;
3023 req->RemainingBytes =
Steve French2026b062018-01-24 23:07:41 -06003024 cpu_to_le32((wdata->nr_pages-1)*PAGE_SIZE + wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003025 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3026 if (need_invalidate)
3027 req->Channel = SMB2_CHANNEL_RDMA_V1;
3028 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003029 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003030 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003031 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003032 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003033 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3034 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3035 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003036 }
3037#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003038 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003039 iov[0].iov_len = 4;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003040 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
3041 iov[0].iov_base = &rfc1002_marker;
3042 iov[1].iov_len = total_len - 1;
3043 iov[1].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003044
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003045 rqst.rq_iov = iov;
3046 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003047 rqst.rq_pages = wdata->pages;
3048 rqst.rq_npages = wdata->nr_pages;
3049 rqst.rq_pagesz = wdata->pagesz;
3050 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003051#ifdef CONFIG_CIFS_SMB_DIRECT
3052 if (wdata->mr) {
3053 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3054 rqst.rq_npages = 0;
3055 }
3056#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003057 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3058 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003059
Long Lidb223a52017-11-22 17:38:45 -07003060#ifdef CONFIG_CIFS_SMB_DIRECT
3061 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3062 if (!wdata->mr)
3063 req->Length = cpu_to_le32(wdata->bytes);
3064#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003065 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003066#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003067
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003068 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003069 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003070 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003071 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003072 spin_lock(&server->req_lock);
3073 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003074 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003075 spin_unlock(&server->req_lock);
3076 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003077 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003078 }
3079
Pavel Shilovsky33319142012-09-18 16:20:29 -07003080 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003081 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3082 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003083
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003084 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003085 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3086 tcon->tid, tcon->ses->Suid, wdata->offset,
3087 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06003088 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003089 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003090 } else
3091 trace_smb3_write_done(0 /* no xid */, req->PersistentFileId,
3092 tcon->tid, tcon->ses->Suid, wdata->offset,
3093 wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003094
Pavel Shilovsky33319142012-09-18 16:20:29 -07003095async_writev_out:
3096 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003097 return rc;
3098}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003099
3100/*
3101 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3102 * The length field from io_parms must be at least 1 and indicates a number of
3103 * elements with data to write that begins with position 1 in iov array. All
3104 * data length is specified by count.
3105 */
3106int
3107SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3108 unsigned int *nbytes, struct kvec *iov, int n_vec)
3109{
3110 int rc = 0;
3111 struct smb2_write_req *req = NULL;
3112 struct smb2_write_rsp *rsp = NULL;
3113 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003114 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003115 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003116 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003117
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003118 *nbytes = 0;
3119
3120 if (n_vec < 1)
3121 return rc;
3122
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003123 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3124 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003125 if (rc)
3126 return rc;
3127
3128 if (io_parms->tcon->ses->server == NULL)
3129 return -ECONNABORTED;
3130
Steve French5a77e752018-05-09 17:43:08 -05003131 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003132 flags |= CIFS_TRANSFORM_REQ;
3133
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003134 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003135
3136 req->PersistentFileId = io_parms->persistent_fid;
3137 req->VolatileFileId = io_parms->volatile_fid;
3138 req->WriteChannelInfoOffset = 0;
3139 req->WriteChannelInfoLength = 0;
3140 req->Channel = 0;
3141 req->Length = cpu_to_le32(io_parms->length);
3142 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003143 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003144 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003145 req->RemainingBytes = 0;
3146
3147 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003148 /* 1 for Buffer */
3149 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003150
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003151 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
3152 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003153 cifs_small_buf_release(req);
3154 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003155
3156 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003157 trace_smb3_write_err(xid, req->PersistentFileId,
3158 io_parms->tcon->tid,
3159 io_parms->tcon->ses->Suid,
3160 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003161 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003162 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05003163 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003164 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05003165 trace_smb3_write_done(xid, req->PersistentFileId,
3166 io_parms->tcon->tid,
3167 io_parms->tcon->ses->Suid,
3168 io_parms->offset, *nbytes);
3169 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003170
3171 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003172 return rc;
3173}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003174
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003175static unsigned int
3176num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3177{
3178 int len;
3179 unsigned int entrycount = 0;
3180 unsigned int next_offset = 0;
3181 FILE_DIRECTORY_INFO *entryptr;
3182
3183 if (bufstart == NULL)
3184 return 0;
3185
3186 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3187
3188 while (1) {
3189 entryptr = (FILE_DIRECTORY_INFO *)
3190 ((char *)entryptr + next_offset);
3191
3192 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003193 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003194 break;
3195 }
3196
3197 len = le32_to_cpu(entryptr->FileNameLength);
3198 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003199 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3200 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003201 break;
3202 }
3203
3204 *lastentry = (char *)entryptr;
3205 entrycount++;
3206
3207 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3208 if (!next_offset)
3209 break;
3210 }
3211
3212 return entrycount;
3213}
3214
3215/*
3216 * Readdir/FindFirst
3217 */
3218int
3219SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3220 u64 persistent_fid, u64 volatile_fid, int index,
3221 struct cifs_search_info *srch_inf)
3222{
3223 struct smb2_query_directory_req *req;
3224 struct smb2_query_directory_rsp *rsp = NULL;
3225 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003226 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003227 int rc = 0;
3228 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003229 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003230 unsigned char *bufptr;
3231 struct TCP_Server_Info *server;
3232 struct cifs_ses *ses = tcon->ses;
3233 __le16 asteriks = cpu_to_le16('*');
3234 char *end_of_smb;
3235 unsigned int output_size = CIFSMaxBufSize;
3236 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003237 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003238 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003239
3240 if (ses && (ses->server))
3241 server = ses->server;
3242 else
3243 return -EIO;
3244
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003245 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3246 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003247 if (rc)
3248 return rc;
3249
Steve French5a77e752018-05-09 17:43:08 -05003250 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003251 flags |= CIFS_TRANSFORM_REQ;
3252
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003253 switch (srch_inf->info_level) {
3254 case SMB_FIND_FILE_DIRECTORY_INFO:
3255 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3256 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3257 break;
3258 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3259 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3260 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3261 break;
3262 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003263 cifs_dbg(VFS, "info level %u isn't supported\n",
3264 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003265 rc = -EINVAL;
3266 goto qdir_exit;
3267 }
3268
3269 req->FileIndex = cpu_to_le32(index);
3270 req->PersistentFileId = persistent_fid;
3271 req->VolatileFileId = volatile_fid;
3272
3273 len = 0x2;
3274 bufptr = req->Buffer;
3275 memcpy(bufptr, &asteriks, len);
3276
3277 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003278 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003279 req->FileNameLength = cpu_to_le16(len);
3280 /*
3281 * BB could be 30 bytes or so longer if we used SMB2 specific
3282 * buffer lengths, but this is safe and close enough.
3283 */
3284 output_size = min_t(unsigned int, output_size, server->maxBuf);
3285 output_size = min_t(unsigned int, output_size, 2 << 15);
3286 req->OutputBufferLength = cpu_to_le32(output_size);
3287
3288 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003289 /* 1 for Buffer */
3290 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003291
3292 iov[1].iov_base = (char *)(req->Buffer);
3293 iov[1].iov_len = len;
3294
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003295 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003296 cifs_small_buf_release(req);
3297 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003298
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003299 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003300 if (rc == -ENODATA &&
3301 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003302 srch_inf->endOfSearch = true;
3303 rc = 0;
3304 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003305 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3306 goto qdir_exit;
3307 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003308
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003309 rc = validate_iov(server,
3310 le16_to_cpu(rsp->OutputBufferOffset),
3311 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003312 info_buf_size);
3313 if (rc)
3314 goto qdir_exit;
3315
3316 srch_inf->unicode = true;
3317
3318 if (srch_inf->ntwrk_buf_start) {
3319 if (srch_inf->smallBuf)
3320 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3321 else
3322 cifs_buf_release(srch_inf->ntwrk_buf_start);
3323 }
3324 srch_inf->ntwrk_buf_start = (char *)rsp;
3325 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3326 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3327 /* 4 for rfc1002 length field */
3328 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3329 srch_inf->entries_in_buffer =
3330 num_entries(srch_inf->srch_entries_start, end_of_smb,
3331 &srch_inf->last_entry, info_buf_size);
3332 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003333 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3334 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3335 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003336 if (resp_buftype == CIFS_LARGE_BUFFER)
3337 srch_inf->smallBuf = false;
3338 else if (resp_buftype == CIFS_SMALL_BUFFER)
3339 srch_inf->smallBuf = true;
3340 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003341 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003342
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003343 return rc;
3344
3345qdir_exit:
3346 free_rsp_buf(resp_buftype, rsp);
3347 return rc;
3348}
3349
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003350static int
3351send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003352 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3353 u8 info_type, u32 additional_info, unsigned int num,
3354 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003355{
3356 struct smb2_set_info_req *req;
3357 struct smb2_set_info_rsp *rsp = NULL;
3358 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003359 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003360 int rc = 0;
3361 int resp_buftype;
3362 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003363 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003364 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003365 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003366
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003367 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003368 return -EIO;
3369
3370 if (!num)
3371 return -EINVAL;
3372
3373 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3374 if (!iov)
3375 return -ENOMEM;
3376
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003377 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003378 if (rc) {
3379 kfree(iov);
3380 return rc;
3381 }
3382
Steve French5a77e752018-05-09 17:43:08 -05003383 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003384 flags |= CIFS_TRANSFORM_REQ;
3385
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003386 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003387
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003388 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003389 req->FileInfoClass = info_class;
3390 req->PersistentFileId = persistent_fid;
3391 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003392 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003393
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003394 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003395 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003396 req->BufferLength = cpu_to_le32(*size);
3397
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003398 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003399 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003400
3401 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003402 /* 1 for Buffer */
3403 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003404
3405 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003406 le32_add_cpu(&req->BufferLength, size[i]);
3407 iov[i].iov_base = (char *)data[i];
3408 iov[i].iov_len = size[i];
3409 }
3410
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003411 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3412 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003413 cifs_small_buf_release(req);
3414 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003415
Steve Frencheccb4422018-05-17 21:16:55 -05003416 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003417 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003418 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
3419 ses->Suid, info_class, (__u32)info_type, rc);
3420 }
Steve French7d3fb242013-11-18 09:56:28 -06003421
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003422 free_rsp_buf(resp_buftype, rsp);
3423 kfree(iov);
3424 return rc;
3425}
3426
3427int
3428SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3429 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3430{
3431 struct smb2_file_rename_info info;
3432 void **data;
3433 unsigned int size[2];
3434 int rc;
3435 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3436
3437 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3438 if (!data)
3439 return -ENOMEM;
3440
3441 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3442 /* 0 = fail if target already exists */
3443 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3444 info.FileNameLength = cpu_to_le32(len);
3445
3446 data[0] = &info;
3447 size[0] = sizeof(struct smb2_file_rename_info);
3448
3449 data[1] = target_file;
3450 size[1] = len + 2 /* null */;
3451
3452 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003453 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3454 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003455 kfree(data);
3456 return rc;
3457}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003458
3459int
Steve French897fba12016-05-12 21:20:36 -05003460SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3461 u64 persistent_fid, u64 volatile_fid)
3462{
3463 __u8 delete_pending = 1;
3464 void *data;
3465 unsigned int size;
3466
3467 data = &delete_pending;
3468 size = 1; /* sizeof __u8 */
3469
3470 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003471 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3472 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003473}
3474
3475int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003476SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3477 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3478{
3479 struct smb2_file_link_info info;
3480 void **data;
3481 unsigned int size[2];
3482 int rc;
3483 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3484
3485 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3486 if (!data)
3487 return -ENOMEM;
3488
3489 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3490 /* 0 = fail if link already exists */
3491 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3492 info.FileNameLength = cpu_to_le32(len);
3493
3494 data[0] = &info;
3495 size[0] = sizeof(struct smb2_file_link_info);
3496
3497 data[1] = target_file;
3498 size[1] = len + 2 /* null */;
3499
3500 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003501 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3502 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003503 kfree(data);
3504 return rc;
3505}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003506
3507int
3508SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003509 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003510{
3511 struct smb2_file_eof_info info;
3512 void *data;
3513 unsigned int size;
3514
3515 info.EndOfFile = *eof;
3516
3517 data = &info;
3518 size = sizeof(struct smb2_file_eof_info);
3519
Steve Frenchf29ebb42014-07-19 21:44:58 -05003520 if (is_falloc)
3521 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003522 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3523 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003524 else
3525 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003526 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3527 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003528}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003529
3530int
3531SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3532 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3533{
3534 unsigned int size;
3535 size = sizeof(FILE_BASIC_INFO);
3536 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003537 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3538 0, 1, (void **)&buf, &size);
3539}
3540
3541int
3542SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3543 u64 persistent_fid, u64 volatile_fid,
3544 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3545{
3546 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3547 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3548 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003549}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003550
3551int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003552SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3553 u64 persistent_fid, u64 volatile_fid,
3554 struct smb2_file_full_ea_info *buf, int len)
3555{
3556 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3557 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3558 0, 1, (void **)&buf, &len);
3559}
3560
3561int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003562SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3563 const u64 persistent_fid, const u64 volatile_fid,
3564 __u8 oplock_level)
3565{
3566 int rc;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003567 struct smb2_oplock_break_req *req = NULL;
3568 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003569 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003570 unsigned int total_len;
3571 struct kvec iov[1];
3572 struct kvec rsp_iov;
3573 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003574
Joe Perchesf96637b2013-05-04 22:12:25 -05003575 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003576 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3577 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003578 if (rc)
3579 return rc;
3580
Steve French5a77e752018-05-09 17:43:08 -05003581 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003582 flags |= CIFS_TRANSFORM_REQ;
3583
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003584 req->VolatileFid = volatile_fid;
3585 req->PersistentFid = persistent_fid;
3586 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003587 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003588
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003589 flags |= CIFS_NO_RESP;
3590
3591 iov[0].iov_base = (char *)req;
3592 iov[0].iov_len = total_len;
3593
3594 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003595 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003596
3597 if (rc) {
3598 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003599 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003600 }
3601
3602 return rc;
3603}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003604
3605static void
3606copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3607 struct kstatfs *kst)
3608{
3609 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3610 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3611 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303612 kst->f_bfree = kst->f_bavail =
3613 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003614 return;
3615}
3616
3617static int
3618build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3619 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3620{
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003621 struct TCP_Server_Info *server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003622 int rc;
3623 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003624 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003625
Joe Perchesf96637b2013-05-04 22:12:25 -05003626 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003627
3628 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3629 return -EIO;
3630
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003631 server = tcon->ses->server;
3632
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003633 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3634 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003635 if (rc)
3636 return rc;
3637
3638 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3639 req->FileInfoClass = level;
3640 req->PersistentFileId = persistent_fid;
3641 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003642 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003643 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003644 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003645 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003646 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - server->vals->header_preamble_size);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003647
3648 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003649 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003650 return 0;
3651}
3652
3653int
3654SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3655 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3656{
3657 struct smb2_query_info_rsp *rsp = NULL;
3658 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003659 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003660 int rc = 0;
3661 int resp_buftype;
3662 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003663 struct TCP_Server_Info *server = ses->server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003664 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003665 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003666
3667 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3668 sizeof(struct smb2_fs_full_size_info),
3669 persistent_fid, volatile_fid);
3670 if (rc)
3671 return rc;
3672
Steve French5a77e752018-05-09 17:43:08 -05003673 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003674 flags |= CIFS_TRANSFORM_REQ;
3675
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003676 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003677 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003678 if (rc) {
3679 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003680 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003681 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003682 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003683
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003684 info = (struct smb2_fs_full_size_info *)(server->vals->header_preamble_size +
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003685 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003686 rc = validate_iov(server,
3687 le16_to_cpu(rsp->OutputBufferOffset),
3688 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003689 sizeof(struct smb2_fs_full_size_info));
3690 if (!rc)
3691 copy_fs_info_to_kstatfs(info, fsdata);
3692
Steve French34f62642013-10-09 02:07:00 -05003693qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003694 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003695 return rc;
3696}
3697
3698int
3699SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003700 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003701{
3702 struct smb2_query_info_rsp *rsp = NULL;
3703 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003704 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003705 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003706 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003707 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003708 struct TCP_Server_Info *server = ses->server;
Steve French34f62642013-10-09 02:07:00 -05003709 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003710 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003711
Steven French21671142013-10-09 13:36:35 -05003712 if (level == FS_DEVICE_INFORMATION) {
3713 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3714 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3715 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3716 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3717 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003718 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3719 max_len = sizeof(struct smb3_fs_ss_info);
3720 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003721 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003722 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003723 return -EINVAL;
3724 }
3725
3726 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003727 persistent_fid, volatile_fid);
3728 if (rc)
3729 return rc;
3730
Steve French5a77e752018-05-09 17:43:08 -05003731 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003732 flags |= CIFS_TRANSFORM_REQ;
3733
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003734 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003735 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003736 if (rc) {
3737 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3738 goto qfsattr_exit;
3739 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003740 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003741
3742 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3743 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003744 rc = validate_iov(server, offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05003745 if (rc)
3746 goto qfsattr_exit;
3747
3748 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003749 memcpy(&tcon->fsAttrInfo, server->vals->header_preamble_size + offset
Steve French34f62642013-10-09 02:07:00 -05003750 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003751 rsp_len, max_len));
3752 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003753 memcpy(&tcon->fsDevInfo, server->vals->header_preamble_size + offset
Steven French21671142013-10-09 13:36:35 -05003754 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003755 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3756 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003757 (server->vals->header_preamble_size + offset + (char *)&rsp->hdr);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003758 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3759 tcon->perf_sector_size =
3760 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3761 }
Steve French34f62642013-10-09 02:07:00 -05003762
3763qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003764 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003765 return rc;
3766}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003767
3768int
3769smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3770 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3771 const __u32 num_lock, struct smb2_lock_element *buf)
3772{
3773 int rc = 0;
3774 struct smb2_lock_req *req = NULL;
3775 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003776 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003777 int resp_buf_type;
3778 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003779 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003780 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003781
Joe Perchesf96637b2013-05-04 22:12:25 -05003782 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003783
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003784 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003785 if (rc)
3786 return rc;
3787
Steve French5a77e752018-05-09 17:43:08 -05003788 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003789 flags |= CIFS_TRANSFORM_REQ;
3790
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003791 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003792 req->LockCount = cpu_to_le16(num_lock);
3793
3794 req->PersistentFileId = persist_fid;
3795 req->VolatileFileId = volatile_fid;
3796
3797 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003798
3799 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003800 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003801 iov[1].iov_base = (char *)buf;
3802 iov[1].iov_len = count;
3803
3804 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003805 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3806 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003807 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003808 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003809 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003810 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003811 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
3812 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003813 }
3814
3815 return rc;
3816}
3817
3818int
3819SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3820 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3821 const __u64 length, const __u64 offset, const __u32 lock_flags,
3822 const bool wait)
3823{
3824 struct smb2_lock_element lock;
3825
3826 lock.Offset = cpu_to_le64(offset);
3827 lock.Length = cpu_to_le64(length);
3828 lock.Flags = cpu_to_le32(lock_flags);
3829 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3830 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3831
3832 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3833}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003834
3835int
3836SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3837 __u8 *lease_key, const __le32 lease_state)
3838{
3839 int rc;
3840 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003841 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003842 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003843 unsigned int total_len;
3844 struct kvec iov[1];
3845 struct kvec rsp_iov;
3846 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003847
Joe Perchesf96637b2013-05-04 22:12:25 -05003848 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003849 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3850 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003851 if (rc)
3852 return rc;
3853
Steve French5a77e752018-05-09 17:43:08 -05003854 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003855 flags |= CIFS_TRANSFORM_REQ;
3856
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003857 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003858 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003859 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003860
3861 memcpy(req->LeaseKey, lease_key, 16);
3862 req->LeaseState = lease_state;
3863
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003864 flags |= CIFS_NO_RESP;
3865
3866 iov[0].iov_base = (char *)req;
3867 iov[0].iov_len = total_len;
3868
3869 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003870 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003871
3872 if (rc) {
3873 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003874 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003875 }
3876
3877 return rc;
3878}