blob: 0ae3ff24ada8ad3ed02a7237dc81e3f01a9b1251 [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,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000468 struct TCP_Server_Info *server,
469 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500470{
471 struct smb2_neg_context *pctx;
472 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
473 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500474 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
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000494 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500495 clen = le16_to_cpu(pctx->DataLength);
496 if (clen > len_of_ctxts)
497 break;
498
499 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
500 decode_preauth_context(
501 (struct smb2_preauth_neg_context *)pctx);
502 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
503 rc = decode_encrypt_ctx(server,
504 (struct smb2_encryption_neg_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500505 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
506 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500507 else
508 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
509 le16_to_cpu(pctx->ContextType));
510
511 if (rc)
512 break;
513 /* offsets must be 8 byte aligned */
514 clen = (clen + 7) & ~0x7;
515 offset += clen + sizeof(struct smb2_neg_context);
516 len_of_ctxts -= clen;
517 }
518 return rc;
519}
520
Steve Frenchce558b02018-05-31 19:16:54 -0500521static struct create_posix *
522create_posix_buf(umode_t mode)
523{
524 struct create_posix *buf;
525
526 buf = kzalloc(sizeof(struct create_posix),
527 GFP_KERNEL);
528 if (!buf)
529 return NULL;
530
531 buf->ccontext.DataOffset =
532 cpu_to_le16(offsetof(struct create_posix, Mode));
533 buf->ccontext.DataLength = cpu_to_le32(4);
534 buf->ccontext.NameOffset =
535 cpu_to_le16(offsetof(struct create_posix, Name));
536 buf->ccontext.NameLength = cpu_to_le16(16);
537
538 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
539 buf->Name[0] = 0x93;
540 buf->Name[1] = 0xAD;
541 buf->Name[2] = 0x25;
542 buf->Name[3] = 0x50;
543 buf->Name[4] = 0x9C;
544 buf->Name[5] = 0xB4;
545 buf->Name[6] = 0x11;
546 buf->Name[7] = 0xE7;
547 buf->Name[8] = 0xB4;
548 buf->Name[9] = 0x23;
549 buf->Name[10] = 0x83;
550 buf->Name[11] = 0xDE;
551 buf->Name[12] = 0x96;
552 buf->Name[13] = 0x8B;
553 buf->Name[14] = 0xCD;
554 buf->Name[15] = 0x7C;
555 buf->Mode = cpu_to_le32(mode);
556 cifs_dbg(FYI, "mode on posix create 0%o", mode);
557 return buf;
558}
559
560static int
561add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
562{
563 struct smb2_create_req *req = iov[0].iov_base;
564 unsigned int num = *num_iovec;
565
566 iov[num].iov_base = create_posix_buf(mode);
567 if (iov[num].iov_base == NULL)
568 return -ENOMEM;
569 iov[num].iov_len = sizeof(struct create_posix);
570 if (!req->CreateContextsOffset)
571 req->CreateContextsOffset = cpu_to_le32(
572 sizeof(struct smb2_create_req) +
573 iov[num - 1].iov_len);
574 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
575 *num_iovec = num + 1;
576 return 0;
577}
578
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500579#else
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100580static void assemble_neg_contexts(struct smb2_negotiate_req *req,
581 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500582{
583 return;
584}
585#endif /* SMB311 */
586
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400587/*
588 *
589 * SMB2 Worker functions follow:
590 *
591 * The general structure of the worker functions is:
592 * 1) Call smb2_init (assembles SMB2 header)
593 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
594 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
595 * 4) Decode SMB2 command specific fields in the fixed length area
596 * 5) Decode variable length data area (if any for this SMB2 command type)
597 * 6) Call free smb buffer
598 * 7) return
599 *
600 */
601
602int
603SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
604{
605 struct smb2_negotiate_req *req;
606 struct smb2_negotiate_rsp *rsp;
607 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700608 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400609 int rc = 0;
610 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400611 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400612 int blob_offset, blob_length;
613 char *security_blob;
614 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100615 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400616
Joe Perchesf96637b2013-05-04 22:12:25 -0500617 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400618
Jeff Layton3534b852013-05-24 07:41:01 -0400619 if (!server) {
620 WARN(1, "%s: server is NULL!\n", __func__);
621 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400622 }
623
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100624 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400625 if (rc)
626 return rc;
627
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100628 req->sync_hdr.SessionId = 0;
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100629#ifdef CONFIG_CIFS_SMB311
630 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
631 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
632#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400633
Steve French9764c022017-09-17 10:41:35 -0500634 if (strcmp(ses->server->vals->version_string,
635 SMB3ANY_VERSION_STRING) == 0) {
636 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
637 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
638 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100639 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500640 } else if (strcmp(ses->server->vals->version_string,
641 SMBDEFAULT_VERSION_STRING) == 0) {
642 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
643 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
644 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
645 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100646 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500647 } else {
648 /* otherwise send specific dialect */
649 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
650 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100651 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500652 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400653
654 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400655 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500656 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400657 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500658 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400659 else
660 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400661
Steve Frenche4aa25e2012-10-01 12:26:22 -0500662 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400663
Steve French3c5f9be12014-05-13 13:37:45 -0700664 /* ClientGUID must be zero for SMB2.02 dialect */
665 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
666 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500667 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700668 memcpy(req->ClientGUID, server->client_guid,
669 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500670 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100671 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500672 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400673 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100674 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400675
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100676 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700677 cifs_small_buf_release(req);
678 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400679 /*
680 * No tcon so can't do
681 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
682 */
Steve French7e682f72017-08-31 21:34:24 -0500683 if (rc == -EOPNOTSUPP) {
684 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500685 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500686 " older servers\n");
687 goto neg_exit;
688 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400689 goto neg_exit;
690
Steve French9764c022017-09-17 10:41:35 -0500691 if (strcmp(ses->server->vals->version_string,
692 SMB3ANY_VERSION_STRING) == 0) {
693 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
694 cifs_dbg(VFS,
695 "SMB2 dialect returned but not requested\n");
696 return -EIO;
697 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
698 cifs_dbg(VFS,
699 "SMB2.1 dialect returned but not requested\n");
700 return -EIO;
701 }
702 } else if (strcmp(ses->server->vals->version_string,
703 SMBDEFAULT_VERSION_STRING) == 0) {
704 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
705 cifs_dbg(VFS,
706 "SMB2 dialect returned but not requested\n");
707 return -EIO;
708 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
709 /* ops set to 3.0 by default for default so update */
710 ses->server->ops = &smb21_operations;
711 }
Steve French590d08d2017-09-19 11:43:47 -0500712 } else if (le16_to_cpu(rsp->DialectRevision) !=
713 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500714 /* if requested single dialect ensure returned dialect matched */
715 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500716 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500717 return -EIO;
718 }
719
Joe Perchesf96637b2013-05-04 22:12:25 -0500720 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400721
Steve Frenche4aa25e2012-10-01 12:26:22 -0500722 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500723 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500724 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500725 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500726 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500727 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500728 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
729 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600730#ifdef CONFIG_CIFS_SMB311
731 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
732 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
733#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400734 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500735 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500736 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400737 rc = -EIO;
738 goto neg_exit;
739 }
740 server->dialect = le16_to_cpu(rsp->DialectRevision);
741
Steve French9764c022017-09-17 10:41:35 -0500742 /* BB: add check that dialect was valid given dialect(s) we asked for */
743
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100744#ifdef CONFIG_CIFS_SMB311
745 /*
746 * Keep a copy of the hash after negprot. This hash will be
747 * the starting hash value for all sessions made from this
748 * server.
749 */
750 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
751 SMB2_PREAUTH_HASH_SIZE);
752#endif
Jeff Laytone598d1d82013-05-26 07:00:59 -0400753 /* SMB2 only has an extended negflavor */
754 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400755 /* set it to the maximum buffer size value we can send with 1 credit */
756 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
757 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400758 server->max_read = le32_to_cpu(rsp->MaxReadSize);
759 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400760 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500761 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
762 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
763 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400764 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400765 /* Internal types */
766 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400767
768 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000769 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500770 /*
771 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
772 * for us will be
773 * ses->sectype = RawNTLMSSP;
774 * but for time being this is our only auth choice so doesn't matter.
775 * We just found a server which sets blob length to zero expecting raw.
776 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700777 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500778 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700779 server->sec_ntlmssp = true;
780 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700781
Jeff Layton38d77c52013-05-26 07:01:00 -0400782 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400783 if (rc)
784 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500785 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500786 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500787 if (rc == 1)
788 rc = 0;
789 else if (rc == 0)
790 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400791 }
Steve French5100d8a2018-04-09 10:47:14 -0500792
793#ifdef CONFIG_CIFS_SMB311
794 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
795 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000796 rc = smb311_decode_neg_context(rsp, server,
797 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -0500798 else
799 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
800 }
801#endif /* CONFIG_CIFS_SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400802neg_exit:
803 free_rsp_buf(resp_buftype, rsp);
804 return rc;
805}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400806
Steve Frenchff1c0382013-11-19 23:44:46 -0600807int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
808{
Long Li2796d302018-04-25 11:30:04 -0700809 int rc;
810 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200811 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600812 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500813 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600814
815 cifs_dbg(FYI, "validate negotiate\n");
816
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100817 /* In SMB3.11 preauth integrity supersedes validate negotiate */
818 if (tcon->ses->server->dialect == SMB311_PROT_ID)
819 return 0;
820
Steve Frenchff1c0382013-11-19 23:44:46 -0600821 /*
822 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500823 * can not sign it (ie are not known user). Even if signing is not
824 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600825 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500826 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600827 */
Steve French0603c962017-09-20 19:57:18 -0500828 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600829 return 0; /* validation requires signing */
830
Steve French0603c962017-09-20 19:57:18 -0500831 if (tcon->ses->user_name == NULL) {
832 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
833 return 0; /* validation requires signing */
834 }
835
836 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
837 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
838
Long Li2796d302018-04-25 11:30:04 -0700839 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
840 if (!pneg_inbuf)
841 return -ENOMEM;
842
843 pneg_inbuf->Capabilities =
Steve Frenchff1c0382013-11-19 23:44:46 -0600844 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Long Li2796d302018-04-25 11:30:04 -0700845 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100846 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600847
848 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -0700849 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600850 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
851 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -0700852 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600853 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
854 else
Long Li2796d302018-04-25 11:30:04 -0700855 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600856
Steve French9764c022017-09-17 10:41:35 -0500857
858 if (strcmp(tcon->ses->server->vals->version_string,
859 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700860 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
861 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
862 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -0500863 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -0700864 inbuflen = sizeof(*pneg_inbuf) -
865 sizeof(pneg_inbuf->Dialects[0]);
Steve French9764c022017-09-17 10:41:35 -0500866 } else if (strcmp(tcon->ses->server->vals->version_string,
867 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700868 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
869 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
870 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
871 pneg_inbuf->DialectCount = cpu_to_le16(3);
Steve French9764c022017-09-17 10:41:35 -0500872 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -0700873 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -0500874 } else {
875 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -0700876 pneg_inbuf->Dialects[0] =
Steve French9764c022017-09-17 10:41:35 -0500877 cpu_to_le16(tcon->ses->server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -0700878 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -0500879 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -0700880 inbuflen = sizeof(*pneg_inbuf) -
881 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -0500882 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600883
884 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
885 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Long Li2796d302018-04-25 11:30:04 -0700886 (char *)pneg_inbuf, inbuflen, (char **)&pneg_rsp, &rsplen);
Steve Frenchff1c0382013-11-19 23:44:46 -0600887
888 if (rc != 0) {
889 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -0700890 rc = -EIO;
891 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -0600892 }
893
Long Li2796d302018-04-25 11:30:04 -0700894 rc = -EIO;
895 if (rsplen != sizeof(*pneg_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500896 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
897 rsplen);
898
899 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -0700900 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
901 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -0600902 }
903
904 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +0100905 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600906 goto vneg_out;
907
908 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
909 goto vneg_out;
910
911 /* do not validate server guid because not saved at negprot time yet */
912
913 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
914 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
915 goto vneg_out;
916
917 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -0700918 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600919 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -0700920 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -0600921
922vneg_out:
923 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -0700924out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200925 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -0700926out_free_inbuf:
927 kfree(pneg_inbuf);
928 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -0600929}
930
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530931enum securityEnum
932smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
933{
934 switch (requested) {
935 case Kerberos:
936 case RawNTLMSSP:
937 return requested;
938 case NTLMv2:
939 return RawNTLMSSP;
940 case Unspecified:
941 if (server->sec_ntlmssp &&
942 (global_secflags & CIFSSEC_MAY_NTLMSSP))
943 return RawNTLMSSP;
944 if ((server->sec_kerberos || server->sec_mskerberos) &&
945 (global_secflags & CIFSSEC_MAY_KRB5))
946 return Kerberos;
947 /* Fallthrough */
948 default:
949 return Unspecified;
950 }
951}
952
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100953struct SMB2_sess_data {
954 unsigned int xid;
955 struct cifs_ses *ses;
956 struct nls_table *nls_cp;
957 void (*func)(struct SMB2_sess_data *);
958 int result;
959 u64 previous_session;
960
961 /* we will send the SMB in three pieces:
962 * a fixed length beginning part, an optional
963 * SPNEGO blob (which can be zero length), and a
964 * last part which will include the strings
965 * and rest of bcc area. This allows us to avoid
966 * a large buffer 17K allocation
967 */
968 int buf0_type;
969 struct kvec iov[2];
970};
971
972static int
973SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
974{
975 int rc;
976 struct cifs_ses *ses = sess_data->ses;
977 struct smb2_sess_setup_req *req;
978 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100979 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100980
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100981 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
982 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100983 if (rc)
984 return rc;
985
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700986 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100987 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100988
989 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
990 req->PreviousSessionId = sess_data->previous_session;
991
992 req->Flags = 0; /* MBZ */
993 /* to enable echos and oplocks */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100994 req->sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100995
996 /* only one of SMB2 signing flags may be set in SMB2 request */
997 if (server->sign)
998 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
999 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1000 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1001 else
1002 req->SecurityMode = 0;
1003
1004 req->Capabilities = 0;
1005 req->Channel = 0; /* MBZ */
1006
1007 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001008 /* 1 for pad */
1009 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001010 /*
1011 * This variable will be used to clear the buffer
1012 * allocated above in case of any error in the calling function.
1013 */
1014 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1015
1016 return 0;
1017}
1018
1019static void
1020SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1021{
1022 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1023 sess_data->buf0_type = CIFS_NO_BUFFER;
1024}
1025
1026static int
1027SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1028{
1029 int rc;
1030 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001031 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001032
1033 /* Testing shows that buffer offset must be at location of Buffer[0] */
1034 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001035 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001036 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1037
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001038 /* BB add code to build os and lm fields */
1039
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001040 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
1041 sess_data->iov, 2,
1042 &sess_data->buf0_type,
1043 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001044 cifs_small_buf_release(sess_data->iov[0].iov_base);
1045 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001046
1047 return rc;
1048}
1049
1050static int
1051SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1052{
1053 int rc = 0;
1054 struct cifs_ses *ses = sess_data->ses;
1055
1056 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001057 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001058 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001059 if (rc) {
1060 cifs_dbg(FYI,
1061 "SMB3 session key generation failed\n");
1062 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001063 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001064 }
1065 }
1066 if (!ses->server->session_estab) {
1067 ses->server->sequence_number = 0x2;
1068 ses->server->session_estab = true;
1069 }
1070 mutex_unlock(&ses->server->srv_mutex);
1071
1072 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1073 spin_lock(&GlobalMid_Lock);
1074 ses->status = CifsGood;
1075 ses->need_reconnect = false;
1076 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001077 return rc;
1078}
1079
1080#ifdef CONFIG_CIFS_UPCALL
1081static void
1082SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1083{
1084 int rc;
1085 struct cifs_ses *ses = sess_data->ses;
1086 struct cifs_spnego_msg *msg;
1087 struct key *spnego_key = NULL;
1088 struct smb2_sess_setup_rsp *rsp = NULL;
1089
1090 rc = SMB2_sess_alloc_buffer(sess_data);
1091 if (rc)
1092 goto out;
1093
1094 spnego_key = cifs_get_spnego_key(ses);
1095 if (IS_ERR(spnego_key)) {
1096 rc = PTR_ERR(spnego_key);
1097 spnego_key = NULL;
1098 goto out;
1099 }
1100
1101 msg = spnego_key->payload.data[0];
1102 /*
1103 * check version field to make sure that cifs.upcall is
1104 * sending us a response in an expected form
1105 */
1106 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1107 cifs_dbg(VFS,
1108 "bad cifs.upcall version. Expected %d got %d",
1109 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1110 rc = -EKEYREJECTED;
1111 goto out_put_spnego_key;
1112 }
1113
1114 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1115 GFP_KERNEL);
1116 if (!ses->auth_key.response) {
1117 cifs_dbg(VFS,
1118 "Kerberos can't allocate (%u bytes) memory",
1119 msg->sesskey_len);
1120 rc = -ENOMEM;
1121 goto out_put_spnego_key;
1122 }
1123 ses->auth_key.len = msg->sesskey_len;
1124
1125 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1126 sess_data->iov[1].iov_len = msg->secblob_len;
1127
1128 rc = SMB2_sess_sendreceive(sess_data);
1129 if (rc)
1130 goto out_put_spnego_key;
1131
1132 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001133 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001134
1135 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001136
1137 rc = SMB2_sess_establish_session(sess_data);
1138out_put_spnego_key:
1139 key_invalidate(spnego_key);
1140 key_put(spnego_key);
1141out:
1142 sess_data->result = rc;
1143 sess_data->func = NULL;
1144 SMB2_sess_free_buffer(sess_data);
1145}
1146#else
1147static void
1148SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1149{
1150 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1151 sess_data->result = -EOPNOTSUPP;
1152 sess_data->func = NULL;
1153}
1154#endif
1155
Sachin Prabhu166cea42016-10-07 19:11:22 +01001156static void
1157SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1158
1159static void
1160SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1161{
1162 int rc;
1163 struct cifs_ses *ses = sess_data->ses;
1164 struct smb2_sess_setup_rsp *rsp = NULL;
1165 char *ntlmssp_blob = NULL;
1166 bool use_spnego = false; /* else use raw ntlmssp */
1167 u16 blob_length = 0;
1168
1169 /*
1170 * If memory allocation is successful, caller of this function
1171 * frees it.
1172 */
1173 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1174 if (!ses->ntlmssp) {
1175 rc = -ENOMEM;
1176 goto out_err;
1177 }
1178 ses->ntlmssp->sesskey_per_smbsess = true;
1179
1180 rc = SMB2_sess_alloc_buffer(sess_data);
1181 if (rc)
1182 goto out_err;
1183
1184 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1185 GFP_KERNEL);
1186 if (ntlmssp_blob == NULL) {
1187 rc = -ENOMEM;
1188 goto out;
1189 }
1190
1191 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1192 if (use_spnego) {
1193 /* BB eventually need to add this */
1194 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1195 rc = -EOPNOTSUPP;
1196 goto out;
1197 } else {
1198 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1199 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1200 }
1201 sess_data->iov[1].iov_base = ntlmssp_blob;
1202 sess_data->iov[1].iov_len = blob_length;
1203
1204 rc = SMB2_sess_sendreceive(sess_data);
1205 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1206
1207 /* If true, rc here is expected and not an error */
1208 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001209 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001210 rc = 0;
1211
1212 if (rc)
1213 goto out;
1214
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001215 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001216 le16_to_cpu(rsp->SecurityBufferOffset)) {
1217 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1218 le16_to_cpu(rsp->SecurityBufferOffset));
1219 rc = -EIO;
1220 goto out;
1221 }
1222 rc = decode_ntlmssp_challenge(rsp->Buffer,
1223 le16_to_cpu(rsp->SecurityBufferLength), ses);
1224 if (rc)
1225 goto out;
1226
1227 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1228
1229
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001230 ses->Suid = rsp->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
1233out:
1234 kfree(ntlmssp_blob);
1235 SMB2_sess_free_buffer(sess_data);
1236 if (!rc) {
1237 sess_data->result = 0;
1238 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1239 return;
1240 }
1241out_err:
1242 kfree(ses->ntlmssp);
1243 ses->ntlmssp = NULL;
1244 sess_data->result = rc;
1245 sess_data->func = NULL;
1246}
1247
1248static void
1249SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1250{
1251 int rc;
1252 struct cifs_ses *ses = sess_data->ses;
1253 struct smb2_sess_setup_req *req;
1254 struct smb2_sess_setup_rsp *rsp = NULL;
1255 unsigned char *ntlmssp_blob = NULL;
1256 bool use_spnego = false; /* else use raw ntlmssp */
1257 u16 blob_length = 0;
1258
1259 rc = SMB2_sess_alloc_buffer(sess_data);
1260 if (rc)
1261 goto out;
1262
1263 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001264 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001265
1266 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1267 sess_data->nls_cp);
1268 if (rc) {
1269 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1270 goto out;
1271 }
1272
1273 if (use_spnego) {
1274 /* BB eventually need to add this */
1275 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1276 rc = -EOPNOTSUPP;
1277 goto out;
1278 }
1279 sess_data->iov[1].iov_base = ntlmssp_blob;
1280 sess_data->iov[1].iov_len = blob_length;
1281
1282 rc = SMB2_sess_sendreceive(sess_data);
1283 if (rc)
1284 goto out;
1285
1286 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1287
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001288 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001289 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001290
1291 rc = SMB2_sess_establish_session(sess_data);
1292out:
1293 kfree(ntlmssp_blob);
1294 SMB2_sess_free_buffer(sess_data);
1295 kfree(ses->ntlmssp);
1296 ses->ntlmssp = NULL;
1297 sess_data->result = rc;
1298 sess_data->func = NULL;
1299}
1300
1301static int
1302SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1303{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301304 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001305
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301306 type = smb2_select_sectype(ses->server, ses->sectype);
1307 cifs_dbg(FYI, "sess setup type %d\n", type);
1308 if (type == Unspecified) {
1309 cifs_dbg(VFS,
1310 "Unable to select appropriate authentication method!");
1311 return -EINVAL;
1312 }
1313
1314 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001315 case Kerberos:
1316 sess_data->func = SMB2_auth_kerberos;
1317 break;
1318 case RawNTLMSSP:
1319 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1320 break;
1321 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301322 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001323 return -EOPNOTSUPP;
1324 }
1325
1326 return 0;
1327}
1328
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001329int
1330SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1331 const struct nls_table *nls_cp)
1332{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001333 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001334 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001335 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001336
Joe Perchesf96637b2013-05-04 22:12:25 -05001337 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001338
Jeff Layton3534b852013-05-24 07:41:01 -04001339 if (!server) {
1340 WARN(1, "%s: server is NULL!\n", __func__);
1341 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001342 }
1343
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001344 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1345 if (!sess_data)
1346 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001347
1348 rc = SMB2_select_sec(ses, sess_data);
1349 if (rc)
1350 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001351 sess_data->xid = xid;
1352 sess_data->ses = ses;
1353 sess_data->buf0_type = CIFS_NO_BUFFER;
1354 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001355 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001356
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001357#ifdef CONFIG_CIFS_SMB311
1358 /*
1359 * Initialize the session hash with the server one.
1360 */
1361 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1362 SMB2_PREAUTH_HASH_SIZE);
1363#endif
1364
Sachin Prabhu166cea42016-10-07 19:11:22 +01001365 while (sess_data->func)
1366 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001367
Steve Frenchc721c382017-09-19 18:40:03 -05001368 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1369 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001370 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001371out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001372 kfree(sess_data);
1373 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001374}
1375
1376int
1377SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1378{
1379 struct smb2_logoff_req *req; /* response is also trivial struct */
1380 int rc = 0;
1381 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001382 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001383 unsigned int total_len;
1384 struct kvec iov[1];
1385 struct kvec rsp_iov;
1386 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001387
Joe Perchesf96637b2013-05-04 22:12:25 -05001388 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001389
1390 if (ses && (ses->server))
1391 server = ses->server;
1392 else
1393 return -EIO;
1394
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001395 /* no need to send SMB logoff if uid already closed due to reconnect */
1396 if (ses->need_reconnect)
1397 goto smb2_session_already_dead;
1398
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001399 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001400 if (rc)
1401 return rc;
1402
1403 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001404 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001405
1406 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1407 flags |= CIFS_TRANSFORM_REQ;
1408 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001409 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001410
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001411 flags |= CIFS_NO_RESP;
1412
1413 iov[0].iov_base = (char *)req;
1414 iov[0].iov_len = total_len;
1415
1416 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001417 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001418 /*
1419 * No tcon so can't do
1420 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1421 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001422
1423smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001424 return rc;
1425}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001426
1427static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1428{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001429 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001430}
1431
1432#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1433
Steve Frenchde9f68df2013-11-15 11:26:24 -06001434/* These are similar values to what Windows uses */
1435static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1436{
1437 tcon->max_chunks = 256;
1438 tcon->max_bytes_chunk = 1048576;
1439 tcon->max_bytes_copy = 16777216;
1440}
1441
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001442int
1443SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1444 struct cifs_tcon *tcon, const struct nls_table *cp)
1445{
1446 struct smb2_tree_connect_req *req;
1447 struct smb2_tree_connect_rsp *rsp = NULL;
1448 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001449 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001450 int rc = 0;
1451 int resp_buftype;
1452 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001453 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001454 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001455 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001456
Joe Perchesf96637b2013-05-04 22:12:25 -05001457 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001458
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001459 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001460 return -EIO;
1461
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001462 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1463 if (unc_path == NULL)
1464 return -ENOMEM;
1465
1466 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1467 unc_path_len *= 2;
1468 if (unc_path_len < 2) {
1469 kfree(unc_path);
1470 return -EINVAL;
1471 }
1472
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001473 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001474 tcon->tid = 0;
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001475
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001476 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1477 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001478 if (rc) {
1479 kfree(unc_path);
1480 return rc;
1481 }
1482
Steve French5a77e752018-05-09 17:43:08 -05001483 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001484 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001485
1486 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001487 /* 1 for pad */
1488 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001489
1490 /* Testing shows that buffer offset must be at location of Buffer[0] */
1491 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001492 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001493 req->PathLength = cpu_to_le16(unc_path_len - 2);
1494 iov[1].iov_base = unc_path;
1495 iov[1].iov_len = unc_path_len;
1496
Steve French6188f282018-03-13 02:29:36 -05001497 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1498 if ((ses->server->dialect == SMB311_PROT_ID) &&
Steve French5a77e752018-05-09 17:43:08 -05001499 !smb3_encryption_required(tcon))
Steve French6188f282018-03-13 02:29:36 -05001500 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1501
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001502 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001503 cifs_small_buf_release(req);
1504 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001505
1506 if (rc != 0) {
1507 if (tcon) {
1508 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1509 tcon->need_reconnect = true;
1510 }
1511 goto tcon_error_exit;
1512 }
1513
Christophe JAILLETcd123002017-05-12 17:59:32 +02001514 switch (rsp->ShareType) {
1515 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001516 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001517 break;
1518 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001519 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001520 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001521 break;
1522 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001523 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001524 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001525 break;
1526 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001527 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001528 rc = -EOPNOTSUPP;
1529 goto tcon_error_exit;
1530 }
1531
1532 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001533 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001534 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1535 tcon->tidStatus = CifsGood;
1536 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001537 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001538 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001539
1540 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1541 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001542 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001543
1544 if (tcon->seal &&
1545 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1546 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1547
Steve Frenchde9f68df2013-11-15 11:26:24 -06001548 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001549 if (tcon->ses->server->ops->validate_negotiate)
1550 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001551tcon_exit:
1552 free_rsp_buf(resp_buftype, rsp);
1553 kfree(unc_path);
1554 return rc;
1555
1556tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001557 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001558 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001559 }
1560 goto tcon_exit;
1561}
1562
1563int
1564SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1565{
1566 struct smb2_tree_disconnect_req *req; /* response is trivial */
1567 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001568 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001569 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001570 unsigned int total_len;
1571 struct kvec iov[1];
1572 struct kvec rsp_iov;
1573 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001574
Joe Perchesf96637b2013-05-04 22:12:25 -05001575 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001576
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001577 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001578 return -EIO;
1579
1580 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1581 return 0;
1582
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001583 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1584 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001585 if (rc)
1586 return rc;
1587
Steve French5a77e752018-05-09 17:43:08 -05001588 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001589 flags |= CIFS_TRANSFORM_REQ;
1590
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001591 flags |= CIFS_NO_RESP;
1592
1593 iov[0].iov_base = (char *)req;
1594 iov[0].iov_len = total_len;
1595
1596 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001597 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001598 if (rc)
1599 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1600
1601 return rc;
1602}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001603
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001604
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001605static struct create_durable *
1606create_durable_buf(void)
1607{
1608 struct create_durable *buf;
1609
1610 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1611 if (!buf)
1612 return NULL;
1613
1614 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001615 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001616 buf->ccontext.DataLength = cpu_to_le32(16);
1617 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1618 (struct create_durable, Name));
1619 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001620 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001621 buf->Name[0] = 'D';
1622 buf->Name[1] = 'H';
1623 buf->Name[2] = 'n';
1624 buf->Name[3] = 'Q';
1625 return buf;
1626}
1627
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001628static struct create_durable *
1629create_reconnect_durable_buf(struct cifs_fid *fid)
1630{
1631 struct create_durable *buf;
1632
1633 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1634 if (!buf)
1635 return NULL;
1636
1637 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1638 (struct create_durable, Data));
1639 buf->ccontext.DataLength = cpu_to_le32(16);
1640 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1641 (struct create_durable, Name));
1642 buf->ccontext.NameLength = cpu_to_le16(4);
1643 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1644 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001645 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001646 buf->Name[0] = 'D';
1647 buf->Name[1] = 'H';
1648 buf->Name[2] = 'n';
1649 buf->Name[3] = 'C';
1650 return buf;
1651}
1652
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001653static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001654parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001655 unsigned int *epoch, char *lease_key)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001656{
1657 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001658 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001659 unsigned int next;
1660 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001661 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001662
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001663 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001664 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001665 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001666 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001667 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001668 if (le16_to_cpu(cc->NameLength) == 4 &&
1669 strncmp(name, "RqLs", 4) == 0)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001670 return server->ops->parse_lease_buf(cc, epoch,
1671 lease_key);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001672
1673 next = le32_to_cpu(cc->Next);
1674 if (!next)
1675 break;
1676 remaining -= next;
1677 cc = (struct create_context *)((char *)cc + next);
1678 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001679
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001680 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001681}
1682
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001683static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001684add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1685 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001686{
1687 struct smb2_create_req *req = iov[0].iov_base;
1688 unsigned int num = *num_iovec;
1689
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001690 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001691 if (iov[num].iov_base == NULL)
1692 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001693 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001694 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1695 if (!req->CreateContextsOffset)
1696 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001697 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001698 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001699 le32_add_cpu(&req->CreateContextsLength,
1700 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001701 *num_iovec = num + 1;
1702 return 0;
1703}
1704
Steve Frenchb56eae42015-11-03 09:26:27 -06001705static struct create_durable_v2 *
1706create_durable_v2_buf(struct cifs_fid *pfid)
1707{
1708 struct create_durable_v2 *buf;
1709
1710 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1711 if (!buf)
1712 return NULL;
1713
1714 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1715 (struct create_durable_v2, dcontext));
1716 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1717 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1718 (struct create_durable_v2, Name));
1719 buf->ccontext.NameLength = cpu_to_le16(4);
1720
1721 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1722 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001723 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001724 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1725
1726 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1727 buf->Name[0] = 'D';
1728 buf->Name[1] = 'H';
1729 buf->Name[2] = '2';
1730 buf->Name[3] = 'Q';
1731 return buf;
1732}
1733
1734static struct create_durable_handle_reconnect_v2 *
1735create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1736{
1737 struct create_durable_handle_reconnect_v2 *buf;
1738
1739 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1740 GFP_KERNEL);
1741 if (!buf)
1742 return NULL;
1743
1744 buf->ccontext.DataOffset =
1745 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1746 dcontext));
1747 buf->ccontext.DataLength =
1748 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1749 buf->ccontext.NameOffset =
1750 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1751 Name));
1752 buf->ccontext.NameLength = cpu_to_le16(4);
1753
1754 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1755 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1756 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1757 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1758
1759 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1760 buf->Name[0] = 'D';
1761 buf->Name[1] = 'H';
1762 buf->Name[2] = '2';
1763 buf->Name[3] = 'C';
1764 return buf;
1765}
1766
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001767static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001768add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001769 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001770{
1771 struct smb2_create_req *req = iov[0].iov_base;
1772 unsigned int num = *num_iovec;
1773
Steve Frenchb56eae42015-11-03 09:26:27 -06001774 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1775 if (iov[num].iov_base == NULL)
1776 return -ENOMEM;
1777 iov[num].iov_len = sizeof(struct create_durable_v2);
1778 if (!req->CreateContextsOffset)
1779 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001780 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001781 iov[1].iov_len);
1782 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001783 *num_iovec = num + 1;
1784 return 0;
1785}
1786
1787static int
1788add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1789 struct cifs_open_parms *oparms)
1790{
1791 struct smb2_create_req *req = iov[0].iov_base;
1792 unsigned int num = *num_iovec;
1793
1794 /* indicate that we don't need to relock the file */
1795 oparms->reconnect = false;
1796
1797 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1798 if (iov[num].iov_base == NULL)
1799 return -ENOMEM;
1800 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1801 if (!req->CreateContextsOffset)
1802 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001803 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001804 iov[1].iov_len);
1805 le32_add_cpu(&req->CreateContextsLength,
1806 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001807 *num_iovec = num + 1;
1808 return 0;
1809}
1810
1811static int
1812add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1813 struct cifs_open_parms *oparms, bool use_persistent)
1814{
1815 struct smb2_create_req *req = iov[0].iov_base;
1816 unsigned int num = *num_iovec;
1817
1818 if (use_persistent) {
1819 if (oparms->reconnect)
1820 return add_durable_reconnect_v2_context(iov, num_iovec,
1821 oparms);
1822 else
1823 return add_durable_v2_context(iov, num_iovec, oparms);
1824 }
1825
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001826 if (oparms->reconnect) {
1827 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1828 /* indicate that we don't need to relock the file */
1829 oparms->reconnect = false;
1830 } else
1831 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001832 if (iov[num].iov_base == NULL)
1833 return -ENOMEM;
1834 iov[num].iov_len = sizeof(struct create_durable);
1835 if (!req->CreateContextsOffset)
1836 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001837 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001838 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001839 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001840 *num_iovec = num + 1;
1841 return 0;
1842}
1843
Aurelien Aptelf0712922017-02-22 14:47:17 +01001844static int
1845alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1846 const char *treename, const __le16 *path)
1847{
1848 int treename_len, path_len;
1849 struct nls_table *cp;
1850 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1851
1852 /*
1853 * skip leading "\\"
1854 */
1855 treename_len = strlen(treename);
1856 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1857 return -EINVAL;
1858
1859 treename += 2;
1860 treename_len -= 2;
1861
1862 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1863
1864 /*
1865 * make room for one path separator between the treename and
1866 * path
1867 */
1868 *out_len = treename_len + 1 + path_len;
1869
1870 /*
1871 * final path needs to be null-terminated UTF16 with a
1872 * size aligned to 8
1873 */
1874
1875 *out_size = roundup((*out_len+1)*2, 8);
1876 *out_path = kzalloc(*out_size, GFP_KERNEL);
1877 if (!*out_path)
1878 return -ENOMEM;
1879
1880 cp = load_nls_default();
1881 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1882 UniStrcat(*out_path, sep);
1883 UniStrcat(*out_path, path);
1884 unload_nls(cp);
1885
1886 return 0;
1887}
1888
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001889int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001890SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001891 __u8 *oplock, struct smb2_file_all_info *buf,
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001892 struct kvec *err_iov)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001893{
1894 struct smb2_create_req *req;
1895 struct smb2_create_rsp *rsp;
1896 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001897 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001898 struct cifs_ses *ses = tcon->ses;
Steve Frenchce558b02018-05-31 19:16:54 -05001899 struct kvec iov[5]; /* make sure at least one for each open context */
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001900 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001901 int resp_buftype;
1902 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001903 __le16 *copy_path = NULL;
1904 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001905 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001906 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001907 __u32 file_attributes = 0;
Steve Frenchce558b02018-05-31 19:16:54 -05001908 char *dhc_buf = NULL, *lc_buf = NULL, *pc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001909 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001910 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001911
Joe Perchesf96637b2013-05-04 22:12:25 -05001912 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001913
1914 if (ses && (ses->server))
1915 server = ses->server;
1916 else
1917 return -EIO;
1918
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001919 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1920
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001921 if (rc)
1922 return rc;
1923
Steve French5a77e752018-05-09 17:43:08 -05001924 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001925 flags |= CIFS_TRANSFORM_REQ;
1926
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001927 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001928 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001929 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1930 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001931
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001932 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001933 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001934 /* File attributes ignored on open (used in create though) */
1935 req->FileAttributes = cpu_to_le32(file_attributes);
1936 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001937 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1938 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001939
1940 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001941 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001942 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001943
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001944 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01001945
1946 /* [MS-SMB2] 2.2.13 NameOffset:
1947 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1948 * the SMB2 header, the file name includes a prefix that will
1949 * be processed during DFS name normalization as specified in
1950 * section 3.3.5.9. Otherwise, the file name is relative to
1951 * the share that is identified by the TreeId in the SMB2
1952 * header.
1953 */
1954 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1955 int name_len;
1956
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001957 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001958 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1959 &name_len,
1960 tcon->treeName, path);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001961 if (rc) {
1962 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001963 return rc;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001964 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001965 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001966 uni_path_len = copy_size;
1967 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001968 } else {
1969 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1970 /* MUST set path len (NameLength) to 0 opening root of share */
1971 req->NameLength = cpu_to_le16(uni_path_len - 2);
1972 if (uni_path_len % 8 != 0) {
1973 copy_size = roundup(uni_path_len, 8);
1974 copy_path = kzalloc(copy_size, GFP_KERNEL);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001975 if (!copy_path) {
1976 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001977 return -ENOMEM;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001978 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001979 memcpy((char *)copy_path, (const char *)path,
1980 uni_path_len);
1981 uni_path_len = copy_size;
1982 path = copy_path;
1983 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001984 }
1985
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001986 iov[1].iov_len = uni_path_len;
1987 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001988
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001989 if (!server->oplocks)
1990 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1991
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001992 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001993 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1994 req->RequestedOplockLevel = *oplock;
1995 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001996 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001997 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001998 cifs_small_buf_release(req);
1999 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002000 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002001 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002002 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002003 }
2004
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002005 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2006 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002007 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002008 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002009 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002010 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002011 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002012 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002013
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002014 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002015 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002016 if (rc) {
2017 cifs_small_buf_release(req);
2018 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04002019 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002020 return rc;
2021 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002022 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002023 }
2024
Steve Frenchce558b02018-05-31 19:16:54 -05002025#ifdef CONFIG_CIFS_SMB311
2026 if (tcon->posix_extensions) {
2027 if (n_iov > 2) {
2028 struct create_context *ccontext =
2029 (struct create_context *)iov[n_iov-1].iov_base;
2030 ccontext->Next =
2031 cpu_to_le32(iov[n_iov-1].iov_len);
2032 }
2033
2034 rc = add_posix_context(iov, &n_iov, oparms->mode);
2035 if (rc) {
2036 cifs_small_buf_release(req);
2037 kfree(copy_path);
2038 kfree(lc_buf);
2039 kfree(dhc_buf);
2040 return rc;
2041 }
2042 pc_buf = iov[n_iov-1].iov_base;
2043 }
2044#endif /* SMB311 */
2045
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002046 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2047 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002048 cifs_small_buf_release(req);
2049 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002050
2051 if (rc != 0) {
2052 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002053 if (err_iov && rsp) {
2054 *err_iov = rsp_iov;
2055 resp_buftype = CIFS_NO_BUFFER;
2056 rsp = NULL;
2057 }
Steve French28d59362018-05-30 21:42:34 -05002058 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2059 oparms->create_options, oparms->desired_access, rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002060 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002061 } else
2062 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2063 ses->Suid, oparms->create_options,
2064 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002065
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002066 oparms->fid->persistent_fid = rsp->PersistentFileId;
2067 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002068
2069 if (buf) {
2070 memcpy(buf, &rsp->CreationTime, 32);
2071 buf->AllocationSize = rsp->AllocationSize;
2072 buf->EndOfFile = rsp->EndofFile;
2073 buf->Attributes = rsp->FileAttributes;
2074 buf->NumberOfLinks = cpu_to_le32(1);
2075 buf->DeletePending = 0;
2076 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002077
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002078 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002079 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch,
2080 oparms->fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002081 else
2082 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002083creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002084 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04002085 kfree(lc_buf);
2086 kfree(dhc_buf);
Steve Frenchce558b02018-05-31 19:16:54 -05002087 kfree(pc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002088 free_rsp_buf(resp_buftype, rsp);
2089 return rc;
2090}
2091
Steve French4a72daf2013-06-25 00:20:49 -05002092/*
2093 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2094 */
2095int
2096SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002097 u64 volatile_fid, u32 opcode, bool is_fsctl,
Aurelien Aptel51146622017-02-28 15:08:41 +01002098 char *in_data, u32 indatalen,
2099 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05002100{
2101 struct smb2_ioctl_req *req;
2102 struct smb2_ioctl_rsp *rsp;
Steve French8e353102015-03-26 19:47:02 -05002103 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05002104 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002105 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002106 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002107 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002108 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002109 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002110 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05002111
2112 cifs_dbg(FYI, "SMB2 IOCTL\n");
2113
Steve French3d1a3742014-08-11 21:05:25 -05002114 if (out_data != NULL)
2115 *out_data = NULL;
2116
Steve French4a72daf2013-06-25 00:20:49 -05002117 /* zero out returned data len, in case of error */
2118 if (plen)
2119 *plen = 0;
2120
Steve French8e353102015-03-26 19:47:02 -05002121 if (tcon)
2122 ses = tcon->ses;
2123 else
2124 return -EIO;
2125
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002126 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05002127 return -EIO;
2128
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002129 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002130 if (rc)
2131 return rc;
2132
Steve French5a77e752018-05-09 17:43:08 -05002133 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002134 flags |= CIFS_TRANSFORM_REQ;
2135
Steve French4a72daf2013-06-25 00:20:49 -05002136 req->CtlCode = cpu_to_le32(opcode);
2137 req->PersistentFileId = persistent_fid;
2138 req->VolatileFileId = volatile_fid;
2139
2140 if (indatalen) {
2141 req->InputCount = cpu_to_le32(indatalen);
2142 /* do not set InputOffset if no input data */
2143 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002144 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05002145 iov[1].iov_base = in_data;
2146 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002147 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05002148 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002149 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05002150
2151 req->OutputOffset = 0;
2152 req->OutputCount = 0; /* MBZ */
2153
2154 /*
2155 * Could increase MaxOutputResponse, but that would require more
2156 * than one credit. Windows typically sets this smaller, but for some
2157 * ioctls it may be useful to allow server to send more. No point
2158 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05002159 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2160 * (by default, note that it can be overridden to make max larger)
2161 * in responses (except for read responses which can be bigger.
2162 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05002163 */
Steve French7db0a6e2017-05-03 21:12:20 -05002164 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05002165
2166 if (is_fsctl)
2167 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2168 else
2169 req->Flags = 0;
2170
2171 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05002172
Steve French7ff8d452013-10-14 00:44:19 -05002173 /*
2174 * If no input data, the size of ioctl struct in
2175 * protocol spec still includes a 1 byte data buffer,
2176 * but if input data passed to ioctl, we do not
2177 * want to double count this, so we do not send
2178 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002179 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05002180 */
2181
2182 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002183 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05002184 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002185 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05002186
Steve French4587eee2017-10-25 15:58:31 -05002187 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2188 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002189 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002190
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002191 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2192 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002193 cifs_small_buf_release(req);
2194 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002195
Steve Frencheccb4422018-05-17 21:16:55 -05002196 if (rc != 0)
2197 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2198 ses->Suid, 0, opcode, rc);
2199
Steve French9bf0c9c2013-11-16 18:05:28 -06002200 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002201 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002202 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002203 } else if (rc == -EINVAL) {
2204 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2205 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002206 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002207 goto ioctl_exit;
2208 }
Steve French4a72daf2013-06-25 00:20:49 -05002209 }
2210
2211 /* check if caller wants to look at return data or just return rc */
2212 if ((plen == NULL) || (out_data == NULL))
2213 goto ioctl_exit;
2214
2215 *plen = le32_to_cpu(rsp->OutputCount);
2216
2217 /* We check for obvious errors in the output buffer length and offset */
2218 if (*plen == 0)
2219 goto ioctl_exit; /* server returned no data */
2220 else if (*plen > 0xFF00) {
2221 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2222 *plen = 0;
2223 rc = -EIO;
2224 goto ioctl_exit;
2225 }
2226
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002227 if (rsp_iov.iov_len < le32_to_cpu(rsp->OutputOffset) + *plen) {
Steve French4a72daf2013-06-25 00:20:49 -05002228 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2229 le32_to_cpu(rsp->OutputOffset));
2230 *plen = 0;
2231 rc = -EIO;
2232 goto ioctl_exit;
2233 }
2234
2235 *out_data = kmalloc(*plen, GFP_KERNEL);
2236 if (*out_data == NULL) {
2237 rc = -ENOMEM;
2238 goto ioctl_exit;
2239 }
2240
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002241 memcpy(*out_data, (char *)rsp + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002242ioctl_exit:
2243 free_rsp_buf(resp_buftype, rsp);
2244 return rc;
2245}
2246
Steve French64a5cfa2013-10-14 15:31:32 -05002247/*
2248 * Individual callers to ioctl worker function follow
2249 */
2250
2251int
2252SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2253 u64 persistent_fid, u64 volatile_fid)
2254{
2255 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002256 struct compress_ioctl fsctl_input;
2257 char *ret_data = NULL;
2258
2259 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002260 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002261
2262 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2263 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2264 (char *)&fsctl_input /* data input */,
2265 2 /* in data len */, &ret_data /* out data */, NULL);
2266
2267 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002268
2269 return rc;
2270}
2271
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002272int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002273SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2274 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002275{
2276 struct smb2_close_req *req;
2277 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002278 struct cifs_ses *ses = tcon->ses;
2279 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002280 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002281 int resp_buftype;
2282 int rc = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002283 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002284
Joe Perchesf96637b2013-05-04 22:12:25 -05002285 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002286
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002287 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002288 return -EIO;
2289
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002290 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002291 if (rc)
2292 return rc;
2293
Steve French5a77e752018-05-09 17:43:08 -05002294 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002295 flags |= CIFS_TRANSFORM_REQ;
2296
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002297 req->PersistentFileId = persistent_fid;
2298 req->VolatileFileId = volatile_fid;
2299
2300 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002301 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002302
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002303 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002304 cifs_small_buf_release(req);
2305 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002306
2307 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002308 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002309 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2310 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002311 goto close_exit;
2312 }
2313
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002314 /* BB FIXME - decode close response, update inode for caching */
2315
2316close_exit:
2317 free_rsp_buf(resp_buftype, rsp);
2318 return rc;
2319}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002320
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002321int
2322SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2323 u64 persistent_fid, u64 volatile_fid)
2324{
2325 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2326}
2327
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002328static int
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002329validate_iov(unsigned int offset, unsigned int buffer_length,
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002330 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002331{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002332 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002333 char *end_of_smb = smb_len + (char *)iov->iov_base;
2334 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002335 char *end_of_buf = begin_of_buf + buffer_length;
2336
2337
2338 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002339 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2340 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002341 return -EINVAL;
2342 }
2343
2344 /* check if beyond RFC1001 maximum length */
2345 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002346 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2347 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002348 return -EINVAL;
2349 }
2350
2351 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002352 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002353 return -EINVAL;
2354 }
2355
2356 return 0;
2357}
2358
2359/*
2360 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2361 * Caller must free buffer.
2362 */
2363static int
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002364validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002365 struct kvec *iov, unsigned int minbufsize,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002366 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002367{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002368 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002369 int rc;
2370
2371 if (!data)
2372 return -EINVAL;
2373
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002374 rc = validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002375 if (rc)
2376 return rc;
2377
2378 memcpy(data, begin_of_buf, buffer_length);
2379
2380 return 0;
2381}
2382
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002383static int
2384query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002385 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2386 u32 additional_info, size_t output_len, size_t min_len, void **data,
2387 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002388{
2389 struct smb2_query_info_req *req;
2390 struct smb2_query_info_rsp *rsp = NULL;
2391 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002392 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002393 int rc = 0;
2394 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002395 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002396 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002397 unsigned int total_len;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002398
Joe Perchesf96637b2013-05-04 22:12:25 -05002399 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002400
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002401 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002402 return -EIO;
2403
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002404 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2405 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002406 if (rc)
2407 return rc;
2408
Steve French5a77e752018-05-09 17:43:08 -05002409 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002410 flags |= CIFS_TRANSFORM_REQ;
2411
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002412 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002413 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002414 req->PersistentFileId = persistent_fid;
2415 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002416 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002417
2418 /*
2419 * We do not use the input buffer (do not send extra byte)
2420 */
2421 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002422
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002423 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002424
2425 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002426 /* 1 for Buffer */
2427 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002428
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002429 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002430 cifs_small_buf_release(req);
2431 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002432
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002433 if (rc) {
2434 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002435 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2436 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002437 goto qinf_exit;
2438 }
2439
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002440 if (dlen) {
2441 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2442 if (!*data) {
2443 *data = kmalloc(*dlen, GFP_KERNEL);
2444 if (!*data) {
2445 cifs_dbg(VFS,
2446 "Error %d allocating memory for acl\n",
2447 rc);
2448 *dlen = 0;
2449 goto qinf_exit;
2450 }
2451 }
2452 }
2453
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002454 rc = validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002455 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002456 &rsp_iov, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002457
2458qinf_exit:
2459 free_rsp_buf(resp_buftype, rsp);
2460 return rc;
2461}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002462
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002463int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002464 u64 persistent_fid, u64 volatile_fid,
2465 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002466{
2467 return query_info(xid, tcon, persistent_fid, volatile_fid,
2468 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002469 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002470 sizeof(struct smb2_file_full_ea_info),
2471 (void **)&data,
2472 NULL);
2473}
2474
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002475int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2476 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002477{
2478 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002479 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002480 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002481 sizeof(struct smb2_file_all_info), (void **)&data,
2482 NULL);
2483}
2484
2485int
2486SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2487 u64 persistent_fid, u64 volatile_fid,
2488 void **data, u32 *plen)
2489{
2490 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2491 *plen = 0;
2492
2493 return query_info(xid, tcon, persistent_fid, volatile_fid,
2494 0, SMB2_O_INFO_SECURITY, additional_info,
2495 SMB2_MAX_BUFFER_SIZE,
2496 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002497}
2498
2499int
2500SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2501 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2502{
2503 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002504 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002505 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002506 sizeof(struct smb2_file_internal_info),
2507 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002508}
2509
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002510/*
2511 * This is a no-op for now. We're not really interested in the reply, but
2512 * rather in the fact that the server sent one and that server->lstrp
2513 * gets updated.
2514 *
2515 * FIXME: maybe we should consider checking that the reply matches request?
2516 */
2517static void
2518smb2_echo_callback(struct mid_q_entry *mid)
2519{
2520 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002521 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002522 unsigned int credits_received = 1;
2523
2524 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002525 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002526
2527 DeleteMidQEntry(mid);
2528 add_credits(server, credits_received, CIFS_ECHO_OP);
2529}
2530
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002531void smb2_reconnect_server(struct work_struct *work)
2532{
2533 struct TCP_Server_Info *server = container_of(work,
2534 struct TCP_Server_Info, reconnect.work);
2535 struct cifs_ses *ses;
2536 struct cifs_tcon *tcon, *tcon2;
2537 struct list_head tmp_list;
2538 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002539 int rc;
2540 int resched = false;
2541
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002542
2543 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2544 mutex_lock(&server->reconnect_mutex);
2545
2546 INIT_LIST_HEAD(&tmp_list);
2547 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2548
2549 spin_lock(&cifs_tcp_ses_lock);
2550 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2551 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002552 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002553 tcon->tc_count++;
2554 list_add_tail(&tcon->rlist, &tmp_list);
2555 tcon_exist = true;
2556 }
2557 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002558 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2559 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2560 tcon_exist = true;
2561 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002562 }
2563 /*
2564 * Get the reference to server struct to be sure that the last call of
2565 * cifs_put_tcon() in the loop below won't release the server pointer.
2566 */
2567 if (tcon_exist)
2568 server->srv_count++;
2569
2570 spin_unlock(&cifs_tcp_ses_lock);
2571
2572 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002573 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2574 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002575 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002576 else
2577 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002578 list_del_init(&tcon->rlist);
2579 cifs_put_tcon(tcon);
2580 }
2581
2582 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002583 if (resched)
2584 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002585 mutex_unlock(&server->reconnect_mutex);
2586
2587 /* now we can safely release srv struct */
2588 if (tcon_exist)
2589 cifs_put_tcp_session(server, 1);
2590}
2591
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002592int
2593SMB2_echo(struct TCP_Server_Info *server)
2594{
2595 struct smb2_echo_req *req;
2596 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002597 struct kvec iov[2];
2598 struct smb_rqst rqst = { .rq_iov = iov,
2599 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002600 unsigned int total_len;
2601 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002602
Joe Perchesf96637b2013-05-04 22:12:25 -05002603 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002604
Steve French4fcd1812016-06-22 20:12:05 -05002605 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002606 /* No need to send echo on newly established connections */
2607 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2608 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002609 }
2610
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002611 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002612 if (rc)
2613 return rc;
2614
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002615 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002616
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002617 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002618 rfc1002_marker = cpu_to_be32(total_len);
2619 iov[0].iov_base = &rfc1002_marker;
2620 iov[1].iov_len = total_len;
2621 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002622
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002623 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2624 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002625 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002626 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002627
2628 cifs_small_buf_release(req);
2629 return rc;
2630}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002631
2632int
2633SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2634 u64 volatile_fid)
2635{
2636 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002637 struct cifs_ses *ses = tcon->ses;
2638 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002639 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002640 int resp_buftype;
2641 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002642 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002643 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002644
Joe Perchesf96637b2013-05-04 22:12:25 -05002645 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002646
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002647 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002648 return -EIO;
2649
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002650 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002651 if (rc)
2652 return rc;
2653
Steve French5a77e752018-05-09 17:43:08 -05002654 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002655 flags |= CIFS_TRANSFORM_REQ;
2656
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002657 req->PersistentFileId = persistent_fid;
2658 req->VolatileFileId = volatile_fid;
2659
2660 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002661 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002662
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002663 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002664 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002665
Steve Frencheccb4422018-05-17 21:16:55 -05002666 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002667 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002668 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
2669 rc);
2670 }
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002671
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002672 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002673 return rc;
2674}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002675
2676/*
2677 * To form a chain of read requests, any read requests after the first should
2678 * have the end_of_chain boolean set to true.
2679 */
2680static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002681smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002682 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2683 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002684{
2685 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002686 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002687 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002688 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002689
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002690 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2691 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002692 if (rc)
2693 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002694
2695 server = io_parms->tcon->ses->server;
2696 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002697 return -ECONNABORTED;
2698
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002699 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002700 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002701
2702 req->PersistentFileId = io_parms->persistent_fid;
2703 req->VolatileFileId = io_parms->volatile_fid;
2704 req->ReadChannelInfoOffset = 0; /* reserved */
2705 req->ReadChannelInfoLength = 0; /* reserved */
2706 req->Channel = 0; /* reserved */
2707 req->MinimumCount = 0;
2708 req->Length = cpu_to_le32(io_parms->length);
2709 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07002710#ifdef CONFIG_CIFS_SMB_DIRECT
2711 /*
2712 * If we want to do a RDMA write, fill in and append
2713 * smbd_buffer_descriptor_v1 to the end of read request
2714 */
Long Libb4c0412018-04-17 12:17:08 -07002715 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07002716 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002717
Long Libd3dcc62017-11-22 17:38:47 -07002718 struct smbd_buffer_descriptor_v1 *v1;
2719 bool need_invalidate =
2720 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2721
2722 rdata->mr = smbd_register_mr(
2723 server->smbd_conn, rdata->pages,
2724 rdata->nr_pages, rdata->tailsz,
2725 true, need_invalidate);
2726 if (!rdata->mr)
2727 return -ENOBUFS;
2728
2729 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2730 if (need_invalidate)
2731 req->Channel = SMB2_CHANNEL_RDMA_V1;
2732 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002733 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07002734 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002735 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07002736 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002737 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2738 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2739 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07002740
2741 *total_len += sizeof(*v1) - 1;
2742 }
2743#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002744 if (request_type & CHAINED_REQUEST) {
2745 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002746 /* next 8-byte aligned request */
2747 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2748 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002749 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002750 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002751 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002752 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002753 /*
2754 * Related requests use info from previous read request
2755 * in chain.
2756 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002757 shdr->SessionId = 0xFFFFFFFF;
2758 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002759 req->PersistentFileId = 0xFFFFFFFF;
2760 req->VolatileFileId = 0xFFFFFFFF;
2761 }
2762 }
2763 if (remaining_bytes > io_parms->length)
2764 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2765 else
2766 req->RemainingBytes = 0;
2767
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002768 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002769 return rc;
2770}
2771
2772static void
2773smb2_readv_callback(struct mid_q_entry *mid)
2774{
2775 struct cifs_readdata *rdata = mid->callback_data;
2776 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2777 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002778 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002779 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002780 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002781 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2782 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002783 .rq_pages = rdata->pages,
2784 .rq_npages = rdata->nr_pages,
2785 .rq_pagesz = rdata->pagesz,
2786 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002787
Joe Perchesf96637b2013-05-04 22:12:25 -05002788 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2789 __func__, mid->mid, mid->mid_state, rdata->result,
2790 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002791
2792 switch (mid->mid_state) {
2793 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002794 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002795 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002796 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002797 int rc;
2798
Jeff Layton0b688cf2012-09-18 16:20:34 -07002799 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002800 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002801 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2802 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002803 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002804 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002805 task_io_account_read(rdata->got_bytes);
2806 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002807 break;
2808 case MID_REQUEST_SUBMITTED:
2809 case MID_RETRY_NEEDED:
2810 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002811 if (server->sign && rdata->got_bytes)
2812 /* reset bytes number since we can not check a sign */
2813 rdata->got_bytes = 0;
2814 /* FIXME: should this be counted toward the initiating task? */
2815 task_io_account_read(rdata->got_bytes);
2816 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002817 break;
2818 default:
2819 if (rdata->result != -ENODATA)
2820 rdata->result = -EIO;
2821 }
Long Libd3dcc62017-11-22 17:38:47 -07002822#ifdef CONFIG_CIFS_SMB_DIRECT
2823 /*
2824 * If this rdata has a memmory registered, the MR can be freed
2825 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2826 * because they have limited number and are used for future I/Os
2827 */
2828 if (rdata->mr) {
2829 smbd_deregister_mr(rdata->mr);
2830 rdata->mr = NULL;
2831 }
2832#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002833 if (rdata->result)
2834 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2835
2836 queue_work(cifsiod_wq, &rdata->work);
2837 DeleteMidQEntry(mid);
2838 add_credits(server, credits_received, 0);
2839}
2840
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002841/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002842int
2843smb2_async_readv(struct cifs_readdata *rdata)
2844{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002845 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002846 char *buf;
2847 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002848 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002849 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2850 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002851 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002852 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002853 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002854
Joe Perchesf96637b2013-05-04 22:12:25 -05002855 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2856 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002857
2858 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2859 io_parms.offset = rdata->offset;
2860 io_parms.length = rdata->bytes;
2861 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2862 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2863 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002864
2865 server = io_parms.tcon->ses->server;
2866
Long Li2dabfd52017-11-07 01:54:53 -07002867 rc = smb2_new_read_req(
2868 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002869 if (rc) {
2870 if (rc == -EAGAIN && rdata->credits) {
2871 /* credits was reset by reconnect */
2872 rdata->credits = 0;
2873 /* reduce in_flight value since we won't send the req */
2874 spin_lock(&server->req_lock);
2875 server->in_flight--;
2876 spin_unlock(&server->req_lock);
2877 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002878 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002879 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002880
Steve French5a77e752018-05-09 17:43:08 -05002881 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002882 flags |= CIFS_TRANSFORM_REQ;
2883
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002884 req_len = cpu_to_be32(total_len);
2885
2886 rdata->iov[0].iov_base = &req_len;
2887 rdata->iov[0].iov_len = sizeof(__be32);
2888 rdata->iov[1].iov_base = buf;
2889 rdata->iov[1].iov_len = total_len;
2890
2891 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002892
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002893 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002894 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002895 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002896 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002897 spin_lock(&server->req_lock);
2898 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002899 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002900 spin_unlock(&server->req_lock);
2901 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002902 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002903 }
2904
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002905 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002906 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002907 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002908 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002909 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002910 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002911 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002912 trace_smb3_read_err(rc, 0 /* xid */, io_parms.persistent_fid,
2913 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
2914 io_parms.offset, io_parms.length);
2915 } else
2916 trace_smb3_read_done(0 /* xid */, io_parms.persistent_fid,
2917 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
2918 io_parms.offset, io_parms.length);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002919
2920 cifs_small_buf_release(buf);
2921 return rc;
2922}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002923
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002924int
2925SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2926 unsigned int *nbytes, char **buf, int *buf_type)
2927{
2928 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002929 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002930 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002931 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002932 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002933 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002934 int flags = CIFS_LOG_ERROR;
2935 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002936
2937 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07002938 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002939 if (rc)
2940 return rc;
2941
Steve French5a77e752018-05-09 17:43:08 -05002942 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002943 flags |= CIFS_TRANSFORM_REQ;
2944
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002945 iov[0].iov_base = (char *)req;
2946 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002947
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002948 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002949 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002950
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002951 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002952
2953 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002954 if (rc != -ENODATA) {
2955 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2956 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002957 }
Steve Frencheccb4422018-05-17 21:16:55 -05002958 trace_smb3_read_err(rc, xid, req->PersistentFileId,
2959 io_parms->tcon->tid, ses->Suid,
2960 io_parms->offset, io_parms->length);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002961 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2962 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05002963 } else
2964 trace_smb3_read_done(xid, req->PersistentFileId,
2965 io_parms->tcon->tid, ses->Suid,
2966 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002967
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002968 *nbytes = le32_to_cpu(rsp->DataLength);
2969 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2970 (*nbytes > io_parms->length)) {
2971 cifs_dbg(FYI, "bad length %d for count %d\n",
2972 *nbytes, io_parms->length);
2973 rc = -EIO;
2974 *nbytes = 0;
2975 }
2976
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002977 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002978 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002979 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002980 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002981 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002982 if (resp_buftype == CIFS_SMALL_BUFFER)
2983 *buf_type = CIFS_SMALL_BUFFER;
2984 else if (resp_buftype == CIFS_LARGE_BUFFER)
2985 *buf_type = CIFS_LARGE_BUFFER;
2986 }
2987 return rc;
2988}
2989
Pavel Shilovsky33319142012-09-18 16:20:29 -07002990/*
2991 * Check the mid_state and signature on received buffer (if any), and queue the
2992 * workqueue completion task.
2993 */
2994static void
2995smb2_writev_callback(struct mid_q_entry *mid)
2996{
2997 struct cifs_writedata *wdata = mid->callback_data;
2998 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2999 unsigned int written;
3000 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3001 unsigned int credits_received = 1;
3002
3003 switch (mid->mid_state) {
3004 case MID_RESPONSE_RECEIVED:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003005 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003006 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
3007 if (wdata->result != 0)
3008 break;
3009
3010 written = le32_to_cpu(rsp->DataLength);
3011 /*
3012 * Mask off high 16 bits when bytes written as returned
3013 * by the server is greater than bytes requested by the
3014 * client. OS/2 servers are known to set incorrect
3015 * CountHigh values.
3016 */
3017 if (written > wdata->bytes)
3018 written &= 0xFFFF;
3019
3020 if (written < wdata->bytes)
3021 wdata->result = -ENOSPC;
3022 else
3023 wdata->bytes = written;
3024 break;
3025 case MID_REQUEST_SUBMITTED:
3026 case MID_RETRY_NEEDED:
3027 wdata->result = -EAGAIN;
3028 break;
3029 default:
3030 wdata->result = -EIO;
3031 break;
3032 }
Long Lidb223a52017-11-22 17:38:45 -07003033#ifdef CONFIG_CIFS_SMB_DIRECT
3034 /*
3035 * If this wdata has a memory registered, the MR can be freed
3036 * The number of MRs available is limited, it's important to recover
3037 * used MR as soon as I/O is finished. Hold MR longer in the later
3038 * I/O process can possibly result in I/O deadlock due to lack of MR
3039 * to send request on I/O retry
3040 */
3041 if (wdata->mr) {
3042 smbd_deregister_mr(wdata->mr);
3043 wdata->mr = NULL;
3044 }
3045#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003046 if (wdata->result)
3047 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3048
3049 queue_work(cifsiod_wq, &wdata->work);
3050 DeleteMidQEntry(mid);
3051 add_credits(tcon->ses->server, credits_received, 0);
3052}
3053
3054/* smb2_async_writev - send an async write, and set up mid to handle result */
3055int
Steve French4a5c80d2014-02-07 20:45:12 -06003056smb2_async_writev(struct cifs_writedata *wdata,
3057 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07003058{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003059 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003060 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003061 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003062 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003063 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003064 struct kvec iov[2];
3065 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003066 unsigned int total_len;
3067 __be32 rfc1002_marker;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003068
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003069 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003070 if (rc) {
3071 if (rc == -EAGAIN && wdata->credits) {
3072 /* credits was reset by reconnect */
3073 wdata->credits = 0;
3074 /* reduce in_flight value since we won't send the req */
3075 spin_lock(&server->req_lock);
3076 server->in_flight--;
3077 spin_unlock(&server->req_lock);
3078 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003079 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003080 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003081
Steve French5a77e752018-05-09 17:43:08 -05003082 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003083 flags |= CIFS_TRANSFORM_REQ;
3084
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003085 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003086 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003087
3088 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3089 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3090 req->WriteChannelInfoOffset = 0;
3091 req->WriteChannelInfoLength = 0;
3092 req->Channel = 0;
3093 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003094 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003095 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003096 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07003097#ifdef CONFIG_CIFS_SMB_DIRECT
3098 /*
3099 * If we want to do a server RDMA read, fill in and append
3100 * smbd_buffer_descriptor_v1 to the end of write request
3101 */
Long Libb4c0412018-04-17 12:17:08 -07003102 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003103 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003104
Long Lidb223a52017-11-22 17:38:45 -07003105 struct smbd_buffer_descriptor_v1 *v1;
3106 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3107
3108 wdata->mr = smbd_register_mr(
3109 server->smbd_conn, wdata->pages,
3110 wdata->nr_pages, wdata->tailsz,
3111 false, need_invalidate);
3112 if (!wdata->mr) {
3113 rc = -ENOBUFS;
3114 goto async_writev_out;
3115 }
3116 req->Length = 0;
3117 req->DataOffset = 0;
3118 req->RemainingBytes =
Steve French2026b062018-01-24 23:07:41 -06003119 cpu_to_le32((wdata->nr_pages-1)*PAGE_SIZE + wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003120 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3121 if (need_invalidate)
3122 req->Channel = SMB2_CHANNEL_RDMA_V1;
3123 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003124 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003125 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003126 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003127 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003128 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3129 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3130 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003131 }
3132#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003133 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003134 iov[0].iov_len = 4;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003135 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
3136 iov[0].iov_base = &rfc1002_marker;
3137 iov[1].iov_len = total_len - 1;
3138 iov[1].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003139
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003140 rqst.rq_iov = iov;
3141 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003142 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07003143 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003144 rqst.rq_npages = wdata->nr_pages;
3145 rqst.rq_pagesz = wdata->pagesz;
3146 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003147#ifdef CONFIG_CIFS_SMB_DIRECT
3148 if (wdata->mr) {
3149 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3150 rqst.rq_npages = 0;
3151 }
3152#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003153 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3154 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003155
Long Lidb223a52017-11-22 17:38:45 -07003156#ifdef CONFIG_CIFS_SMB_DIRECT
3157 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3158 if (!wdata->mr)
3159 req->Length = cpu_to_le32(wdata->bytes);
3160#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003161 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003162#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003163
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003164 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003165 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003166 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003167 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003168 spin_lock(&server->req_lock);
3169 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003170 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003171 spin_unlock(&server->req_lock);
3172 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003173 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003174 }
3175
Pavel Shilovsky33319142012-09-18 16:20:29 -07003176 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003177 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3178 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003179
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003180 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003181 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3182 tcon->tid, tcon->ses->Suid, wdata->offset,
3183 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06003184 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003185 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003186 } else
3187 trace_smb3_write_done(0 /* no xid */, req->PersistentFileId,
3188 tcon->tid, tcon->ses->Suid, wdata->offset,
3189 wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003190
Pavel Shilovsky33319142012-09-18 16:20:29 -07003191async_writev_out:
3192 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003193 return rc;
3194}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003195
3196/*
3197 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3198 * The length field from io_parms must be at least 1 and indicates a number of
3199 * elements with data to write that begins with position 1 in iov array. All
3200 * data length is specified by count.
3201 */
3202int
3203SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3204 unsigned int *nbytes, struct kvec *iov, int n_vec)
3205{
3206 int rc = 0;
3207 struct smb2_write_req *req = NULL;
3208 struct smb2_write_rsp *rsp = NULL;
3209 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003210 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003211 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003212 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003213
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003214 *nbytes = 0;
3215
3216 if (n_vec < 1)
3217 return rc;
3218
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003219 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3220 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003221 if (rc)
3222 return rc;
3223
3224 if (io_parms->tcon->ses->server == NULL)
3225 return -ECONNABORTED;
3226
Steve French5a77e752018-05-09 17:43:08 -05003227 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003228 flags |= CIFS_TRANSFORM_REQ;
3229
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003230 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003231
3232 req->PersistentFileId = io_parms->persistent_fid;
3233 req->VolatileFileId = io_parms->volatile_fid;
3234 req->WriteChannelInfoOffset = 0;
3235 req->WriteChannelInfoLength = 0;
3236 req->Channel = 0;
3237 req->Length = cpu_to_le32(io_parms->length);
3238 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003239 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003240 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003241 req->RemainingBytes = 0;
3242
3243 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003244 /* 1 for Buffer */
3245 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003246
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003247 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
3248 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003249 cifs_small_buf_release(req);
3250 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003251
3252 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003253 trace_smb3_write_err(xid, req->PersistentFileId,
3254 io_parms->tcon->tid,
3255 io_parms->tcon->ses->Suid,
3256 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003257 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003258 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05003259 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003260 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05003261 trace_smb3_write_done(xid, req->PersistentFileId,
3262 io_parms->tcon->tid,
3263 io_parms->tcon->ses->Suid,
3264 io_parms->offset, *nbytes);
3265 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003266
3267 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003268 return rc;
3269}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003270
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003271static unsigned int
3272num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3273{
3274 int len;
3275 unsigned int entrycount = 0;
3276 unsigned int next_offset = 0;
3277 FILE_DIRECTORY_INFO *entryptr;
3278
3279 if (bufstart == NULL)
3280 return 0;
3281
3282 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3283
3284 while (1) {
3285 entryptr = (FILE_DIRECTORY_INFO *)
3286 ((char *)entryptr + next_offset);
3287
3288 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003289 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003290 break;
3291 }
3292
3293 len = le32_to_cpu(entryptr->FileNameLength);
3294 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003295 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3296 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003297 break;
3298 }
3299
3300 *lastentry = (char *)entryptr;
3301 entrycount++;
3302
3303 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3304 if (!next_offset)
3305 break;
3306 }
3307
3308 return entrycount;
3309}
3310
3311/*
3312 * Readdir/FindFirst
3313 */
3314int
3315SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3316 u64 persistent_fid, u64 volatile_fid, int index,
3317 struct cifs_search_info *srch_inf)
3318{
3319 struct smb2_query_directory_req *req;
3320 struct smb2_query_directory_rsp *rsp = NULL;
3321 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003322 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003323 int rc = 0;
3324 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003325 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003326 unsigned char *bufptr;
3327 struct TCP_Server_Info *server;
3328 struct cifs_ses *ses = tcon->ses;
3329 __le16 asteriks = cpu_to_le16('*');
3330 char *end_of_smb;
3331 unsigned int output_size = CIFSMaxBufSize;
3332 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003333 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003334 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003335
3336 if (ses && (ses->server))
3337 server = ses->server;
3338 else
3339 return -EIO;
3340
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003341 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3342 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003343 if (rc)
3344 return rc;
3345
Steve French5a77e752018-05-09 17:43:08 -05003346 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003347 flags |= CIFS_TRANSFORM_REQ;
3348
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003349 switch (srch_inf->info_level) {
3350 case SMB_FIND_FILE_DIRECTORY_INFO:
3351 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3352 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3353 break;
3354 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3355 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3356 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3357 break;
3358 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003359 cifs_dbg(VFS, "info level %u isn't supported\n",
3360 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003361 rc = -EINVAL;
3362 goto qdir_exit;
3363 }
3364
3365 req->FileIndex = cpu_to_le32(index);
3366 req->PersistentFileId = persistent_fid;
3367 req->VolatileFileId = volatile_fid;
3368
3369 len = 0x2;
3370 bufptr = req->Buffer;
3371 memcpy(bufptr, &asteriks, len);
3372
3373 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003374 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003375 req->FileNameLength = cpu_to_le16(len);
3376 /*
3377 * BB could be 30 bytes or so longer if we used SMB2 specific
3378 * buffer lengths, but this is safe and close enough.
3379 */
3380 output_size = min_t(unsigned int, output_size, server->maxBuf);
3381 output_size = min_t(unsigned int, output_size, 2 << 15);
3382 req->OutputBufferLength = cpu_to_le32(output_size);
3383
3384 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003385 /* 1 for Buffer */
3386 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003387
3388 iov[1].iov_base = (char *)(req->Buffer);
3389 iov[1].iov_len = len;
3390
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003391 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003392 cifs_small_buf_release(req);
3393 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003394
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003395 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003396 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003397 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003398 srch_inf->endOfSearch = true;
3399 rc = 0;
3400 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003401 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3402 goto qdir_exit;
3403 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003404
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003405 rc = validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003406 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003407 info_buf_size);
3408 if (rc)
3409 goto qdir_exit;
3410
3411 srch_inf->unicode = true;
3412
3413 if (srch_inf->ntwrk_buf_start) {
3414 if (srch_inf->smallBuf)
3415 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3416 else
3417 cifs_buf_release(srch_inf->ntwrk_buf_start);
3418 }
3419 srch_inf->ntwrk_buf_start = (char *)rsp;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003420 srch_inf->srch_entries_start = srch_inf->last_entry =
3421 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
3422 end_of_smb = rsp_iov.iov_len + (char *)rsp;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003423 srch_inf->entries_in_buffer =
3424 num_entries(srch_inf->srch_entries_start, end_of_smb,
3425 &srch_inf->last_entry, info_buf_size);
3426 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003427 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3428 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3429 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003430 if (resp_buftype == CIFS_LARGE_BUFFER)
3431 srch_inf->smallBuf = false;
3432 else if (resp_buftype == CIFS_SMALL_BUFFER)
3433 srch_inf->smallBuf = true;
3434 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003435 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003436
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003437 return rc;
3438
3439qdir_exit:
3440 free_rsp_buf(resp_buftype, rsp);
3441 return rc;
3442}
3443
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003444static int
3445send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003446 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3447 u8 info_type, u32 additional_info, unsigned int num,
3448 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003449{
3450 struct smb2_set_info_req *req;
3451 struct smb2_set_info_rsp *rsp = NULL;
3452 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003453 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003454 int rc = 0;
3455 int resp_buftype;
3456 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003457 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003458 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003459 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003460
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003461 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003462 return -EIO;
3463
3464 if (!num)
3465 return -EINVAL;
3466
3467 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3468 if (!iov)
3469 return -ENOMEM;
3470
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003471 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003472 if (rc) {
3473 kfree(iov);
3474 return rc;
3475 }
3476
Steve French5a77e752018-05-09 17:43:08 -05003477 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003478 flags |= CIFS_TRANSFORM_REQ;
3479
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003480 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003481
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003482 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003483 req->FileInfoClass = info_class;
3484 req->PersistentFileId = persistent_fid;
3485 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003486 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003487
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003488 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003489 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003490 req->BufferLength = cpu_to_le32(*size);
3491
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003492 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003493 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003494
3495 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003496 /* 1 for Buffer */
3497 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003498
3499 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003500 le32_add_cpu(&req->BufferLength, size[i]);
3501 iov[i].iov_base = (char *)data[i];
3502 iov[i].iov_len = size[i];
3503 }
3504
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003505 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3506 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003507 cifs_small_buf_release(req);
3508 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003509
Steve Frencheccb4422018-05-17 21:16:55 -05003510 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003511 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003512 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
3513 ses->Suid, info_class, (__u32)info_type, rc);
3514 }
Steve French7d3fb242013-11-18 09:56:28 -06003515
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003516 free_rsp_buf(resp_buftype, rsp);
3517 kfree(iov);
3518 return rc;
3519}
3520
3521int
3522SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3523 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3524{
3525 struct smb2_file_rename_info info;
3526 void **data;
3527 unsigned int size[2];
3528 int rc;
3529 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3530
3531 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3532 if (!data)
3533 return -ENOMEM;
3534
3535 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3536 /* 0 = fail if target already exists */
3537 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3538 info.FileNameLength = cpu_to_le32(len);
3539
3540 data[0] = &info;
3541 size[0] = sizeof(struct smb2_file_rename_info);
3542
3543 data[1] = target_file;
3544 size[1] = len + 2 /* null */;
3545
3546 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003547 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3548 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003549 kfree(data);
3550 return rc;
3551}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003552
3553int
Steve French897fba12016-05-12 21:20:36 -05003554SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3555 u64 persistent_fid, u64 volatile_fid)
3556{
3557 __u8 delete_pending = 1;
3558 void *data;
3559 unsigned int size;
3560
3561 data = &delete_pending;
3562 size = 1; /* sizeof __u8 */
3563
3564 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003565 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3566 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003567}
3568
3569int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003570SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3571 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3572{
3573 struct smb2_file_link_info info;
3574 void **data;
3575 unsigned int size[2];
3576 int rc;
3577 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3578
3579 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3580 if (!data)
3581 return -ENOMEM;
3582
3583 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3584 /* 0 = fail if link already exists */
3585 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3586 info.FileNameLength = cpu_to_le32(len);
3587
3588 data[0] = &info;
3589 size[0] = sizeof(struct smb2_file_link_info);
3590
3591 data[1] = target_file;
3592 size[1] = len + 2 /* null */;
3593
3594 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003595 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3596 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003597 kfree(data);
3598 return rc;
3599}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003600
3601int
3602SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003603 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003604{
3605 struct smb2_file_eof_info info;
3606 void *data;
3607 unsigned int size;
3608
3609 info.EndOfFile = *eof;
3610
3611 data = &info;
3612 size = sizeof(struct smb2_file_eof_info);
3613
Steve Frenchf29ebb42014-07-19 21:44:58 -05003614 if (is_falloc)
3615 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003616 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3617 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003618 else
3619 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003620 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3621 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003622}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003623
3624int
3625SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3626 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3627{
3628 unsigned int size;
3629 size = sizeof(FILE_BASIC_INFO);
3630 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003631 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3632 0, 1, (void **)&buf, &size);
3633}
3634
3635int
3636SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3637 u64 persistent_fid, u64 volatile_fid,
3638 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3639{
3640 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3641 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3642 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003643}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003644
3645int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003646SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3647 u64 persistent_fid, u64 volatile_fid,
3648 struct smb2_file_full_ea_info *buf, int len)
3649{
3650 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3651 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3652 0, 1, (void **)&buf, &len);
3653}
3654
3655int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003656SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3657 const u64 persistent_fid, const u64 volatile_fid,
3658 __u8 oplock_level)
3659{
3660 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10003661 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003662 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003663 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003664 unsigned int total_len;
3665 struct kvec iov[1];
3666 struct kvec rsp_iov;
3667 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003668
Joe Perchesf96637b2013-05-04 22:12:25 -05003669 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003670 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3671 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003672 if (rc)
3673 return rc;
3674
Steve French5a77e752018-05-09 17:43:08 -05003675 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003676 flags |= CIFS_TRANSFORM_REQ;
3677
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003678 req->VolatileFid = volatile_fid;
3679 req->PersistentFid = persistent_fid;
3680 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003681 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003682
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003683 flags |= CIFS_NO_RESP;
3684
3685 iov[0].iov_base = (char *)req;
3686 iov[0].iov_len = total_len;
3687
3688 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003689 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003690
3691 if (rc) {
3692 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003693 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003694 }
3695
3696 return rc;
3697}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003698
3699static void
3700copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3701 struct kstatfs *kst)
3702{
3703 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3704 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3705 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303706 kst->f_bfree = kst->f_bavail =
3707 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003708 return;
3709}
3710
3711static int
3712build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3713 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3714{
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003715 struct TCP_Server_Info *server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003716 int rc;
3717 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003718 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003719
Joe Perchesf96637b2013-05-04 22:12:25 -05003720 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003721
3722 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3723 return -EIO;
3724
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003725 server = tcon->ses->server;
3726
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003727 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3728 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003729 if (rc)
3730 return rc;
3731
3732 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3733 req->FileInfoClass = level;
3734 req->PersistentFileId = persistent_fid;
3735 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003736 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003737 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003738 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003739 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003740 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003741
3742 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003743 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003744 return 0;
3745}
3746
3747int
3748SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3749 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3750{
3751 struct smb2_query_info_rsp *rsp = NULL;
3752 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003753 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003754 int rc = 0;
3755 int resp_buftype;
3756 struct cifs_ses *ses = tcon->ses;
3757 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003758 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003759
3760 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3761 sizeof(struct smb2_fs_full_size_info),
3762 persistent_fid, volatile_fid);
3763 if (rc)
3764 return rc;
3765
Steve French5a77e752018-05-09 17:43:08 -05003766 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003767 flags |= CIFS_TRANSFORM_REQ;
3768
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003769 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003770 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003771 if (rc) {
3772 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003773 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003774 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003775 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003776
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003777 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003778 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003779 rc = validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003780 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003781 sizeof(struct smb2_fs_full_size_info));
3782 if (!rc)
3783 copy_fs_info_to_kstatfs(info, fsdata);
3784
Steve French34f62642013-10-09 02:07:00 -05003785qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003786 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003787 return rc;
3788}
3789
3790int
3791SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003792 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003793{
3794 struct smb2_query_info_rsp *rsp = NULL;
3795 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003796 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003797 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003798 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003799 struct cifs_ses *ses = tcon->ses;
3800 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003801 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003802
Steven French21671142013-10-09 13:36:35 -05003803 if (level == FS_DEVICE_INFORMATION) {
3804 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3805 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3806 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3807 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3808 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003809 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3810 max_len = sizeof(struct smb3_fs_ss_info);
3811 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003812 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003813 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003814 return -EINVAL;
3815 }
3816
3817 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003818 persistent_fid, volatile_fid);
3819 if (rc)
3820 return rc;
3821
Steve French5a77e752018-05-09 17:43:08 -05003822 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003823 flags |= CIFS_TRANSFORM_REQ;
3824
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003825 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003826 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003827 if (rc) {
3828 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3829 goto qfsattr_exit;
3830 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003831 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003832
3833 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3834 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003835 rc = validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05003836 if (rc)
3837 goto qfsattr_exit;
3838
3839 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003840 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003841 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003842 rsp_len, max_len));
3843 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003844 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003845 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003846 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3847 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003848 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003849 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3850 tcon->perf_sector_size =
3851 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3852 }
Steve French34f62642013-10-09 02:07:00 -05003853
3854qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003855 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003856 return rc;
3857}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003858
3859int
3860smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3861 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3862 const __u32 num_lock, struct smb2_lock_element *buf)
3863{
3864 int rc = 0;
3865 struct smb2_lock_req *req = NULL;
3866 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003867 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003868 int resp_buf_type;
3869 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003870 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003871 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003872
Joe Perchesf96637b2013-05-04 22:12:25 -05003873 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003874
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003875 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003876 if (rc)
3877 return rc;
3878
Steve French5a77e752018-05-09 17:43:08 -05003879 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003880 flags |= CIFS_TRANSFORM_REQ;
3881
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003882 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003883 req->LockCount = cpu_to_le16(num_lock);
3884
3885 req->PersistentFileId = persist_fid;
3886 req->VolatileFileId = volatile_fid;
3887
3888 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003889
3890 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003891 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003892 iov[1].iov_base = (char *)buf;
3893 iov[1].iov_len = count;
3894
3895 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003896 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3897 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003898 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003899 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003900 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003901 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003902 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
3903 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003904 }
3905
3906 return rc;
3907}
3908
3909int
3910SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3911 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3912 const __u64 length, const __u64 offset, const __u32 lock_flags,
3913 const bool wait)
3914{
3915 struct smb2_lock_element lock;
3916
3917 lock.Offset = cpu_to_le64(offset);
3918 lock.Length = cpu_to_le64(length);
3919 lock.Flags = cpu_to_le32(lock_flags);
3920 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3921 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3922
3923 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3924}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003925
3926int
3927SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3928 __u8 *lease_key, const __le32 lease_state)
3929{
3930 int rc;
3931 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003932 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003933 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003934 unsigned int total_len;
3935 struct kvec iov[1];
3936 struct kvec rsp_iov;
3937 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003938
Joe Perchesf96637b2013-05-04 22:12:25 -05003939 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003940 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3941 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003942 if (rc)
3943 return rc;
3944
Steve French5a77e752018-05-09 17:43:08 -05003945 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003946 flags |= CIFS_TRANSFORM_REQ;
3947
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003948 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003949 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003950 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003951
3952 memcpy(req->LeaseKey, lease_key, 16);
3953 req->LeaseState = lease_state;
3954
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003955 flags |= CIFS_NO_RESP;
3956
3957 iov[0].iov_base = (char *)req;
3958 iov[0].iov_len = total_len;
3959
3960 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003961 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003962
3963 if (rc) {
3964 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003965 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003966 }
3967
3968 return rc;
3969}