blob: 5e947db322a718d9af61ed8245456e0fa56550bc [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)
Steve Frenchfcef0db2018-05-19 20:45:27 -0500371#define SMB2_POSIX_EXTENSIONS_AVAILABLE cpu_to_le16(0x100)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500372
373static void
374build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
375{
376 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
377 pneg_ctxt->DataLength = cpu_to_le16(38);
378 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
379 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
380 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
381 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
382}
383
384static void
385build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
386{
387 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French23657ad2018-04-22 15:14:58 -0500388 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
389 pneg_ctxt->CipherCount = cpu_to_le16(1);
390/* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
391 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500392}
393
394static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500395build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
396{
397 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
398 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
399}
400
401static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100402assemble_neg_contexts(struct smb2_negotiate_req *req,
403 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500404{
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100405 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500406 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500407
Steve Frenchfcef0db2018-05-19 20:45:27 -0500408 *total_len += 2; /* Add 2 due to round to 8 byte boundary for 1st ctxt */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500409 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500410 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
411 *total_len += ctxt_len;
412 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100413
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500414 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500415 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
416 *total_len += ctxt_len;
417 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100418
Steve Frenchfcef0db2018-05-19 20:45:27 -0500419 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
420 *total_len += sizeof(struct smb2_posix_neg_context);
421
422 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
423 req->NegotiateContextCount = cpu_to_le16(3);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500424}
Steve French5100d8a2018-04-09 10:47:14 -0500425
426static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
427{
428 unsigned int len = le16_to_cpu(ctxt->DataLength);
429
430 /* If invalid preauth context warn but use what we requested, SHA-512 */
431 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
432 printk_once(KERN_WARNING "server sent bad preauth context\n");
433 return;
434 }
435 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
436 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
437 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
438 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
439}
440
441static int decode_encrypt_ctx(struct TCP_Server_Info *server,
442 struct smb2_encryption_neg_context *ctxt)
443{
444 unsigned int len = le16_to_cpu(ctxt->DataLength);
445
446 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
447 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
448 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
449 return -EINVAL;
450 }
451
452 if (le16_to_cpu(ctxt->CipherCount) != 1) {
453 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
454 return -EINVAL;
455 }
456 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
457 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
458 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
459 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
460 return -EINVAL;
461 }
462 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500463 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500464 return 0;
465}
466
467static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
468 struct TCP_Server_Info *server)
469{
470 struct smb2_neg_context *pctx;
471 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
472 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
473 unsigned int len_of_smb = be32_to_cpu(rsp->hdr.smb2_buf_length);
474 unsigned int len_of_ctxts, i;
475 int rc = 0;
476
477 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
478 if (len_of_smb <= offset) {
479 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
480 return -EINVAL;
481 }
482
483 len_of_ctxts = len_of_smb - offset;
484
485 for (i = 0; i < ctxt_cnt; i++) {
486 int clen;
487 /* check that offset is not beyond end of SMB */
488 if (len_of_ctxts == 0)
489 break;
490
491 if (len_of_ctxts < sizeof(struct smb2_neg_context))
492 break;
493
Steve French0d4b46b2018-04-12 20:32:13 -0500494 pctx = (struct smb2_neg_context *)(offset +
495 server->vals->header_preamble_size + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500496 clen = le16_to_cpu(pctx->DataLength);
497 if (clen > len_of_ctxts)
498 break;
499
500 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
501 decode_preauth_context(
502 (struct smb2_preauth_neg_context *)pctx);
503 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
504 rc = decode_encrypt_ctx(server,
505 (struct smb2_encryption_neg_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500506 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
507 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500508 else
509 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
510 le16_to_cpu(pctx->ContextType));
511
512 if (rc)
513 break;
514 /* offsets must be 8 byte aligned */
515 clen = (clen + 7) & ~0x7;
516 offset += clen + sizeof(struct smb2_neg_context);
517 len_of_ctxts -= clen;
518 }
519 return rc;
520}
521
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500522#else
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100523static void assemble_neg_contexts(struct smb2_negotiate_req *req,
524 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500525{
526 return;
527}
528#endif /* SMB311 */
529
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400530/*
531 *
532 * SMB2 Worker functions follow:
533 *
534 * The general structure of the worker functions is:
535 * 1) Call smb2_init (assembles SMB2 header)
536 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
537 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
538 * 4) Decode SMB2 command specific fields in the fixed length area
539 * 5) Decode variable length data area (if any for this SMB2 command type)
540 * 6) Call free smb buffer
541 * 7) return
542 *
543 */
544
545int
546SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
547{
548 struct smb2_negotiate_req *req;
549 struct smb2_negotiate_rsp *rsp;
550 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700551 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400552 int rc = 0;
553 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400554 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400555 int blob_offset, blob_length;
556 char *security_blob;
557 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100558 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400559
Joe Perchesf96637b2013-05-04 22:12:25 -0500560 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400561
Jeff Layton3534b852013-05-24 07:41:01 -0400562 if (!server) {
563 WARN(1, "%s: server is NULL!\n", __func__);
564 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400565 }
566
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100567 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400568 if (rc)
569 return rc;
570
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100571 req->sync_hdr.SessionId = 0;
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100572#ifdef CONFIG_CIFS_SMB311
573 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
574 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
575#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400576
Steve French9764c022017-09-17 10:41:35 -0500577 if (strcmp(ses->server->vals->version_string,
578 SMB3ANY_VERSION_STRING) == 0) {
579 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
580 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
581 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100582 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500583 } else if (strcmp(ses->server->vals->version_string,
584 SMBDEFAULT_VERSION_STRING) == 0) {
585 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
586 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
587 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
588 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100589 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500590 } else {
591 /* otherwise send specific dialect */
592 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
593 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100594 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500595 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400596
597 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400598 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500599 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400600 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500601 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400602 else
603 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400604
Steve Frenche4aa25e2012-10-01 12:26:22 -0500605 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400606
Steve French3c5f9be12014-05-13 13:37:45 -0700607 /* ClientGUID must be zero for SMB2.02 dialect */
608 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
609 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500610 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700611 memcpy(req->ClientGUID, server->client_guid,
612 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500613 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100614 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500615 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400616 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100617 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400618
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100619 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700620 cifs_small_buf_release(req);
621 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400622 /*
623 * No tcon so can't do
624 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
625 */
Steve French7e682f72017-08-31 21:34:24 -0500626 if (rc == -EOPNOTSUPP) {
627 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500628 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500629 " older servers\n");
630 goto neg_exit;
631 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400632 goto neg_exit;
633
Steve French9764c022017-09-17 10:41:35 -0500634 if (strcmp(ses->server->vals->version_string,
635 SMB3ANY_VERSION_STRING) == 0) {
636 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
637 cifs_dbg(VFS,
638 "SMB2 dialect returned but not requested\n");
639 return -EIO;
640 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
641 cifs_dbg(VFS,
642 "SMB2.1 dialect returned but not requested\n");
643 return -EIO;
644 }
645 } else if (strcmp(ses->server->vals->version_string,
646 SMBDEFAULT_VERSION_STRING) == 0) {
647 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
648 cifs_dbg(VFS,
649 "SMB2 dialect returned but not requested\n");
650 return -EIO;
651 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
652 /* ops set to 3.0 by default for default so update */
653 ses->server->ops = &smb21_operations;
654 }
Steve French590d08d2017-09-19 11:43:47 -0500655 } else if (le16_to_cpu(rsp->DialectRevision) !=
656 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500657 /* if requested single dialect ensure returned dialect matched */
658 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500659 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500660 return -EIO;
661 }
662
Joe Perchesf96637b2013-05-04 22:12:25 -0500663 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400664
Steve Frenche4aa25e2012-10-01 12:26:22 -0500665 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500666 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500667 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500668 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500669 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500670 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500671 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
672 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600673#ifdef CONFIG_CIFS_SMB311
674 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
675 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
676#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400677 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500678 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500679 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400680 rc = -EIO;
681 goto neg_exit;
682 }
683 server->dialect = le16_to_cpu(rsp->DialectRevision);
684
Steve French9764c022017-09-17 10:41:35 -0500685 /* BB: add check that dialect was valid given dialect(s) we asked for */
686
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100687#ifdef CONFIG_CIFS_SMB311
688 /*
689 * Keep a copy of the hash after negprot. This hash will be
690 * the starting hash value for all sessions made from this
691 * server.
692 */
693 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
694 SMB2_PREAUTH_HASH_SIZE);
695#endif
Jeff Laytone598d1d82013-05-26 07:00:59 -0400696 /* SMB2 only has an extended negflavor */
697 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400698 /* set it to the maximum buffer size value we can send with 1 credit */
699 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
700 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400701 server->max_read = le32_to_cpu(rsp->MaxReadSize);
702 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400703 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500704 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
705 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
706 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400707 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400708 /* Internal types */
709 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400710
711 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
712 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500713 /*
714 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
715 * for us will be
716 * ses->sectype = RawNTLMSSP;
717 * but for time being this is our only auth choice so doesn't matter.
718 * We just found a server which sets blob length to zero expecting raw.
719 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700720 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500721 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700722 server->sec_ntlmssp = true;
723 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700724
Jeff Layton38d77c52013-05-26 07:01:00 -0400725 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400726 if (rc)
727 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500728 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500729 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500730 if (rc == 1)
731 rc = 0;
732 else if (rc == 0)
733 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400734 }
Steve French5100d8a2018-04-09 10:47:14 -0500735
736#ifdef CONFIG_CIFS_SMB311
737 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
738 if (rsp->NegotiateContextCount)
739 rc = smb311_decode_neg_context(rsp, server);
740 else
741 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
742 }
743#endif /* CONFIG_CIFS_SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400744neg_exit:
745 free_rsp_buf(resp_buftype, rsp);
746 return rc;
747}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400748
Steve Frenchff1c0382013-11-19 23:44:46 -0600749int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
750{
Long Li2796d302018-04-25 11:30:04 -0700751 int rc;
752 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200753 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600754 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500755 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600756
757 cifs_dbg(FYI, "validate negotiate\n");
758
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100759 /* In SMB3.11 preauth integrity supersedes validate negotiate */
760 if (tcon->ses->server->dialect == SMB311_PROT_ID)
761 return 0;
762
Steve Frenchff1c0382013-11-19 23:44:46 -0600763 /*
764 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500765 * can not sign it (ie are not known user). Even if signing is not
766 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600767 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500768 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600769 */
Steve French0603c962017-09-20 19:57:18 -0500770 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600771 return 0; /* validation requires signing */
772
Steve French0603c962017-09-20 19:57:18 -0500773 if (tcon->ses->user_name == NULL) {
774 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
775 return 0; /* validation requires signing */
776 }
777
778 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
779 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
780
Long Li2796d302018-04-25 11:30:04 -0700781 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
782 if (!pneg_inbuf)
783 return -ENOMEM;
784
785 pneg_inbuf->Capabilities =
Steve Frenchff1c0382013-11-19 23:44:46 -0600786 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Long Li2796d302018-04-25 11:30:04 -0700787 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100788 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600789
790 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -0700791 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600792 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
793 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -0700794 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600795 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
796 else
Long Li2796d302018-04-25 11:30:04 -0700797 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600798
Steve French9764c022017-09-17 10:41:35 -0500799
800 if (strcmp(tcon->ses->server->vals->version_string,
801 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700802 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
803 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
804 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -0500805 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -0700806 inbuflen = sizeof(*pneg_inbuf) -
807 sizeof(pneg_inbuf->Dialects[0]);
Steve French9764c022017-09-17 10:41:35 -0500808 } else if (strcmp(tcon->ses->server->vals->version_string,
809 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700810 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
811 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
812 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
813 pneg_inbuf->DialectCount = cpu_to_le16(3);
Steve French9764c022017-09-17 10:41:35 -0500814 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -0700815 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -0500816 } else {
817 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -0700818 pneg_inbuf->Dialects[0] =
Steve French9764c022017-09-17 10:41:35 -0500819 cpu_to_le16(tcon->ses->server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -0700820 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -0500821 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -0700822 inbuflen = sizeof(*pneg_inbuf) -
823 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -0500824 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600825
826 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
827 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Long Li2796d302018-04-25 11:30:04 -0700828 (char *)pneg_inbuf, inbuflen, (char **)&pneg_rsp, &rsplen);
Steve Frenchff1c0382013-11-19 23:44:46 -0600829
830 if (rc != 0) {
831 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -0700832 rc = -EIO;
833 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -0600834 }
835
Long Li2796d302018-04-25 11:30:04 -0700836 rc = -EIO;
837 if (rsplen != sizeof(*pneg_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500838 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
839 rsplen);
840
841 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -0700842 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
843 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -0600844 }
845
846 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +0100847 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600848 goto vneg_out;
849
850 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
851 goto vneg_out;
852
853 /* do not validate server guid because not saved at negprot time yet */
854
855 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
856 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
857 goto vneg_out;
858
859 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -0700860 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600861 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -0700862 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -0600863
864vneg_out:
865 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -0700866out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200867 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -0700868out_free_inbuf:
869 kfree(pneg_inbuf);
870 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -0600871}
872
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530873enum securityEnum
874smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
875{
876 switch (requested) {
877 case Kerberos:
878 case RawNTLMSSP:
879 return requested;
880 case NTLMv2:
881 return RawNTLMSSP;
882 case Unspecified:
883 if (server->sec_ntlmssp &&
884 (global_secflags & CIFSSEC_MAY_NTLMSSP))
885 return RawNTLMSSP;
886 if ((server->sec_kerberos || server->sec_mskerberos) &&
887 (global_secflags & CIFSSEC_MAY_KRB5))
888 return Kerberos;
889 /* Fallthrough */
890 default:
891 return Unspecified;
892 }
893}
894
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100895struct SMB2_sess_data {
896 unsigned int xid;
897 struct cifs_ses *ses;
898 struct nls_table *nls_cp;
899 void (*func)(struct SMB2_sess_data *);
900 int result;
901 u64 previous_session;
902
903 /* we will send the SMB in three pieces:
904 * a fixed length beginning part, an optional
905 * SPNEGO blob (which can be zero length), and a
906 * last part which will include the strings
907 * and rest of bcc area. This allows us to avoid
908 * a large buffer 17K allocation
909 */
910 int buf0_type;
911 struct kvec iov[2];
912};
913
914static int
915SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
916{
917 int rc;
918 struct cifs_ses *ses = sess_data->ses;
919 struct smb2_sess_setup_req *req;
920 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100921 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100922
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100923 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
924 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100925 if (rc)
926 return rc;
927
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700928 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100929 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100930
931 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
932 req->PreviousSessionId = sess_data->previous_session;
933
934 req->Flags = 0; /* MBZ */
935 /* to enable echos and oplocks */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100936 req->sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100937
938 /* only one of SMB2 signing flags may be set in SMB2 request */
939 if (server->sign)
940 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
941 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
942 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
943 else
944 req->SecurityMode = 0;
945
946 req->Capabilities = 0;
947 req->Channel = 0; /* MBZ */
948
949 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100950 /* 1 for pad */
951 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100952 /*
953 * This variable will be used to clear the buffer
954 * allocated above in case of any error in the calling function.
955 */
956 sess_data->buf0_type = CIFS_SMALL_BUFFER;
957
958 return 0;
959}
960
961static void
962SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
963{
964 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
965 sess_data->buf0_type = CIFS_NO_BUFFER;
966}
967
968static int
969SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
970{
971 int rc;
972 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700973 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100974
975 /* Testing shows that buffer offset must be at location of Buffer[0] */
976 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100977 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100978 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
979
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100980 /* BB add code to build os and lm fields */
981
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100982 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
983 sess_data->iov, 2,
984 &sess_data->buf0_type,
985 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700986 cifs_small_buf_release(sess_data->iov[0].iov_base);
987 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100988
989 return rc;
990}
991
992static int
993SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
994{
995 int rc = 0;
996 struct cifs_ses *ses = sess_data->ses;
997
998 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800999 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001000 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001001 if (rc) {
1002 cifs_dbg(FYI,
1003 "SMB3 session key generation failed\n");
1004 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001005 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001006 }
1007 }
1008 if (!ses->server->session_estab) {
1009 ses->server->sequence_number = 0x2;
1010 ses->server->session_estab = true;
1011 }
1012 mutex_unlock(&ses->server->srv_mutex);
1013
1014 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1015 spin_lock(&GlobalMid_Lock);
1016 ses->status = CifsGood;
1017 ses->need_reconnect = false;
1018 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001019 return rc;
1020}
1021
1022#ifdef CONFIG_CIFS_UPCALL
1023static void
1024SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1025{
1026 int rc;
1027 struct cifs_ses *ses = sess_data->ses;
1028 struct cifs_spnego_msg *msg;
1029 struct key *spnego_key = NULL;
1030 struct smb2_sess_setup_rsp *rsp = NULL;
1031
1032 rc = SMB2_sess_alloc_buffer(sess_data);
1033 if (rc)
1034 goto out;
1035
1036 spnego_key = cifs_get_spnego_key(ses);
1037 if (IS_ERR(spnego_key)) {
1038 rc = PTR_ERR(spnego_key);
1039 spnego_key = NULL;
1040 goto out;
1041 }
1042
1043 msg = spnego_key->payload.data[0];
1044 /*
1045 * check version field to make sure that cifs.upcall is
1046 * sending us a response in an expected form
1047 */
1048 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1049 cifs_dbg(VFS,
1050 "bad cifs.upcall version. Expected %d got %d",
1051 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1052 rc = -EKEYREJECTED;
1053 goto out_put_spnego_key;
1054 }
1055
1056 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1057 GFP_KERNEL);
1058 if (!ses->auth_key.response) {
1059 cifs_dbg(VFS,
1060 "Kerberos can't allocate (%u bytes) memory",
1061 msg->sesskey_len);
1062 rc = -ENOMEM;
1063 goto out_put_spnego_key;
1064 }
1065 ses->auth_key.len = msg->sesskey_len;
1066
1067 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1068 sess_data->iov[1].iov_len = msg->secblob_len;
1069
1070 rc = SMB2_sess_sendreceive(sess_data);
1071 if (rc)
1072 goto out_put_spnego_key;
1073
1074 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001075 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001076
1077 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001078
1079 rc = SMB2_sess_establish_session(sess_data);
1080out_put_spnego_key:
1081 key_invalidate(spnego_key);
1082 key_put(spnego_key);
1083out:
1084 sess_data->result = rc;
1085 sess_data->func = NULL;
1086 SMB2_sess_free_buffer(sess_data);
1087}
1088#else
1089static void
1090SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1091{
1092 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1093 sess_data->result = -EOPNOTSUPP;
1094 sess_data->func = NULL;
1095}
1096#endif
1097
Sachin Prabhu166cea42016-10-07 19:11:22 +01001098static void
1099SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1100
1101static void
1102SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1103{
1104 int rc;
1105 struct cifs_ses *ses = sess_data->ses;
1106 struct smb2_sess_setup_rsp *rsp = NULL;
1107 char *ntlmssp_blob = NULL;
1108 bool use_spnego = false; /* else use raw ntlmssp */
1109 u16 blob_length = 0;
1110
1111 /*
1112 * If memory allocation is successful, caller of this function
1113 * frees it.
1114 */
1115 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1116 if (!ses->ntlmssp) {
1117 rc = -ENOMEM;
1118 goto out_err;
1119 }
1120 ses->ntlmssp->sesskey_per_smbsess = true;
1121
1122 rc = SMB2_sess_alloc_buffer(sess_data);
1123 if (rc)
1124 goto out_err;
1125
1126 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1127 GFP_KERNEL);
1128 if (ntlmssp_blob == NULL) {
1129 rc = -ENOMEM;
1130 goto out;
1131 }
1132
1133 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1134 if (use_spnego) {
1135 /* BB eventually need to add this */
1136 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1137 rc = -EOPNOTSUPP;
1138 goto out;
1139 } else {
1140 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1141 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1142 }
1143 sess_data->iov[1].iov_base = ntlmssp_blob;
1144 sess_data->iov[1].iov_len = blob_length;
1145
1146 rc = SMB2_sess_sendreceive(sess_data);
1147 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1148
1149 /* If true, rc here is expected and not an error */
1150 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001151 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001152 rc = 0;
1153
1154 if (rc)
1155 goto out;
1156
Ronnie Sahlberg9fdd2e02018-04-09 18:06:27 +10001157 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - ses->server->vals->header_preamble_size !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001158 le16_to_cpu(rsp->SecurityBufferOffset)) {
1159 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1160 le16_to_cpu(rsp->SecurityBufferOffset));
1161 rc = -EIO;
1162 goto out;
1163 }
1164 rc = decode_ntlmssp_challenge(rsp->Buffer,
1165 le16_to_cpu(rsp->SecurityBufferLength), ses);
1166 if (rc)
1167 goto out;
1168
1169 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1170
1171
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001172 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001173 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001174
1175out:
1176 kfree(ntlmssp_blob);
1177 SMB2_sess_free_buffer(sess_data);
1178 if (!rc) {
1179 sess_data->result = 0;
1180 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1181 return;
1182 }
1183out_err:
1184 kfree(ses->ntlmssp);
1185 ses->ntlmssp = NULL;
1186 sess_data->result = rc;
1187 sess_data->func = NULL;
1188}
1189
1190static void
1191SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1192{
1193 int rc;
1194 struct cifs_ses *ses = sess_data->ses;
1195 struct smb2_sess_setup_req *req;
1196 struct smb2_sess_setup_rsp *rsp = NULL;
1197 unsigned char *ntlmssp_blob = NULL;
1198 bool use_spnego = false; /* else use raw ntlmssp */
1199 u16 blob_length = 0;
1200
1201 rc = SMB2_sess_alloc_buffer(sess_data);
1202 if (rc)
1203 goto out;
1204
1205 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001206 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001207
1208 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1209 sess_data->nls_cp);
1210 if (rc) {
1211 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1212 goto out;
1213 }
1214
1215 if (use_spnego) {
1216 /* BB eventually need to add this */
1217 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1218 rc = -EOPNOTSUPP;
1219 goto out;
1220 }
1221 sess_data->iov[1].iov_base = ntlmssp_blob;
1222 sess_data->iov[1].iov_len = blob_length;
1223
1224 rc = SMB2_sess_sendreceive(sess_data);
1225 if (rc)
1226 goto out;
1227
1228 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1229
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001230 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001231 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001232
1233 rc = SMB2_sess_establish_session(sess_data);
1234out:
1235 kfree(ntlmssp_blob);
1236 SMB2_sess_free_buffer(sess_data);
1237 kfree(ses->ntlmssp);
1238 ses->ntlmssp = NULL;
1239 sess_data->result = rc;
1240 sess_data->func = NULL;
1241}
1242
1243static int
1244SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1245{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301246 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001247
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301248 type = smb2_select_sectype(ses->server, ses->sectype);
1249 cifs_dbg(FYI, "sess setup type %d\n", type);
1250 if (type == Unspecified) {
1251 cifs_dbg(VFS,
1252 "Unable to select appropriate authentication method!");
1253 return -EINVAL;
1254 }
1255
1256 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001257 case Kerberos:
1258 sess_data->func = SMB2_auth_kerberos;
1259 break;
1260 case RawNTLMSSP:
1261 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1262 break;
1263 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301264 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001265 return -EOPNOTSUPP;
1266 }
1267
1268 return 0;
1269}
1270
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001271int
1272SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1273 const struct nls_table *nls_cp)
1274{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001275 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001276 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001277 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001278
Joe Perchesf96637b2013-05-04 22:12:25 -05001279 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001280
Jeff Layton3534b852013-05-24 07:41:01 -04001281 if (!server) {
1282 WARN(1, "%s: server is NULL!\n", __func__);
1283 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001284 }
1285
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001286 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1287 if (!sess_data)
1288 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001289
1290 rc = SMB2_select_sec(ses, sess_data);
1291 if (rc)
1292 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001293 sess_data->xid = xid;
1294 sess_data->ses = ses;
1295 sess_data->buf0_type = CIFS_NO_BUFFER;
1296 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001297
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001298#ifdef CONFIG_CIFS_SMB311
1299 /*
1300 * Initialize the session hash with the server one.
1301 */
1302 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1303 SMB2_PREAUTH_HASH_SIZE);
1304#endif
1305
Sachin Prabhu166cea42016-10-07 19:11:22 +01001306 while (sess_data->func)
1307 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001308
Steve Frenchc721c382017-09-19 18:40:03 -05001309 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1310 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001311 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001312out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001313 kfree(sess_data);
1314 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001315}
1316
1317int
1318SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1319{
1320 struct smb2_logoff_req *req; /* response is also trivial struct */
1321 int rc = 0;
1322 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001323 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001324 unsigned int total_len;
1325 struct kvec iov[1];
1326 struct kvec rsp_iov;
1327 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001328
Joe Perchesf96637b2013-05-04 22:12:25 -05001329 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001330
1331 if (ses && (ses->server))
1332 server = ses->server;
1333 else
1334 return -EIO;
1335
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001336 /* no need to send SMB logoff if uid already closed due to reconnect */
1337 if (ses->need_reconnect)
1338 goto smb2_session_already_dead;
1339
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001340 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001341 if (rc)
1342 return rc;
1343
1344 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001345 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001346
1347 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1348 flags |= CIFS_TRANSFORM_REQ;
1349 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001350 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001351
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001352 flags |= CIFS_NO_RESP;
1353
1354 iov[0].iov_base = (char *)req;
1355 iov[0].iov_len = total_len;
1356
1357 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001358 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001359 /*
1360 * No tcon so can't do
1361 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1362 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001363
1364smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001365 return rc;
1366}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001367
1368static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1369{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001370 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001371}
1372
1373#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1374
Steve Frenchde9f68df2013-11-15 11:26:24 -06001375/* These are similar values to what Windows uses */
1376static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1377{
1378 tcon->max_chunks = 256;
1379 tcon->max_bytes_chunk = 1048576;
1380 tcon->max_bytes_copy = 16777216;
1381}
1382
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001383int
1384SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1385 struct cifs_tcon *tcon, const struct nls_table *cp)
1386{
1387 struct smb2_tree_connect_req *req;
1388 struct smb2_tree_connect_rsp *rsp = NULL;
1389 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001390 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001391 int rc = 0;
1392 int resp_buftype;
1393 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001394 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001395 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001396 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001397
Joe Perchesf96637b2013-05-04 22:12:25 -05001398 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001399
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001400 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001401 return -EIO;
1402
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001403 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1404 if (unc_path == NULL)
1405 return -ENOMEM;
1406
1407 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1408 unc_path_len *= 2;
1409 if (unc_path_len < 2) {
1410 kfree(unc_path);
1411 return -EINVAL;
1412 }
1413
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001414 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001415 tcon->tid = 0;
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001416
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001417 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1418 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001419 if (rc) {
1420 kfree(unc_path);
1421 return rc;
1422 }
1423
Steve French5a77e752018-05-09 17:43:08 -05001424 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001425 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001426
1427 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001428 /* 1 for pad */
1429 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001430
1431 /* Testing shows that buffer offset must be at location of Buffer[0] */
1432 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001433 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001434 req->PathLength = cpu_to_le16(unc_path_len - 2);
1435 iov[1].iov_base = unc_path;
1436 iov[1].iov_len = unc_path_len;
1437
Steve French6188f282018-03-13 02:29:36 -05001438 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1439 if ((ses->server->dialect == SMB311_PROT_ID) &&
Steve French5a77e752018-05-09 17:43:08 -05001440 !smb3_encryption_required(tcon))
Steve French6188f282018-03-13 02:29:36 -05001441 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1442
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001443 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001444 cifs_small_buf_release(req);
1445 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001446
1447 if (rc != 0) {
1448 if (tcon) {
1449 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1450 tcon->need_reconnect = true;
1451 }
1452 goto tcon_error_exit;
1453 }
1454
Christophe JAILLETcd123002017-05-12 17:59:32 +02001455 switch (rsp->ShareType) {
1456 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001457 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001458 break;
1459 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001460 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001461 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001462 break;
1463 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001464 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001465 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001466 break;
1467 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001468 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001469 rc = -EOPNOTSUPP;
1470 goto tcon_error_exit;
1471 }
1472
1473 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001474 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001475 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1476 tcon->tidStatus = CifsGood;
1477 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001478 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001479 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001480
1481 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1482 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001483 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001484
1485 if (tcon->seal &&
1486 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1487 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1488
Steve Frenchde9f68df2013-11-15 11:26:24 -06001489 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001490 if (tcon->ses->server->ops->validate_negotiate)
1491 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001492tcon_exit:
1493 free_rsp_buf(resp_buftype, rsp);
1494 kfree(unc_path);
1495 return rc;
1496
1497tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001498 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001499 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001500 }
1501 goto tcon_exit;
1502}
1503
1504int
1505SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1506{
1507 struct smb2_tree_disconnect_req *req; /* response is trivial */
1508 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001509 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001510 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001511 unsigned int total_len;
1512 struct kvec iov[1];
1513 struct kvec rsp_iov;
1514 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001515
Joe Perchesf96637b2013-05-04 22:12:25 -05001516 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001517
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001518 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001519 return -EIO;
1520
1521 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1522 return 0;
1523
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001524 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1525 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001526 if (rc)
1527 return rc;
1528
Steve French5a77e752018-05-09 17:43:08 -05001529 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001530 flags |= CIFS_TRANSFORM_REQ;
1531
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001532 flags |= CIFS_NO_RESP;
1533
1534 iov[0].iov_base = (char *)req;
1535 iov[0].iov_len = total_len;
1536
1537 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001538 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001539 if (rc)
1540 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1541
1542 return rc;
1543}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001544
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001545
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001546static struct create_durable *
1547create_durable_buf(void)
1548{
1549 struct create_durable *buf;
1550
1551 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1552 if (!buf)
1553 return NULL;
1554
1555 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001556 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001557 buf->ccontext.DataLength = cpu_to_le32(16);
1558 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1559 (struct create_durable, Name));
1560 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001561 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001562 buf->Name[0] = 'D';
1563 buf->Name[1] = 'H';
1564 buf->Name[2] = 'n';
1565 buf->Name[3] = 'Q';
1566 return buf;
1567}
1568
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001569static struct create_durable *
1570create_reconnect_durable_buf(struct cifs_fid *fid)
1571{
1572 struct create_durable *buf;
1573
1574 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1575 if (!buf)
1576 return NULL;
1577
1578 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1579 (struct create_durable, Data));
1580 buf->ccontext.DataLength = cpu_to_le32(16);
1581 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1582 (struct create_durable, Name));
1583 buf->ccontext.NameLength = cpu_to_le16(4);
1584 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1585 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001586 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001587 buf->Name[0] = 'D';
1588 buf->Name[1] = 'H';
1589 buf->Name[2] = 'n';
1590 buf->Name[3] = 'C';
1591 return buf;
1592}
1593
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001594static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001595parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001596 unsigned int *epoch, char *lease_key)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001597{
1598 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001599 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001600 unsigned int next;
1601 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001602 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001603
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001604 data_offset = (char *)rsp + server->vals->header_preamble_size + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001605 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001606 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001607 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001608 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001609 if (le16_to_cpu(cc->NameLength) == 4 &&
1610 strncmp(name, "RqLs", 4) == 0)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001611 return server->ops->parse_lease_buf(cc, epoch,
1612 lease_key);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001613
1614 next = le32_to_cpu(cc->Next);
1615 if (!next)
1616 break;
1617 remaining -= next;
1618 cc = (struct create_context *)((char *)cc + next);
1619 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001620
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001621 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001622}
1623
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001624static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001625add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1626 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001627{
1628 struct smb2_create_req *req = iov[0].iov_base;
1629 unsigned int num = *num_iovec;
1630
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001631 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001632 if (iov[num].iov_base == NULL)
1633 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001634 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001635 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1636 if (!req->CreateContextsOffset)
1637 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001638 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001639 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001640 le32_add_cpu(&req->CreateContextsLength,
1641 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001642 *num_iovec = num + 1;
1643 return 0;
1644}
1645
Steve Frenchb56eae42015-11-03 09:26:27 -06001646static struct create_durable_v2 *
1647create_durable_v2_buf(struct cifs_fid *pfid)
1648{
1649 struct create_durable_v2 *buf;
1650
1651 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1652 if (!buf)
1653 return NULL;
1654
1655 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1656 (struct create_durable_v2, dcontext));
1657 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1658 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1659 (struct create_durable_v2, Name));
1660 buf->ccontext.NameLength = cpu_to_le16(4);
1661
1662 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1663 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001664 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001665 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1666
1667 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1668 buf->Name[0] = 'D';
1669 buf->Name[1] = 'H';
1670 buf->Name[2] = '2';
1671 buf->Name[3] = 'Q';
1672 return buf;
1673}
1674
1675static struct create_durable_handle_reconnect_v2 *
1676create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1677{
1678 struct create_durable_handle_reconnect_v2 *buf;
1679
1680 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1681 GFP_KERNEL);
1682 if (!buf)
1683 return NULL;
1684
1685 buf->ccontext.DataOffset =
1686 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1687 dcontext));
1688 buf->ccontext.DataLength =
1689 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1690 buf->ccontext.NameOffset =
1691 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1692 Name));
1693 buf->ccontext.NameLength = cpu_to_le16(4);
1694
1695 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1696 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1697 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1698 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1699
1700 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1701 buf->Name[0] = 'D';
1702 buf->Name[1] = 'H';
1703 buf->Name[2] = '2';
1704 buf->Name[3] = 'C';
1705 return buf;
1706}
1707
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001708static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001709add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001710 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001711{
1712 struct smb2_create_req *req = iov[0].iov_base;
1713 unsigned int num = *num_iovec;
1714
Steve Frenchb56eae42015-11-03 09:26:27 -06001715 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1716 if (iov[num].iov_base == NULL)
1717 return -ENOMEM;
1718 iov[num].iov_len = sizeof(struct create_durable_v2);
1719 if (!req->CreateContextsOffset)
1720 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001721 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001722 iov[1].iov_len);
1723 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001724 *num_iovec = num + 1;
1725 return 0;
1726}
1727
1728static int
1729add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1730 struct cifs_open_parms *oparms)
1731{
1732 struct smb2_create_req *req = iov[0].iov_base;
1733 unsigned int num = *num_iovec;
1734
1735 /* indicate that we don't need to relock the file */
1736 oparms->reconnect = false;
1737
1738 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1739 if (iov[num].iov_base == NULL)
1740 return -ENOMEM;
1741 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1742 if (!req->CreateContextsOffset)
1743 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001744 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001745 iov[1].iov_len);
1746 le32_add_cpu(&req->CreateContextsLength,
1747 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001748 *num_iovec = num + 1;
1749 return 0;
1750}
1751
1752static int
1753add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1754 struct cifs_open_parms *oparms, bool use_persistent)
1755{
1756 struct smb2_create_req *req = iov[0].iov_base;
1757 unsigned int num = *num_iovec;
1758
1759 if (use_persistent) {
1760 if (oparms->reconnect)
1761 return add_durable_reconnect_v2_context(iov, num_iovec,
1762 oparms);
1763 else
1764 return add_durable_v2_context(iov, num_iovec, oparms);
1765 }
1766
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001767 if (oparms->reconnect) {
1768 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1769 /* indicate that we don't need to relock the file */
1770 oparms->reconnect = false;
1771 } else
1772 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001773 if (iov[num].iov_base == NULL)
1774 return -ENOMEM;
1775 iov[num].iov_len = sizeof(struct create_durable);
1776 if (!req->CreateContextsOffset)
1777 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001778 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001779 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001780 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001781 *num_iovec = num + 1;
1782 return 0;
1783}
1784
Aurelien Aptelf0712922017-02-22 14:47:17 +01001785static int
1786alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1787 const char *treename, const __le16 *path)
1788{
1789 int treename_len, path_len;
1790 struct nls_table *cp;
1791 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1792
1793 /*
1794 * skip leading "\\"
1795 */
1796 treename_len = strlen(treename);
1797 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1798 return -EINVAL;
1799
1800 treename += 2;
1801 treename_len -= 2;
1802
1803 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1804
1805 /*
1806 * make room for one path separator between the treename and
1807 * path
1808 */
1809 *out_len = treename_len + 1 + path_len;
1810
1811 /*
1812 * final path needs to be null-terminated UTF16 with a
1813 * size aligned to 8
1814 */
1815
1816 *out_size = roundup((*out_len+1)*2, 8);
1817 *out_path = kzalloc(*out_size, GFP_KERNEL);
1818 if (!*out_path)
1819 return -ENOMEM;
1820
1821 cp = load_nls_default();
1822 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1823 UniStrcat(*out_path, sep);
1824 UniStrcat(*out_path, path);
1825 unload_nls(cp);
1826
1827 return 0;
1828}
1829
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001830int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001831SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001832 __u8 *oplock, struct smb2_file_all_info *buf,
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001833 struct kvec *err_iov)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001834{
1835 struct smb2_create_req *req;
1836 struct smb2_create_rsp *rsp;
1837 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001838 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001839 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001840 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001841 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001842 int resp_buftype;
1843 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001844 __le16 *copy_path = NULL;
1845 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001846 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001847 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001848 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001849 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001850 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001851 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001852
Joe Perchesf96637b2013-05-04 22:12:25 -05001853 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001854
1855 if (ses && (ses->server))
1856 server = ses->server;
1857 else
1858 return -EIO;
1859
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001860 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1861
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001862 if (rc)
1863 return rc;
1864
Steve French5a77e752018-05-09 17:43:08 -05001865 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001866 flags |= CIFS_TRANSFORM_REQ;
1867
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001868 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001869 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001870 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1871 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001872
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001873 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001874 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001875 /* File attributes ignored on open (used in create though) */
1876 req->FileAttributes = cpu_to_le32(file_attributes);
1877 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001878 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1879 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001880
1881 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001882 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001883 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001884
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001885 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01001886
1887 /* [MS-SMB2] 2.2.13 NameOffset:
1888 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1889 * the SMB2 header, the file name includes a prefix that will
1890 * be processed during DFS name normalization as specified in
1891 * section 3.3.5.9. Otherwise, the file name is relative to
1892 * the share that is identified by the TreeId in the SMB2
1893 * header.
1894 */
1895 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1896 int name_len;
1897
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001898 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001899 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1900 &name_len,
1901 tcon->treeName, path);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001902 if (rc) {
1903 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001904 return rc;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001905 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001906 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001907 uni_path_len = copy_size;
1908 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001909 } else {
1910 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1911 /* MUST set path len (NameLength) to 0 opening root of share */
1912 req->NameLength = cpu_to_le16(uni_path_len - 2);
1913 if (uni_path_len % 8 != 0) {
1914 copy_size = roundup(uni_path_len, 8);
1915 copy_path = kzalloc(copy_size, GFP_KERNEL);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001916 if (!copy_path) {
1917 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001918 return -ENOMEM;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001919 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001920 memcpy((char *)copy_path, (const char *)path,
1921 uni_path_len);
1922 uni_path_len = copy_size;
1923 path = copy_path;
1924 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001925 }
1926
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001927 iov[1].iov_len = uni_path_len;
1928 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001929
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001930 if (!server->oplocks)
1931 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1932
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001933 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001934 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1935 req->RequestedOplockLevel = *oplock;
1936 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001937 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001938 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001939 cifs_small_buf_release(req);
1940 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001941 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001942 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001943 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001944 }
1945
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001946 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1947 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001948 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001949 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001950 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001951 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001952 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001953 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001954
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001955 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001956 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001957 if (rc) {
1958 cifs_small_buf_release(req);
1959 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001960 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001961 return rc;
1962 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001963 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001964 }
1965
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001966 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1967 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001968 cifs_small_buf_release(req);
1969 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001970
1971 if (rc != 0) {
1972 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001973 if (err_iov && rsp) {
1974 *err_iov = rsp_iov;
1975 resp_buftype = CIFS_NO_BUFFER;
1976 rsp = NULL;
1977 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001978 goto creat_exit;
1979 }
1980
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001981 oparms->fid->persistent_fid = rsp->PersistentFileId;
1982 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001983
1984 if (buf) {
1985 memcpy(buf, &rsp->CreationTime, 32);
1986 buf->AllocationSize = rsp->AllocationSize;
1987 buf->EndOfFile = rsp->EndofFile;
1988 buf->Attributes = rsp->FileAttributes;
1989 buf->NumberOfLinks = cpu_to_le32(1);
1990 buf->DeletePending = 0;
1991 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001992
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001993 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001994 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch,
1995 oparms->fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001996 else
1997 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001998creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001999 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04002000 kfree(lc_buf);
2001 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002002 free_rsp_buf(resp_buftype, rsp);
2003 return rc;
2004}
2005
Steve French4a72daf2013-06-25 00:20:49 -05002006/*
2007 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2008 */
2009int
2010SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002011 u64 volatile_fid, u32 opcode, bool is_fsctl,
Aurelien Aptel51146622017-02-28 15:08:41 +01002012 char *in_data, u32 indatalen,
2013 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05002014{
2015 struct smb2_ioctl_req *req;
2016 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002017 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05002018 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05002019 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002020 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002021 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002022 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002023 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002024 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002025 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05002026
2027 cifs_dbg(FYI, "SMB2 IOCTL\n");
2028
Steve French3d1a3742014-08-11 21:05:25 -05002029 if (out_data != NULL)
2030 *out_data = NULL;
2031
Steve French4a72daf2013-06-25 00:20:49 -05002032 /* zero out returned data len, in case of error */
2033 if (plen)
2034 *plen = 0;
2035
Steve French8e353102015-03-26 19:47:02 -05002036 if (tcon)
2037 ses = tcon->ses;
2038 else
2039 return -EIO;
2040
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002041 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05002042 return -EIO;
2043
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002044 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002045 if (rc)
2046 return rc;
2047
Steve French5a77e752018-05-09 17:43:08 -05002048 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002049 flags |= CIFS_TRANSFORM_REQ;
2050
Steve French4a72daf2013-06-25 00:20:49 -05002051 req->CtlCode = cpu_to_le32(opcode);
2052 req->PersistentFileId = persistent_fid;
2053 req->VolatileFileId = volatile_fid;
2054
2055 if (indatalen) {
2056 req->InputCount = cpu_to_le32(indatalen);
2057 /* do not set InputOffset if no input data */
2058 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002059 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05002060 iov[1].iov_base = in_data;
2061 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002062 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05002063 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002064 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05002065
2066 req->OutputOffset = 0;
2067 req->OutputCount = 0; /* MBZ */
2068
2069 /*
2070 * Could increase MaxOutputResponse, but that would require more
2071 * than one credit. Windows typically sets this smaller, but for some
2072 * ioctls it may be useful to allow server to send more. No point
2073 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05002074 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2075 * (by default, note that it can be overridden to make max larger)
2076 * in responses (except for read responses which can be bigger.
2077 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05002078 */
Steve French7db0a6e2017-05-03 21:12:20 -05002079 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05002080
2081 if (is_fsctl)
2082 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2083 else
2084 req->Flags = 0;
2085
2086 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05002087
Steve French7ff8d452013-10-14 00:44:19 -05002088 /*
2089 * If no input data, the size of ioctl struct in
2090 * protocol spec still includes a 1 byte data buffer,
2091 * but if input data passed to ioctl, we do not
2092 * want to double count this, so we do not send
2093 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002094 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05002095 */
2096
2097 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002098 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05002099 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002100 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05002101
Steve French4587eee2017-10-25 15:58:31 -05002102 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2103 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002104 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002105
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002106 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2107 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002108 cifs_small_buf_release(req);
2109 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002110
Steve Frencheccb4422018-05-17 21:16:55 -05002111 if (rc != 0)
2112 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2113 ses->Suid, 0, opcode, rc);
2114
Steve French9bf0c9c2013-11-16 18:05:28 -06002115 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002116 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002117 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002118 } else if (rc == -EINVAL) {
2119 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2120 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002121 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002122 goto ioctl_exit;
2123 }
Steve French4a72daf2013-06-25 00:20:49 -05002124 }
2125
2126 /* check if caller wants to look at return data or just return rc */
2127 if ((plen == NULL) || (out_data == NULL))
2128 goto ioctl_exit;
2129
2130 *plen = le32_to_cpu(rsp->OutputCount);
2131
2132 /* We check for obvious errors in the output buffer length and offset */
2133 if (*plen == 0)
2134 goto ioctl_exit; /* server returned no data */
2135 else if (*plen > 0xFF00) {
2136 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2137 *plen = 0;
2138 rc = -EIO;
2139 goto ioctl_exit;
2140 }
2141
2142 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2143 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2144 le32_to_cpu(rsp->OutputOffset));
2145 *plen = 0;
2146 rc = -EIO;
2147 goto ioctl_exit;
2148 }
2149
2150 *out_data = kmalloc(*plen, GFP_KERNEL);
2151 if (*out_data == NULL) {
2152 rc = -ENOMEM;
2153 goto ioctl_exit;
2154 }
2155
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002156 shdr = get_sync_hdr(rsp);
2157 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002158ioctl_exit:
2159 free_rsp_buf(resp_buftype, rsp);
2160 return rc;
2161}
2162
Steve French64a5cfa2013-10-14 15:31:32 -05002163/*
2164 * Individual callers to ioctl worker function follow
2165 */
2166
2167int
2168SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2169 u64 persistent_fid, u64 volatile_fid)
2170{
2171 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002172 struct compress_ioctl fsctl_input;
2173 char *ret_data = NULL;
2174
2175 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002176 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002177
2178 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2179 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2180 (char *)&fsctl_input /* data input */,
2181 2 /* in data len */, &ret_data /* out data */, NULL);
2182
2183 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002184
2185 return rc;
2186}
2187
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002188int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002189SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2190 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002191{
2192 struct smb2_close_req *req;
2193 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002194 struct cifs_ses *ses = tcon->ses;
2195 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002196 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002197 int resp_buftype;
2198 int rc = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002199 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002200
Joe Perchesf96637b2013-05-04 22:12:25 -05002201 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002202
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002203 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002204 return -EIO;
2205
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002206 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002207 if (rc)
2208 return rc;
2209
Steve French5a77e752018-05-09 17:43:08 -05002210 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002211 flags |= CIFS_TRANSFORM_REQ;
2212
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002213 req->PersistentFileId = persistent_fid;
2214 req->VolatileFileId = volatile_fid;
2215
2216 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002217 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002218
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002219 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002220 cifs_small_buf_release(req);
2221 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002222
2223 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002224 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002225 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2226 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002227 goto close_exit;
2228 }
2229
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002230 /* BB FIXME - decode close response, update inode for caching */
2231
2232close_exit:
2233 free_rsp_buf(resp_buftype, rsp);
2234 return rc;
2235}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002236
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002237int
2238SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2239 u64 persistent_fid, u64 volatile_fid)
2240{
2241 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2242}
2243
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002244static int
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002245validate_iov(struct TCP_Server_Info *server,
2246 unsigned int offset, unsigned int buffer_length,
2247 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002248{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002249 unsigned int smb_len = iov->iov_len;
2250 char *end_of_smb = smb_len + server->vals->header_preamble_size + (char *)iov->iov_base;
2251 char *begin_of_buf = server->vals->header_preamble_size + offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002252 char *end_of_buf = begin_of_buf + buffer_length;
2253
2254
2255 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002256 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2257 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002258 return -EINVAL;
2259 }
2260
2261 /* check if beyond RFC1001 maximum length */
2262 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002263 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2264 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002265 return -EINVAL;
2266 }
2267
2268 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002269 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002270 return -EINVAL;
2271 }
2272
2273 return 0;
2274}
2275
2276/*
2277 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2278 * Caller must free buffer.
2279 */
2280static int
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002281validate_and_copy_iov(struct TCP_Server_Info *server,
2282 unsigned int offset, unsigned int buffer_length,
2283 struct kvec *iov, unsigned int minbufsize,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002284 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002285{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002286 char *begin_of_buf = server->vals->header_preamble_size + offset + (char *)(iov->iov_base);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002287 int rc;
2288
2289 if (!data)
2290 return -EINVAL;
2291
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002292 rc = validate_iov(server, offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002293 if (rc)
2294 return rc;
2295
2296 memcpy(data, begin_of_buf, buffer_length);
2297
2298 return 0;
2299}
2300
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002301static int
2302query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002303 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2304 u32 additional_info, size_t output_len, size_t min_len, void **data,
2305 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002306{
2307 struct smb2_query_info_req *req;
2308 struct smb2_query_info_rsp *rsp = NULL;
2309 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002310 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002311 int rc = 0;
2312 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002313 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002314 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002315 unsigned int total_len;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002316
Joe Perchesf96637b2013-05-04 22:12:25 -05002317 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002318
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002319 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002320 return -EIO;
2321
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002322 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2323 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002324 if (rc)
2325 return rc;
2326
Steve French5a77e752018-05-09 17:43:08 -05002327 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002328 flags |= CIFS_TRANSFORM_REQ;
2329
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002330 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002331 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002332 req->PersistentFileId = persistent_fid;
2333 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002334 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002335
2336 /*
2337 * We do not use the input buffer (do not send extra byte)
2338 */
2339 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002340
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002341 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002342
2343 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002344 /* 1 for Buffer */
2345 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002346
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002347 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002348 cifs_small_buf_release(req);
2349 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002350
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002351 if (rc) {
2352 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002353 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2354 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002355 goto qinf_exit;
2356 }
2357
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002358 if (dlen) {
2359 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2360 if (!*data) {
2361 *data = kmalloc(*dlen, GFP_KERNEL);
2362 if (!*data) {
2363 cifs_dbg(VFS,
2364 "Error %d allocating memory for acl\n",
2365 rc);
2366 *dlen = 0;
2367 goto qinf_exit;
2368 }
2369 }
2370 }
2371
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002372 rc = validate_and_copy_iov(ses->server,
2373 le16_to_cpu(rsp->OutputBufferOffset),
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002374 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002375 &rsp_iov, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002376
2377qinf_exit:
2378 free_rsp_buf(resp_buftype, rsp);
2379 return rc;
2380}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002381
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002382int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002383 u64 persistent_fid, u64 volatile_fid,
2384 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002385{
2386 return query_info(xid, tcon, persistent_fid, volatile_fid,
2387 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002388 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002389 sizeof(struct smb2_file_full_ea_info),
2390 (void **)&data,
2391 NULL);
2392}
2393
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002394int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2395 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002396{
2397 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002398 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002399 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002400 sizeof(struct smb2_file_all_info), (void **)&data,
2401 NULL);
2402}
2403
2404int
2405SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2406 u64 persistent_fid, u64 volatile_fid,
2407 void **data, u32 *plen)
2408{
2409 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2410 *plen = 0;
2411
2412 return query_info(xid, tcon, persistent_fid, volatile_fid,
2413 0, SMB2_O_INFO_SECURITY, additional_info,
2414 SMB2_MAX_BUFFER_SIZE,
2415 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002416}
2417
2418int
2419SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2420 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2421{
2422 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002423 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002424 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002425 sizeof(struct smb2_file_internal_info),
2426 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002427}
2428
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002429/*
2430 * This is a no-op for now. We're not really interested in the reply, but
2431 * rather in the fact that the server sent one and that server->lstrp
2432 * gets updated.
2433 *
2434 * FIXME: maybe we should consider checking that the reply matches request?
2435 */
2436static void
2437smb2_echo_callback(struct mid_q_entry *mid)
2438{
2439 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002440 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002441 unsigned int credits_received = 1;
2442
2443 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002444 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002445
2446 DeleteMidQEntry(mid);
2447 add_credits(server, credits_received, CIFS_ECHO_OP);
2448}
2449
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002450void smb2_reconnect_server(struct work_struct *work)
2451{
2452 struct TCP_Server_Info *server = container_of(work,
2453 struct TCP_Server_Info, reconnect.work);
2454 struct cifs_ses *ses;
2455 struct cifs_tcon *tcon, *tcon2;
2456 struct list_head tmp_list;
2457 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002458 int rc;
2459 int resched = false;
2460
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002461
2462 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2463 mutex_lock(&server->reconnect_mutex);
2464
2465 INIT_LIST_HEAD(&tmp_list);
2466 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2467
2468 spin_lock(&cifs_tcp_ses_lock);
2469 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2470 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002471 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002472 tcon->tc_count++;
2473 list_add_tail(&tcon->rlist, &tmp_list);
2474 tcon_exist = true;
2475 }
2476 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002477 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2478 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2479 tcon_exist = true;
2480 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002481 }
2482 /*
2483 * Get the reference to server struct to be sure that the last call of
2484 * cifs_put_tcon() in the loop below won't release the server pointer.
2485 */
2486 if (tcon_exist)
2487 server->srv_count++;
2488
2489 spin_unlock(&cifs_tcp_ses_lock);
2490
2491 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002492 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2493 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002494 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002495 else
2496 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002497 list_del_init(&tcon->rlist);
2498 cifs_put_tcon(tcon);
2499 }
2500
2501 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002502 if (resched)
2503 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002504 mutex_unlock(&server->reconnect_mutex);
2505
2506 /* now we can safely release srv struct */
2507 if (tcon_exist)
2508 cifs_put_tcp_session(server, 1);
2509}
2510
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002511int
2512SMB2_echo(struct TCP_Server_Info *server)
2513{
2514 struct smb2_echo_req *req;
2515 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002516 struct kvec iov[2];
2517 struct smb_rqst rqst = { .rq_iov = iov,
2518 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002519 unsigned int total_len;
2520 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002521
Joe Perchesf96637b2013-05-04 22:12:25 -05002522 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002523
Steve French4fcd1812016-06-22 20:12:05 -05002524 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002525 /* No need to send echo on newly established connections */
2526 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2527 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002528 }
2529
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002530 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002531 if (rc)
2532 return rc;
2533
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002534 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002535
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002536 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002537 rfc1002_marker = cpu_to_be32(total_len);
2538 iov[0].iov_base = &rfc1002_marker;
2539 iov[1].iov_len = total_len;
2540 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002541
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002542 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2543 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002544 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002545 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002546
2547 cifs_small_buf_release(req);
2548 return rc;
2549}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002550
2551int
2552SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2553 u64 volatile_fid)
2554{
2555 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002556 struct cifs_ses *ses = tcon->ses;
2557 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002558 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002559 int resp_buftype;
2560 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002561 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002562 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002563
Joe Perchesf96637b2013-05-04 22:12:25 -05002564 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002565
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002566 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002567 return -EIO;
2568
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002569 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002570 if (rc)
2571 return rc;
2572
Steve French5a77e752018-05-09 17:43:08 -05002573 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002574 flags |= CIFS_TRANSFORM_REQ;
2575
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002576 req->PersistentFileId = persistent_fid;
2577 req->VolatileFileId = volatile_fid;
2578
2579 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002580 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002581
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002582 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002583 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002584
Steve Frencheccb4422018-05-17 21:16:55 -05002585 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002586 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002587 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
2588 rc);
2589 }
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002590
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002591 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002592 return rc;
2593}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002594
2595/*
2596 * To form a chain of read requests, any read requests after the first should
2597 * have the end_of_chain boolean set to true.
2598 */
2599static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002600smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002601 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2602 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002603{
2604 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002605 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002606 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002607 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002608
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002609 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2610 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002611 if (rc)
2612 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002613
2614 server = io_parms->tcon->ses->server;
2615 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002616 return -ECONNABORTED;
2617
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002618 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002619 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002620
2621 req->PersistentFileId = io_parms->persistent_fid;
2622 req->VolatileFileId = io_parms->volatile_fid;
2623 req->ReadChannelInfoOffset = 0; /* reserved */
2624 req->ReadChannelInfoLength = 0; /* reserved */
2625 req->Channel = 0; /* reserved */
2626 req->MinimumCount = 0;
2627 req->Length = cpu_to_le32(io_parms->length);
2628 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07002629#ifdef CONFIG_CIFS_SMB_DIRECT
2630 /*
2631 * If we want to do a RDMA write, fill in and append
2632 * smbd_buffer_descriptor_v1 to the end of read request
2633 */
Long Libb4c0412018-04-17 12:17:08 -07002634 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07002635 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002636
Long Libd3dcc62017-11-22 17:38:47 -07002637 struct smbd_buffer_descriptor_v1 *v1;
2638 bool need_invalidate =
2639 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2640
2641 rdata->mr = smbd_register_mr(
2642 server->smbd_conn, rdata->pages,
2643 rdata->nr_pages, rdata->tailsz,
2644 true, need_invalidate);
2645 if (!rdata->mr)
2646 return -ENOBUFS;
2647
2648 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2649 if (need_invalidate)
2650 req->Channel = SMB2_CHANNEL_RDMA_V1;
2651 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002652 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07002653 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002654 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07002655 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002656 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2657 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2658 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07002659
2660 *total_len += sizeof(*v1) - 1;
2661 }
2662#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002663 if (request_type & CHAINED_REQUEST) {
2664 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002665 /* next 8-byte aligned request */
2666 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2667 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002668 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002669 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002670 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002671 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002672 /*
2673 * Related requests use info from previous read request
2674 * in chain.
2675 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002676 shdr->SessionId = 0xFFFFFFFF;
2677 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002678 req->PersistentFileId = 0xFFFFFFFF;
2679 req->VolatileFileId = 0xFFFFFFFF;
2680 }
2681 }
2682 if (remaining_bytes > io_parms->length)
2683 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2684 else
2685 req->RemainingBytes = 0;
2686
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002687 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002688 return rc;
2689}
2690
2691static void
2692smb2_readv_callback(struct mid_q_entry *mid)
2693{
2694 struct cifs_readdata *rdata = mid->callback_data;
2695 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2696 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002697 struct smb2_sync_hdr *shdr =
2698 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002699 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002700 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2701 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002702 .rq_pages = rdata->pages,
2703 .rq_npages = rdata->nr_pages,
2704 .rq_pagesz = rdata->pagesz,
2705 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002706
Joe Perchesf96637b2013-05-04 22:12:25 -05002707 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2708 __func__, mid->mid, mid->mid_state, rdata->result,
2709 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002710
2711 switch (mid->mid_state) {
2712 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002713 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002714 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002715 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002716 int rc;
2717
Jeff Layton0b688cf2012-09-18 16:20:34 -07002718 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002719 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002720 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2721 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002722 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002723 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002724 task_io_account_read(rdata->got_bytes);
2725 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002726 break;
2727 case MID_REQUEST_SUBMITTED:
2728 case MID_RETRY_NEEDED:
2729 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002730 if (server->sign && rdata->got_bytes)
2731 /* reset bytes number since we can not check a sign */
2732 rdata->got_bytes = 0;
2733 /* FIXME: should this be counted toward the initiating task? */
2734 task_io_account_read(rdata->got_bytes);
2735 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002736 break;
2737 default:
2738 if (rdata->result != -ENODATA)
2739 rdata->result = -EIO;
2740 }
Long Libd3dcc62017-11-22 17:38:47 -07002741#ifdef CONFIG_CIFS_SMB_DIRECT
2742 /*
2743 * If this rdata has a memmory registered, the MR can be freed
2744 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2745 * because they have limited number and are used for future I/Os
2746 */
2747 if (rdata->mr) {
2748 smbd_deregister_mr(rdata->mr);
2749 rdata->mr = NULL;
2750 }
2751#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002752 if (rdata->result)
2753 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2754
2755 queue_work(cifsiod_wq, &rdata->work);
2756 DeleteMidQEntry(mid);
2757 add_credits(server, credits_received, 0);
2758}
2759
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002760/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002761int
2762smb2_async_readv(struct cifs_readdata *rdata)
2763{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002764 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002765 char *buf;
2766 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002767 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002768 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2769 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002770 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002771 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002772 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002773
Joe Perchesf96637b2013-05-04 22:12:25 -05002774 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2775 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002776
2777 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2778 io_parms.offset = rdata->offset;
2779 io_parms.length = rdata->bytes;
2780 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2781 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2782 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002783
2784 server = io_parms.tcon->ses->server;
2785
Long Li2dabfd52017-11-07 01:54:53 -07002786 rc = smb2_new_read_req(
2787 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002788 if (rc) {
2789 if (rc == -EAGAIN && rdata->credits) {
2790 /* credits was reset by reconnect */
2791 rdata->credits = 0;
2792 /* reduce in_flight value since we won't send the req */
2793 spin_lock(&server->req_lock);
2794 server->in_flight--;
2795 spin_unlock(&server->req_lock);
2796 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002797 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002798 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002799
Steve French5a77e752018-05-09 17:43:08 -05002800 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002801 flags |= CIFS_TRANSFORM_REQ;
2802
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002803 req_len = cpu_to_be32(total_len);
2804
2805 rdata->iov[0].iov_base = &req_len;
2806 rdata->iov[0].iov_len = sizeof(__be32);
2807 rdata->iov[1].iov_base = buf;
2808 rdata->iov[1].iov_len = total_len;
2809
2810 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002811
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002812 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002813 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002814 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002815 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002816 spin_lock(&server->req_lock);
2817 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002818 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002819 spin_unlock(&server->req_lock);
2820 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002821 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002822 }
2823
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002824 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002825 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002826 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002827 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002828 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002829 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002830 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002831 trace_smb3_read_err(rc, 0 /* xid */, io_parms.persistent_fid,
2832 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
2833 io_parms.offset, io_parms.length);
2834 } else
2835 trace_smb3_read_done(0 /* xid */, io_parms.persistent_fid,
2836 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
2837 io_parms.offset, io_parms.length);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002838
2839 cifs_small_buf_release(buf);
2840 return rc;
2841}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002842
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002843int
2844SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2845 unsigned int *nbytes, char **buf, int *buf_type)
2846{
2847 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002848 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002849 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002850 struct smb2_sync_hdr *shdr;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002851 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002852 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002853 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002854 int flags = CIFS_LOG_ERROR;
2855 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002856
2857 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07002858 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002859 if (rc)
2860 return rc;
2861
Steve French5a77e752018-05-09 17:43:08 -05002862 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002863 flags |= CIFS_TRANSFORM_REQ;
2864
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002865 iov[0].iov_base = (char *)req;
2866 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002867
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002868 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002869 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002870
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002871 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002872
2873 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002874 if (rc != -ENODATA) {
2875 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2876 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002877 }
Steve Frencheccb4422018-05-17 21:16:55 -05002878 trace_smb3_read_err(rc, xid, req->PersistentFileId,
2879 io_parms->tcon->tid, ses->Suid,
2880 io_parms->offset, io_parms->length);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002881 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2882 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05002883 } else
2884 trace_smb3_read_done(xid, req->PersistentFileId,
2885 io_parms->tcon->tid, ses->Suid,
2886 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002887
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002888 *nbytes = le32_to_cpu(rsp->DataLength);
2889 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2890 (*nbytes > io_parms->length)) {
2891 cifs_dbg(FYI, "bad length %d for count %d\n",
2892 *nbytes, io_parms->length);
2893 rc = -EIO;
2894 *nbytes = 0;
2895 }
2896
2897 shdr = get_sync_hdr(rsp);
2898
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002899 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002900 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002901 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002902 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002903 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002904 if (resp_buftype == CIFS_SMALL_BUFFER)
2905 *buf_type = CIFS_SMALL_BUFFER;
2906 else if (resp_buftype == CIFS_LARGE_BUFFER)
2907 *buf_type = CIFS_LARGE_BUFFER;
2908 }
2909 return rc;
2910}
2911
Pavel Shilovsky33319142012-09-18 16:20:29 -07002912/*
2913 * Check the mid_state and signature on received buffer (if any), and queue the
2914 * workqueue completion task.
2915 */
2916static void
2917smb2_writev_callback(struct mid_q_entry *mid)
2918{
2919 struct cifs_writedata *wdata = mid->callback_data;
2920 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2921 unsigned int written;
2922 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2923 unsigned int credits_received = 1;
2924
2925 switch (mid->mid_state) {
2926 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002927 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002928 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2929 if (wdata->result != 0)
2930 break;
2931
2932 written = le32_to_cpu(rsp->DataLength);
2933 /*
2934 * Mask off high 16 bits when bytes written as returned
2935 * by the server is greater than bytes requested by the
2936 * client. OS/2 servers are known to set incorrect
2937 * CountHigh values.
2938 */
2939 if (written > wdata->bytes)
2940 written &= 0xFFFF;
2941
2942 if (written < wdata->bytes)
2943 wdata->result = -ENOSPC;
2944 else
2945 wdata->bytes = written;
2946 break;
2947 case MID_REQUEST_SUBMITTED:
2948 case MID_RETRY_NEEDED:
2949 wdata->result = -EAGAIN;
2950 break;
2951 default:
2952 wdata->result = -EIO;
2953 break;
2954 }
Long Lidb223a52017-11-22 17:38:45 -07002955#ifdef CONFIG_CIFS_SMB_DIRECT
2956 /*
2957 * If this wdata has a memory registered, the MR can be freed
2958 * The number of MRs available is limited, it's important to recover
2959 * used MR as soon as I/O is finished. Hold MR longer in the later
2960 * I/O process can possibly result in I/O deadlock due to lack of MR
2961 * to send request on I/O retry
2962 */
2963 if (wdata->mr) {
2964 smbd_deregister_mr(wdata->mr);
2965 wdata->mr = NULL;
2966 }
2967#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002968 if (wdata->result)
2969 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2970
2971 queue_work(cifsiod_wq, &wdata->work);
2972 DeleteMidQEntry(mid);
2973 add_credits(tcon->ses->server, credits_received, 0);
2974}
2975
2976/* smb2_async_writev - send an async write, and set up mid to handle result */
2977int
Steve French4a5c80d2014-02-07 20:45:12 -06002978smb2_async_writev(struct cifs_writedata *wdata,
2979 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002980{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002981 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002982 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002983 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002984 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002985 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002986 struct kvec iov[2];
2987 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002988 unsigned int total_len;
2989 __be32 rfc1002_marker;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002990
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002991 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002992 if (rc) {
2993 if (rc == -EAGAIN && wdata->credits) {
2994 /* credits was reset by reconnect */
2995 wdata->credits = 0;
2996 /* reduce in_flight value since we won't send the req */
2997 spin_lock(&server->req_lock);
2998 server->in_flight--;
2999 spin_unlock(&server->req_lock);
3000 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003001 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003002 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003003
Steve French5a77e752018-05-09 17:43:08 -05003004 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003005 flags |= CIFS_TRANSFORM_REQ;
3006
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003007 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003008 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003009
3010 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3011 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3012 req->WriteChannelInfoOffset = 0;
3013 req->WriteChannelInfoLength = 0;
3014 req->Channel = 0;
3015 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003016 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003017 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003018 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07003019#ifdef CONFIG_CIFS_SMB_DIRECT
3020 /*
3021 * If we want to do a server RDMA read, fill in and append
3022 * smbd_buffer_descriptor_v1 to the end of write request
3023 */
Long Libb4c0412018-04-17 12:17:08 -07003024 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003025 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003026
Long Lidb223a52017-11-22 17:38:45 -07003027 struct smbd_buffer_descriptor_v1 *v1;
3028 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3029
3030 wdata->mr = smbd_register_mr(
3031 server->smbd_conn, wdata->pages,
3032 wdata->nr_pages, wdata->tailsz,
3033 false, need_invalidate);
3034 if (!wdata->mr) {
3035 rc = -ENOBUFS;
3036 goto async_writev_out;
3037 }
3038 req->Length = 0;
3039 req->DataOffset = 0;
3040 req->RemainingBytes =
Steve French2026b062018-01-24 23:07:41 -06003041 cpu_to_le32((wdata->nr_pages-1)*PAGE_SIZE + wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003042 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3043 if (need_invalidate)
3044 req->Channel = SMB2_CHANNEL_RDMA_V1;
3045 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003046 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003047 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003048 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003049 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003050 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3051 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3052 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003053 }
3054#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003055 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003056 iov[0].iov_len = 4;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003057 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
3058 iov[0].iov_base = &rfc1002_marker;
3059 iov[1].iov_len = total_len - 1;
3060 iov[1].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003061
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003062 rqst.rq_iov = iov;
3063 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003064 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07003065 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003066 rqst.rq_npages = wdata->nr_pages;
3067 rqst.rq_pagesz = wdata->pagesz;
3068 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003069#ifdef CONFIG_CIFS_SMB_DIRECT
3070 if (wdata->mr) {
3071 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3072 rqst.rq_npages = 0;
3073 }
3074#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003075 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3076 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003077
Long Lidb223a52017-11-22 17:38:45 -07003078#ifdef CONFIG_CIFS_SMB_DIRECT
3079 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3080 if (!wdata->mr)
3081 req->Length = cpu_to_le32(wdata->bytes);
3082#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003083 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003084#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003085
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003086 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003087 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003088 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003089 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003090 spin_lock(&server->req_lock);
3091 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003092 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003093 spin_unlock(&server->req_lock);
3094 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003095 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003096 }
3097
Pavel Shilovsky33319142012-09-18 16:20:29 -07003098 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003099 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3100 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003101
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003102 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003103 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3104 tcon->tid, tcon->ses->Suid, wdata->offset,
3105 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06003106 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003107 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003108 } else
3109 trace_smb3_write_done(0 /* no xid */, req->PersistentFileId,
3110 tcon->tid, tcon->ses->Suid, wdata->offset,
3111 wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003112
Pavel Shilovsky33319142012-09-18 16:20:29 -07003113async_writev_out:
3114 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003115 return rc;
3116}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003117
3118/*
3119 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3120 * The length field from io_parms must be at least 1 and indicates a number of
3121 * elements with data to write that begins with position 1 in iov array. All
3122 * data length is specified by count.
3123 */
3124int
3125SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3126 unsigned int *nbytes, struct kvec *iov, int n_vec)
3127{
3128 int rc = 0;
3129 struct smb2_write_req *req = NULL;
3130 struct smb2_write_rsp *rsp = NULL;
3131 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003132 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003133 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003134 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003135
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003136 *nbytes = 0;
3137
3138 if (n_vec < 1)
3139 return rc;
3140
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003141 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3142 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003143 if (rc)
3144 return rc;
3145
3146 if (io_parms->tcon->ses->server == NULL)
3147 return -ECONNABORTED;
3148
Steve French5a77e752018-05-09 17:43:08 -05003149 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003150 flags |= CIFS_TRANSFORM_REQ;
3151
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003152 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003153
3154 req->PersistentFileId = io_parms->persistent_fid;
3155 req->VolatileFileId = io_parms->volatile_fid;
3156 req->WriteChannelInfoOffset = 0;
3157 req->WriteChannelInfoLength = 0;
3158 req->Channel = 0;
3159 req->Length = cpu_to_le32(io_parms->length);
3160 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003161 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003162 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003163 req->RemainingBytes = 0;
3164
3165 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003166 /* 1 for Buffer */
3167 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003168
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003169 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
3170 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003171 cifs_small_buf_release(req);
3172 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003173
3174 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003175 trace_smb3_write_err(xid, req->PersistentFileId,
3176 io_parms->tcon->tid,
3177 io_parms->tcon->ses->Suid,
3178 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003179 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003180 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05003181 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003182 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05003183 trace_smb3_write_done(xid, req->PersistentFileId,
3184 io_parms->tcon->tid,
3185 io_parms->tcon->ses->Suid,
3186 io_parms->offset, *nbytes);
3187 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003188
3189 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003190 return rc;
3191}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003192
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003193static unsigned int
3194num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3195{
3196 int len;
3197 unsigned int entrycount = 0;
3198 unsigned int next_offset = 0;
3199 FILE_DIRECTORY_INFO *entryptr;
3200
3201 if (bufstart == NULL)
3202 return 0;
3203
3204 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3205
3206 while (1) {
3207 entryptr = (FILE_DIRECTORY_INFO *)
3208 ((char *)entryptr + next_offset);
3209
3210 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003211 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003212 break;
3213 }
3214
3215 len = le32_to_cpu(entryptr->FileNameLength);
3216 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003217 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3218 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003219 break;
3220 }
3221
3222 *lastentry = (char *)entryptr;
3223 entrycount++;
3224
3225 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3226 if (!next_offset)
3227 break;
3228 }
3229
3230 return entrycount;
3231}
3232
3233/*
3234 * Readdir/FindFirst
3235 */
3236int
3237SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3238 u64 persistent_fid, u64 volatile_fid, int index,
3239 struct cifs_search_info *srch_inf)
3240{
3241 struct smb2_query_directory_req *req;
3242 struct smb2_query_directory_rsp *rsp = NULL;
3243 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003244 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003245 int rc = 0;
3246 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003247 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003248 unsigned char *bufptr;
3249 struct TCP_Server_Info *server;
3250 struct cifs_ses *ses = tcon->ses;
3251 __le16 asteriks = cpu_to_le16('*');
3252 char *end_of_smb;
3253 unsigned int output_size = CIFSMaxBufSize;
3254 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003255 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003256 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003257
3258 if (ses && (ses->server))
3259 server = ses->server;
3260 else
3261 return -EIO;
3262
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003263 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3264 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003265 if (rc)
3266 return rc;
3267
Steve French5a77e752018-05-09 17:43:08 -05003268 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003269 flags |= CIFS_TRANSFORM_REQ;
3270
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003271 switch (srch_inf->info_level) {
3272 case SMB_FIND_FILE_DIRECTORY_INFO:
3273 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3274 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3275 break;
3276 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3277 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3278 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3279 break;
3280 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003281 cifs_dbg(VFS, "info level %u isn't supported\n",
3282 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003283 rc = -EINVAL;
3284 goto qdir_exit;
3285 }
3286
3287 req->FileIndex = cpu_to_le32(index);
3288 req->PersistentFileId = persistent_fid;
3289 req->VolatileFileId = volatile_fid;
3290
3291 len = 0x2;
3292 bufptr = req->Buffer;
3293 memcpy(bufptr, &asteriks, len);
3294
3295 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003296 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003297 req->FileNameLength = cpu_to_le16(len);
3298 /*
3299 * BB could be 30 bytes or so longer if we used SMB2 specific
3300 * buffer lengths, but this is safe and close enough.
3301 */
3302 output_size = min_t(unsigned int, output_size, server->maxBuf);
3303 output_size = min_t(unsigned int, output_size, 2 << 15);
3304 req->OutputBufferLength = cpu_to_le32(output_size);
3305
3306 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003307 /* 1 for Buffer */
3308 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003309
3310 iov[1].iov_base = (char *)(req->Buffer);
3311 iov[1].iov_len = len;
3312
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003313 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003314 cifs_small_buf_release(req);
3315 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003316
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003317 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003318 if (rc == -ENODATA &&
3319 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003320 srch_inf->endOfSearch = true;
3321 rc = 0;
3322 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003323 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3324 goto qdir_exit;
3325 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003326
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003327 rc = validate_iov(server,
3328 le16_to_cpu(rsp->OutputBufferOffset),
3329 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003330 info_buf_size);
3331 if (rc)
3332 goto qdir_exit;
3333
3334 srch_inf->unicode = true;
3335
3336 if (srch_inf->ntwrk_buf_start) {
3337 if (srch_inf->smallBuf)
3338 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3339 else
3340 cifs_buf_release(srch_inf->ntwrk_buf_start);
3341 }
3342 srch_inf->ntwrk_buf_start = (char *)rsp;
3343 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3344 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3345 /* 4 for rfc1002 length field */
3346 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3347 srch_inf->entries_in_buffer =
3348 num_entries(srch_inf->srch_entries_start, end_of_smb,
3349 &srch_inf->last_entry, info_buf_size);
3350 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003351 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3352 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3353 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003354 if (resp_buftype == CIFS_LARGE_BUFFER)
3355 srch_inf->smallBuf = false;
3356 else if (resp_buftype == CIFS_SMALL_BUFFER)
3357 srch_inf->smallBuf = true;
3358 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003359 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003360
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003361 return rc;
3362
3363qdir_exit:
3364 free_rsp_buf(resp_buftype, rsp);
3365 return rc;
3366}
3367
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003368static int
3369send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003370 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3371 u8 info_type, u32 additional_info, unsigned int num,
3372 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003373{
3374 struct smb2_set_info_req *req;
3375 struct smb2_set_info_rsp *rsp = NULL;
3376 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003377 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003378 int rc = 0;
3379 int resp_buftype;
3380 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003381 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003382 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003383 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003384
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003385 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003386 return -EIO;
3387
3388 if (!num)
3389 return -EINVAL;
3390
3391 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3392 if (!iov)
3393 return -ENOMEM;
3394
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003395 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003396 if (rc) {
3397 kfree(iov);
3398 return rc;
3399 }
3400
Steve French5a77e752018-05-09 17:43:08 -05003401 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003402 flags |= CIFS_TRANSFORM_REQ;
3403
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003404 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003405
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003406 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003407 req->FileInfoClass = info_class;
3408 req->PersistentFileId = persistent_fid;
3409 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003410 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003411
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003412 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003413 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003414 req->BufferLength = cpu_to_le32(*size);
3415
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003416 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003417 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003418
3419 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003420 /* 1 for Buffer */
3421 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003422
3423 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003424 le32_add_cpu(&req->BufferLength, size[i]);
3425 iov[i].iov_base = (char *)data[i];
3426 iov[i].iov_len = size[i];
3427 }
3428
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003429 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3430 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003431 cifs_small_buf_release(req);
3432 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003433
Steve Frencheccb4422018-05-17 21:16:55 -05003434 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003435 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003436 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
3437 ses->Suid, info_class, (__u32)info_type, rc);
3438 }
Steve French7d3fb242013-11-18 09:56:28 -06003439
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003440 free_rsp_buf(resp_buftype, rsp);
3441 kfree(iov);
3442 return rc;
3443}
3444
3445int
3446SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3447 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3448{
3449 struct smb2_file_rename_info info;
3450 void **data;
3451 unsigned int size[2];
3452 int rc;
3453 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3454
3455 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3456 if (!data)
3457 return -ENOMEM;
3458
3459 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3460 /* 0 = fail if target already exists */
3461 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3462 info.FileNameLength = cpu_to_le32(len);
3463
3464 data[0] = &info;
3465 size[0] = sizeof(struct smb2_file_rename_info);
3466
3467 data[1] = target_file;
3468 size[1] = len + 2 /* null */;
3469
3470 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003471 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3472 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003473 kfree(data);
3474 return rc;
3475}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003476
3477int
Steve French897fba12016-05-12 21:20:36 -05003478SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3479 u64 persistent_fid, u64 volatile_fid)
3480{
3481 __u8 delete_pending = 1;
3482 void *data;
3483 unsigned int size;
3484
3485 data = &delete_pending;
3486 size = 1; /* sizeof __u8 */
3487
3488 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003489 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3490 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003491}
3492
3493int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003494SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3495 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3496{
3497 struct smb2_file_link_info info;
3498 void **data;
3499 unsigned int size[2];
3500 int rc;
3501 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3502
3503 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3504 if (!data)
3505 return -ENOMEM;
3506
3507 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3508 /* 0 = fail if link already exists */
3509 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3510 info.FileNameLength = cpu_to_le32(len);
3511
3512 data[0] = &info;
3513 size[0] = sizeof(struct smb2_file_link_info);
3514
3515 data[1] = target_file;
3516 size[1] = len + 2 /* null */;
3517
3518 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003519 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3520 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003521 kfree(data);
3522 return rc;
3523}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003524
3525int
3526SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003527 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003528{
3529 struct smb2_file_eof_info info;
3530 void *data;
3531 unsigned int size;
3532
3533 info.EndOfFile = *eof;
3534
3535 data = &info;
3536 size = sizeof(struct smb2_file_eof_info);
3537
Steve Frenchf29ebb42014-07-19 21:44:58 -05003538 if (is_falloc)
3539 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003540 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3541 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003542 else
3543 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003544 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3545 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003546}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003547
3548int
3549SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3550 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3551{
3552 unsigned int size;
3553 size = sizeof(FILE_BASIC_INFO);
3554 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003555 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3556 0, 1, (void **)&buf, &size);
3557}
3558
3559int
3560SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3561 u64 persistent_fid, u64 volatile_fid,
3562 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3563{
3564 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3565 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3566 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003567}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003568
3569int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003570SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3571 u64 persistent_fid, u64 volatile_fid,
3572 struct smb2_file_full_ea_info *buf, int len)
3573{
3574 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3575 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3576 0, 1, (void **)&buf, &len);
3577}
3578
3579int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003580SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3581 const u64 persistent_fid, const u64 volatile_fid,
3582 __u8 oplock_level)
3583{
3584 int rc;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003585 struct smb2_oplock_break_req *req = NULL;
3586 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003587 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003588 unsigned int total_len;
3589 struct kvec iov[1];
3590 struct kvec rsp_iov;
3591 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003592
Joe Perchesf96637b2013-05-04 22:12:25 -05003593 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003594 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3595 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003596 if (rc)
3597 return rc;
3598
Steve French5a77e752018-05-09 17:43:08 -05003599 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003600 flags |= CIFS_TRANSFORM_REQ;
3601
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003602 req->VolatileFid = volatile_fid;
3603 req->PersistentFid = persistent_fid;
3604 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003605 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003606
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003607 flags |= CIFS_NO_RESP;
3608
3609 iov[0].iov_base = (char *)req;
3610 iov[0].iov_len = total_len;
3611
3612 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003613 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003614
3615 if (rc) {
3616 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003617 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003618 }
3619
3620 return rc;
3621}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003622
3623static void
3624copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3625 struct kstatfs *kst)
3626{
3627 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3628 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3629 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303630 kst->f_bfree = kst->f_bavail =
3631 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003632 return;
3633}
3634
3635static int
3636build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3637 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3638{
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003639 struct TCP_Server_Info *server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003640 int rc;
3641 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003642 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003643
Joe Perchesf96637b2013-05-04 22:12:25 -05003644 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003645
3646 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3647 return -EIO;
3648
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003649 server = tcon->ses->server;
3650
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003651 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3652 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003653 if (rc)
3654 return rc;
3655
3656 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3657 req->FileInfoClass = level;
3658 req->PersistentFileId = persistent_fid;
3659 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003660 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003661 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003662 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003663 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003664 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - server->vals->header_preamble_size);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003665
3666 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003667 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003668 return 0;
3669}
3670
3671int
3672SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3673 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3674{
3675 struct smb2_query_info_rsp *rsp = NULL;
3676 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003677 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003678 int rc = 0;
3679 int resp_buftype;
3680 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003681 struct TCP_Server_Info *server = ses->server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003682 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003683 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003684
3685 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3686 sizeof(struct smb2_fs_full_size_info),
3687 persistent_fid, volatile_fid);
3688 if (rc)
3689 return rc;
3690
Steve French5a77e752018-05-09 17:43:08 -05003691 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003692 flags |= CIFS_TRANSFORM_REQ;
3693
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003694 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003695 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003696 if (rc) {
3697 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003698 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003699 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003700 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003701
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003702 info = (struct smb2_fs_full_size_info *)(server->vals->header_preamble_size +
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003703 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003704 rc = validate_iov(server,
3705 le16_to_cpu(rsp->OutputBufferOffset),
3706 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003707 sizeof(struct smb2_fs_full_size_info));
3708 if (!rc)
3709 copy_fs_info_to_kstatfs(info, fsdata);
3710
Steve French34f62642013-10-09 02:07:00 -05003711qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003712 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003713 return rc;
3714}
3715
3716int
3717SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003718 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003719{
3720 struct smb2_query_info_rsp *rsp = NULL;
3721 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003722 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003723 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003724 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003725 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003726 struct TCP_Server_Info *server = ses->server;
Steve French34f62642013-10-09 02:07:00 -05003727 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003728 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003729
Steven French21671142013-10-09 13:36:35 -05003730 if (level == FS_DEVICE_INFORMATION) {
3731 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3732 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3733 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3734 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3735 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003736 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3737 max_len = sizeof(struct smb3_fs_ss_info);
3738 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003739 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003740 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003741 return -EINVAL;
3742 }
3743
3744 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003745 persistent_fid, volatile_fid);
3746 if (rc)
3747 return rc;
3748
Steve French5a77e752018-05-09 17:43:08 -05003749 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003750 flags |= CIFS_TRANSFORM_REQ;
3751
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003752 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003753 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003754 if (rc) {
3755 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3756 goto qfsattr_exit;
3757 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003758 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003759
3760 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3761 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003762 rc = validate_iov(server, offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05003763 if (rc)
3764 goto qfsattr_exit;
3765
3766 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003767 memcpy(&tcon->fsAttrInfo, server->vals->header_preamble_size + offset
Steve French34f62642013-10-09 02:07:00 -05003768 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003769 rsp_len, max_len));
3770 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003771 memcpy(&tcon->fsDevInfo, server->vals->header_preamble_size + offset
Steven French21671142013-10-09 13:36:35 -05003772 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003773 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3774 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003775 (server->vals->header_preamble_size + offset + (char *)&rsp->hdr);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003776 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3777 tcon->perf_sector_size =
3778 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3779 }
Steve French34f62642013-10-09 02:07:00 -05003780
3781qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003782 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003783 return rc;
3784}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003785
3786int
3787smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3788 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3789 const __u32 num_lock, struct smb2_lock_element *buf)
3790{
3791 int rc = 0;
3792 struct smb2_lock_req *req = NULL;
3793 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003794 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003795 int resp_buf_type;
3796 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003797 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003798 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003799
Joe Perchesf96637b2013-05-04 22:12:25 -05003800 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003801
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003802 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003803 if (rc)
3804 return rc;
3805
Steve French5a77e752018-05-09 17:43:08 -05003806 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003807 flags |= CIFS_TRANSFORM_REQ;
3808
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003809 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003810 req->LockCount = cpu_to_le16(num_lock);
3811
3812 req->PersistentFileId = persist_fid;
3813 req->VolatileFileId = volatile_fid;
3814
3815 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003816
3817 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003818 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003819 iov[1].iov_base = (char *)buf;
3820 iov[1].iov_len = count;
3821
3822 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003823 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3824 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003825 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003826 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003827 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003828 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003829 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
3830 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003831 }
3832
3833 return rc;
3834}
3835
3836int
3837SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3838 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3839 const __u64 length, const __u64 offset, const __u32 lock_flags,
3840 const bool wait)
3841{
3842 struct smb2_lock_element lock;
3843
3844 lock.Offset = cpu_to_le64(offset);
3845 lock.Length = cpu_to_le64(length);
3846 lock.Flags = cpu_to_le32(lock_flags);
3847 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3848 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3849
3850 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3851}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003852
3853int
3854SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3855 __u8 *lease_key, const __le32 lease_state)
3856{
3857 int rc;
3858 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003859 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003860 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003861 unsigned int total_len;
3862 struct kvec iov[1];
3863 struct kvec rsp_iov;
3864 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003865
Joe Perchesf96637b2013-05-04 22:12:25 -05003866 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003867 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3868 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003869 if (rc)
3870 return rc;
3871
Steve French5a77e752018-05-09 17:43:08 -05003872 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003873 flags |= CIFS_TRANSFORM_REQ;
3874
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003875 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003876 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003877 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003878
3879 memcpy(req->LeaseKey, lease_key, 16);
3880 req->LeaseState = lease_state;
3881
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003882 flags |= CIFS_NO_RESP;
3883
3884 iov[0].iov_base = (char *)req;
3885 iov[0].iov_len = total_len;
3886
3887 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003888 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003889
3890 if (rc) {
3891 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003892 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003893 }
3894
3895 return rc;
3896}