blob: c92b4e6689bd0cee8f51e9c3f606bb7a2f1abce9 [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020036#include <linux/uuid.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070037#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040038#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070048#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070049#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050050#include "cifs_spnego.h"
Long Lidb223a52017-11-22 17:38:45 -070051#include "smbdirect.h"
Steve Frencheccb4422018-05-17 21:16:55 -050052#include "trace.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040053
54/*
55 * The following table defines the expected "StructureSize" of SMB2 requests
56 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
57 *
58 * Note that commands are defined in smb2pdu.h in le16 but the array below is
59 * indexed by command in host byte order.
60 */
61static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
62 /* SMB2_NEGOTIATE */ 36,
63 /* SMB2_SESSION_SETUP */ 25,
64 /* SMB2_LOGOFF */ 4,
65 /* SMB2_TREE_CONNECT */ 9,
66 /* SMB2_TREE_DISCONNECT */ 4,
67 /* SMB2_CREATE */ 57,
68 /* SMB2_CLOSE */ 24,
69 /* SMB2_FLUSH */ 24,
70 /* SMB2_READ */ 49,
71 /* SMB2_WRITE */ 49,
72 /* SMB2_LOCK */ 48,
73 /* SMB2_IOCTL */ 57,
74 /* SMB2_CANCEL */ 4,
75 /* SMB2_ECHO */ 4,
76 /* SMB2_QUERY_DIRECTORY */ 33,
77 /* SMB2_CHANGE_NOTIFY */ 32,
78 /* SMB2_QUERY_INFO */ 41,
79 /* SMB2_SET_INFO */ 33,
80 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
81};
82
Steve French5a77e752018-05-09 17:43:08 -050083static int smb3_encryption_required(const struct cifs_tcon *tcon)
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070084{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080085 if (!tcon)
86 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070087 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
88 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
89 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080090 if (tcon->seal &&
91 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
92 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070093 return 0;
94}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040095
96static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070097smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040098 const struct cifs_tcon *tcon)
99{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700100 shdr->ProtocolId = SMB2_PROTO_NUMBER;
101 shdr->StructureSize = cpu_to_le16(64);
102 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100103 if (tcon && tcon->ses && tcon->ses->server) {
104 struct TCP_Server_Info *server = tcon->ses->server;
105
106 spin_lock(&server->req_lock);
107 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500108 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700109 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100110 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700111 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500112 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100113 server->credits, 2));
114 spin_unlock(&server->req_lock);
115 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700116 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100117 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700118 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400119
120 if (!tcon)
121 goto out;
122
Steve French2b80d042013-06-23 18:43:37 -0500123 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
124 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500125 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500126 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700127 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500128 /* else CreditCharge MBZ */
129
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700130 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400131 /* Uid is not converted */
132 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700133 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500134
135 /*
136 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
137 * to pass the path on the Open SMB prefixed by \\server\share.
138 * Not sure when we would need to do the augmented path (if ever) and
139 * setting this flag breaks the SMB2 open operation since it is
140 * illegal to send an empty path name (without \\server\share prefix)
141 * when the DFS flag is set in the SMB open header. We could
142 * consider setting the flag on all operations other than open
143 * but it is safer to net set it for now.
144 */
145/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700146 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500147
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700148 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
Steve French5a77e752018-05-09 17:43:08 -0500149 !smb3_encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700150 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400151out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400152 return;
153}
154
155static int
156smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
157{
158 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400159 struct nls_table *nls_codepage;
160 struct cifs_ses *ses;
161 struct TCP_Server_Info *server;
162
163 /*
164 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
165 * check for tcp and smb session status done differently
166 * for those three - in the calling routine.
167 */
168 if (tcon == NULL)
169 return rc;
170
171 if (smb2_command == SMB2_TREE_CONNECT)
172 return rc;
173
174 if (tcon->tidStatus == CifsExiting) {
175 /*
176 * only tree disconnect, open, and write,
177 * (and ulogoff which does not have tcon)
178 * are allowed as we start force umount.
179 */
180 if ((smb2_command != SMB2_WRITE) &&
181 (smb2_command != SMB2_CREATE) &&
182 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500183 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
184 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400185 return -ENODEV;
186 }
187 }
188 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
189 (!tcon->ses->server))
190 return -EIO;
191
192 ses = tcon->ses;
193 server = ses->server;
194
195 /*
196 * Give demultiplex thread up to 10 seconds to reconnect, should be
197 * greater than cifs socket timeout which is 7 seconds
198 */
199 while (server->tcpStatus == CifsNeedReconnect) {
200 /*
201 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
202 * here since they are implicitly done when session drops.
203 */
204 switch (smb2_command) {
205 /*
206 * BB Should we keep oplock break and add flush to exceptions?
207 */
208 case SMB2_TREE_DISCONNECT:
209 case SMB2_CANCEL:
210 case SMB2_CLOSE:
211 case SMB2_OPLOCK_BREAK:
212 return -EAGAIN;
213 }
214
215 wait_event_interruptible_timeout(server->response_q,
216 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
217
218 /* are we still trying to reconnect? */
219 if (server->tcpStatus != CifsNeedReconnect)
220 break;
221
222 /*
223 * on "soft" mounts we wait once. Hard mounts keep
224 * retrying until process is killed or server comes
225 * back on-line
226 */
227 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500228 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400229 return -EHOSTDOWN;
230 }
231 }
232
233 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
234 return rc;
235
236 nls_codepage = load_nls_default();
237
238 /*
239 * need to prevent multiple threads trying to simultaneously reconnect
240 * the same SMB session
241 */
242 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200243
244 /*
245 * Recheck after acquire mutex. If another thread is negotiating
246 * and the server never sends an answer the socket will be closed
247 * and tcpStatus set to reconnect.
248 */
249 if (server->tcpStatus == CifsNeedReconnect) {
250 rc = -EHOSTDOWN;
251 mutex_unlock(&tcon->ses->session_mutex);
252 goto out;
253 }
254
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400255 rc = cifs_negotiate_protocol(0, tcon->ses);
256 if (!rc && tcon->ses->need_reconnect)
257 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
258
259 if (rc || !tcon->need_reconnect) {
260 mutex_unlock(&tcon->ses->session_mutex);
261 goto out;
262 }
263
264 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800265 if (tcon->use_persistent)
266 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500267
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400268 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
269 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500270
Joe Perchesf96637b2013-05-04 22:12:25 -0500271 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500272 if (rc) {
273 /* If sess reconnected but tcon didn't, something strange ... */
274 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400275 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500276 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800277
278 if (smb2_command != SMB2_INTERNAL_CMD)
279 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
280
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400281 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400282out:
283 /*
284 * Check if handle based operation so we know whether we can continue
285 * or not without returning to caller to reset file handle.
286 */
287 /*
288 * BB Is flush done by server on drop of tcp session? Should we special
289 * case it and skip above?
290 */
291 switch (smb2_command) {
292 case SMB2_FLUSH:
293 case SMB2_READ:
294 case SMB2_WRITE:
295 case SMB2_LOCK:
296 case SMB2_IOCTL:
297 case SMB2_QUERY_DIRECTORY:
298 case SMB2_CHANGE_NOTIFY:
299 case SMB2_QUERY_INFO:
300 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800301 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400302 }
303 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400304 return rc;
305}
306
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700307static void
308fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
309 unsigned int *total_len)
310{
311 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
312 /* lookup word count ie StructureSize from table */
313 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
314
315 /*
316 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
317 * largest operations (Create)
318 */
319 memset(buf, 0, 256);
320
321 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
322 spdu->StructureSize2 = cpu_to_le16(parmsize);
323
324 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
325}
326
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100327/*
328 * Allocate and return pointer to an SMB request hdr, and set basic
329 * SMB information in the SMB header. If the return code is zero, this
330 * function must have filled in request_buf pointer.
331 */
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800332static int
333smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
334 void **request_buf, unsigned int *total_len)
335{
336 int rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800337
338 rc = smb2_reconnect(smb2_command, tcon);
339 if (rc)
340 return rc;
341
342 /* BB eventually switch this to SMB2 specific small buf size */
343 *request_buf = cifs_small_buf_get();
344 if (*request_buf == NULL) {
345 /* BB should we add a retry in here if not a writepage? */
346 return -ENOMEM;
347 }
348
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100349 fill_small_buf(smb2_command, tcon,
350 (struct smb2_sync_hdr *)(*request_buf),
351 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400352
353 if (tcon != NULL) {
354#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400355 uint16_t com_code = le16_to_cpu(smb2_command);
356 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400357#endif
358 cifs_stats_inc(&tcon->num_smbs_sent);
359 }
360
361 return rc;
362}
363
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500364#ifdef CONFIG_CIFS_SMB311
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100365/* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
366#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500367
368
369#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
370#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
Steve Frenchfcef0db2018-05-19 20:45:27 -0500371#define SMB2_POSIX_EXTENSIONS_AVAILABLE cpu_to_le16(0x100)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500372
373static void
374build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
375{
376 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
377 pneg_ctxt->DataLength = cpu_to_le16(38);
378 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
379 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
380 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
381 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
382}
383
384static void
385build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
386{
387 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French23657ad2018-04-22 15:14:58 -0500388 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
389 pneg_ctxt->CipherCount = cpu_to_le16(1);
390/* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
391 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500392}
393
394static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500395build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
396{
397 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
398 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
399}
400
401static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100402assemble_neg_contexts(struct smb2_negotiate_req *req,
403 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500404{
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100405 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500406 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500407
Steve Frenchfcef0db2018-05-19 20:45:27 -0500408 *total_len += 2; /* Add 2 due to round to 8 byte boundary for 1st ctxt */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500409 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500410 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
411 *total_len += ctxt_len;
412 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100413
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500414 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500415 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
416 *total_len += ctxt_len;
417 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100418
Steve Frenchfcef0db2018-05-19 20:45:27 -0500419 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
420 *total_len += sizeof(struct smb2_posix_neg_context);
421
422 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
423 req->NegotiateContextCount = cpu_to_le16(3);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500424}
Steve French5100d8a2018-04-09 10:47:14 -0500425
426static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
427{
428 unsigned int len = le16_to_cpu(ctxt->DataLength);
429
430 /* If invalid preauth context warn but use what we requested, SHA-512 */
431 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
432 printk_once(KERN_WARNING "server sent bad preauth context\n");
433 return;
434 }
435 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
436 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
437 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
438 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
439}
440
441static int decode_encrypt_ctx(struct TCP_Server_Info *server,
442 struct smb2_encryption_neg_context *ctxt)
443{
444 unsigned int len = le16_to_cpu(ctxt->DataLength);
445
446 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
447 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
448 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
449 return -EINVAL;
450 }
451
452 if (le16_to_cpu(ctxt->CipherCount) != 1) {
453 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
454 return -EINVAL;
455 }
456 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
457 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
458 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
459 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
460 return -EINVAL;
461 }
462 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500463 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500464 return 0;
465}
466
467static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
468 struct TCP_Server_Info *server)
469{
470 struct smb2_neg_context *pctx;
471 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
472 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
473 unsigned int len_of_smb = be32_to_cpu(rsp->hdr.smb2_buf_length);
474 unsigned int len_of_ctxts, i;
475 int rc = 0;
476
477 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
478 if (len_of_smb <= offset) {
479 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
480 return -EINVAL;
481 }
482
483 len_of_ctxts = len_of_smb - offset;
484
485 for (i = 0; i < ctxt_cnt; i++) {
486 int clen;
487 /* check that offset is not beyond end of SMB */
488 if (len_of_ctxts == 0)
489 break;
490
491 if (len_of_ctxts < sizeof(struct smb2_neg_context))
492 break;
493
Steve French0d4b46b2018-04-12 20:32:13 -0500494 pctx = (struct smb2_neg_context *)(offset +
495 server->vals->header_preamble_size + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500496 clen = le16_to_cpu(pctx->DataLength);
497 if (clen > len_of_ctxts)
498 break;
499
500 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
501 decode_preauth_context(
502 (struct smb2_preauth_neg_context *)pctx);
503 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
504 rc = decode_encrypt_ctx(server,
505 (struct smb2_encryption_neg_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500506 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
507 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500508 else
509 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
510 le16_to_cpu(pctx->ContextType));
511
512 if (rc)
513 break;
514 /* offsets must be 8 byte aligned */
515 clen = (clen + 7) & ~0x7;
516 offset += clen + sizeof(struct smb2_neg_context);
517 len_of_ctxts -= clen;
518 }
519 return rc;
520}
521
Steve Frenchce558b02018-05-31 19:16:54 -0500522static struct create_posix *
523create_posix_buf(umode_t mode)
524{
525 struct create_posix *buf;
526
527 buf = kzalloc(sizeof(struct create_posix),
528 GFP_KERNEL);
529 if (!buf)
530 return NULL;
531
532 buf->ccontext.DataOffset =
533 cpu_to_le16(offsetof(struct create_posix, Mode));
534 buf->ccontext.DataLength = cpu_to_le32(4);
535 buf->ccontext.NameOffset =
536 cpu_to_le16(offsetof(struct create_posix, Name));
537 buf->ccontext.NameLength = cpu_to_le16(16);
538
539 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
540 buf->Name[0] = 0x93;
541 buf->Name[1] = 0xAD;
542 buf->Name[2] = 0x25;
543 buf->Name[3] = 0x50;
544 buf->Name[4] = 0x9C;
545 buf->Name[5] = 0xB4;
546 buf->Name[6] = 0x11;
547 buf->Name[7] = 0xE7;
548 buf->Name[8] = 0xB4;
549 buf->Name[9] = 0x23;
550 buf->Name[10] = 0x83;
551 buf->Name[11] = 0xDE;
552 buf->Name[12] = 0x96;
553 buf->Name[13] = 0x8B;
554 buf->Name[14] = 0xCD;
555 buf->Name[15] = 0x7C;
556 buf->Mode = cpu_to_le32(mode);
557 cifs_dbg(FYI, "mode on posix create 0%o", mode);
558 return buf;
559}
560
561static int
562add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
563{
564 struct smb2_create_req *req = iov[0].iov_base;
565 unsigned int num = *num_iovec;
566
567 iov[num].iov_base = create_posix_buf(mode);
568 if (iov[num].iov_base == NULL)
569 return -ENOMEM;
570 iov[num].iov_len = sizeof(struct create_posix);
571 if (!req->CreateContextsOffset)
572 req->CreateContextsOffset = cpu_to_le32(
573 sizeof(struct smb2_create_req) +
574 iov[num - 1].iov_len);
575 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
576 *num_iovec = num + 1;
577 return 0;
578}
579
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500580#else
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100581static void assemble_neg_contexts(struct smb2_negotiate_req *req,
582 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500583{
584 return;
585}
586#endif /* SMB311 */
587
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400588/*
589 *
590 * SMB2 Worker functions follow:
591 *
592 * The general structure of the worker functions is:
593 * 1) Call smb2_init (assembles SMB2 header)
594 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
595 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
596 * 4) Decode SMB2 command specific fields in the fixed length area
597 * 5) Decode variable length data area (if any for this SMB2 command type)
598 * 6) Call free smb buffer
599 * 7) return
600 *
601 */
602
603int
604SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
605{
606 struct smb2_negotiate_req *req;
607 struct smb2_negotiate_rsp *rsp;
608 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700609 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400610 int rc = 0;
611 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400612 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400613 int blob_offset, blob_length;
614 char *security_blob;
615 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100616 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400617
Joe Perchesf96637b2013-05-04 22:12:25 -0500618 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400619
Jeff Layton3534b852013-05-24 07:41:01 -0400620 if (!server) {
621 WARN(1, "%s: server is NULL!\n", __func__);
622 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400623 }
624
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100625 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400626 if (rc)
627 return rc;
628
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100629 req->sync_hdr.SessionId = 0;
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100630#ifdef CONFIG_CIFS_SMB311
631 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
632 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
633#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400634
Steve French9764c022017-09-17 10:41:35 -0500635 if (strcmp(ses->server->vals->version_string,
636 SMB3ANY_VERSION_STRING) == 0) {
637 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
638 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
639 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100640 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500641 } else if (strcmp(ses->server->vals->version_string,
642 SMBDEFAULT_VERSION_STRING) == 0) {
643 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
644 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
645 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
646 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100647 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500648 } else {
649 /* otherwise send specific dialect */
650 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
651 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100652 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500653 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400654
655 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400656 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500657 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400658 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500659 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400660 else
661 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400662
Steve Frenche4aa25e2012-10-01 12:26:22 -0500663 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400664
Steve French3c5f9be12014-05-13 13:37:45 -0700665 /* ClientGUID must be zero for SMB2.02 dialect */
666 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
667 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500668 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700669 memcpy(req->ClientGUID, server->client_guid,
670 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500671 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100672 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500673 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400674 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100675 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400676
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100677 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700678 cifs_small_buf_release(req);
679 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400680 /*
681 * No tcon so can't do
682 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
683 */
Steve French7e682f72017-08-31 21:34:24 -0500684 if (rc == -EOPNOTSUPP) {
685 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500686 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500687 " older servers\n");
688 goto neg_exit;
689 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400690 goto neg_exit;
691
Steve French9764c022017-09-17 10:41:35 -0500692 if (strcmp(ses->server->vals->version_string,
693 SMB3ANY_VERSION_STRING) == 0) {
694 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
695 cifs_dbg(VFS,
696 "SMB2 dialect returned but not requested\n");
697 return -EIO;
698 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
699 cifs_dbg(VFS,
700 "SMB2.1 dialect returned but not requested\n");
701 return -EIO;
702 }
703 } else if (strcmp(ses->server->vals->version_string,
704 SMBDEFAULT_VERSION_STRING) == 0) {
705 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
706 cifs_dbg(VFS,
707 "SMB2 dialect returned but not requested\n");
708 return -EIO;
709 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
710 /* ops set to 3.0 by default for default so update */
711 ses->server->ops = &smb21_operations;
712 }
Steve French590d08d2017-09-19 11:43:47 -0500713 } else if (le16_to_cpu(rsp->DialectRevision) !=
714 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500715 /* if requested single dialect ensure returned dialect matched */
716 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500717 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500718 return -EIO;
719 }
720
Joe Perchesf96637b2013-05-04 22:12:25 -0500721 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400722
Steve Frenche4aa25e2012-10-01 12:26:22 -0500723 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500724 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500725 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500726 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500727 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500728 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500729 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
730 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600731#ifdef CONFIG_CIFS_SMB311
732 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
733 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
734#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400735 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500736 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500737 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400738 rc = -EIO;
739 goto neg_exit;
740 }
741 server->dialect = le16_to_cpu(rsp->DialectRevision);
742
Steve French9764c022017-09-17 10:41:35 -0500743 /* BB: add check that dialect was valid given dialect(s) we asked for */
744
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100745#ifdef CONFIG_CIFS_SMB311
746 /*
747 * Keep a copy of the hash after negprot. This hash will be
748 * the starting hash value for all sessions made from this
749 * server.
750 */
751 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
752 SMB2_PREAUTH_HASH_SIZE);
753#endif
Jeff Laytone598d1d82013-05-26 07:00:59 -0400754 /* SMB2 only has an extended negflavor */
755 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400756 /* set it to the maximum buffer size value we can send with 1 credit */
757 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
758 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400759 server->max_read = le32_to_cpu(rsp->MaxReadSize);
760 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400761 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500762 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
763 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
764 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400765 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400766 /* Internal types */
767 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400768
769 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
770 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500771 /*
772 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
773 * for us will be
774 * ses->sectype = RawNTLMSSP;
775 * but for time being this is our only auth choice so doesn't matter.
776 * We just found a server which sets blob length to zero expecting raw.
777 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700778 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500779 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700780 server->sec_ntlmssp = true;
781 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700782
Jeff Layton38d77c52013-05-26 07:01:00 -0400783 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400784 if (rc)
785 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500786 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500787 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500788 if (rc == 1)
789 rc = 0;
790 else if (rc == 0)
791 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400792 }
Steve French5100d8a2018-04-09 10:47:14 -0500793
794#ifdef CONFIG_CIFS_SMB311
795 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
796 if (rsp->NegotiateContextCount)
797 rc = smb311_decode_neg_context(rsp, server);
798 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;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001133 ses->Suid = rsp->hdr.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 &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001209 rsp->hdr.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 Sahlberg9fdd2e02018-04-09 18:06:27 +10001215 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - ses->server->vals->header_preamble_size !=
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
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001230 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001231 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001232
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
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001288 ses->Suid = rsp->hdr.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;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001355
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001356#ifdef CONFIG_CIFS_SMB311
1357 /*
1358 * Initialize the session hash with the server one.
1359 */
1360 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1361 SMB2_PREAUTH_HASH_SIZE);
1362#endif
1363
Sachin Prabhu166cea42016-10-07 19:11:22 +01001364 while (sess_data->func)
1365 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001366
Steve Frenchc721c382017-09-19 18:40:03 -05001367 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1368 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001369 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001370out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001371 kfree(sess_data);
1372 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001373}
1374
1375int
1376SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1377{
1378 struct smb2_logoff_req *req; /* response is also trivial struct */
1379 int rc = 0;
1380 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001381 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001382 unsigned int total_len;
1383 struct kvec iov[1];
1384 struct kvec rsp_iov;
1385 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001386
Joe Perchesf96637b2013-05-04 22:12:25 -05001387 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001388
1389 if (ses && (ses->server))
1390 server = ses->server;
1391 else
1392 return -EIO;
1393
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001394 /* no need to send SMB logoff if uid already closed due to reconnect */
1395 if (ses->need_reconnect)
1396 goto smb2_session_already_dead;
1397
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001398 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001399 if (rc)
1400 return rc;
1401
1402 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001403 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001404
1405 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1406 flags |= CIFS_TRANSFORM_REQ;
1407 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001408 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001409
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001410 flags |= CIFS_NO_RESP;
1411
1412 iov[0].iov_base = (char *)req;
1413 iov[0].iov_len = total_len;
1414
1415 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001416 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001417 /*
1418 * No tcon so can't do
1419 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1420 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001421
1422smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001423 return rc;
1424}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001425
1426static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1427{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001428 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001429}
1430
1431#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1432
Steve Frenchde9f68d2013-11-15 11:26:24 -06001433/* These are similar values to what Windows uses */
1434static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1435{
1436 tcon->max_chunks = 256;
1437 tcon->max_bytes_chunk = 1048576;
1438 tcon->max_bytes_copy = 16777216;
1439}
1440
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001441int
1442SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1443 struct cifs_tcon *tcon, const struct nls_table *cp)
1444{
1445 struct smb2_tree_connect_req *req;
1446 struct smb2_tree_connect_rsp *rsp = NULL;
1447 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001448 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001449 int rc = 0;
1450 int resp_buftype;
1451 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001452 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001453 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001454 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001455
Joe Perchesf96637b2013-05-04 22:12:25 -05001456 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001457
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001458 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001459 return -EIO;
1460
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001461 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1462 if (unc_path == NULL)
1463 return -ENOMEM;
1464
1465 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1466 unc_path_len *= 2;
1467 if (unc_path_len < 2) {
1468 kfree(unc_path);
1469 return -EINVAL;
1470 }
1471
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001472 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001473 tcon->tid = 0;
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001474
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001475 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1476 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001477 if (rc) {
1478 kfree(unc_path);
1479 return rc;
1480 }
1481
Steve French5a77e752018-05-09 17:43:08 -05001482 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001483 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001484
1485 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001486 /* 1 for pad */
1487 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001488
1489 /* Testing shows that buffer offset must be at location of Buffer[0] */
1490 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001491 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001492 req->PathLength = cpu_to_le16(unc_path_len - 2);
1493 iov[1].iov_base = unc_path;
1494 iov[1].iov_len = unc_path_len;
1495
Steve French6188f282018-03-13 02:29:36 -05001496 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1497 if ((ses->server->dialect == SMB311_PROT_ID) &&
Steve French5a77e752018-05-09 17:43:08 -05001498 !smb3_encryption_required(tcon))
Steve French6188f282018-03-13 02:29:36 -05001499 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1500
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001501 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001502 cifs_small_buf_release(req);
1503 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001504
1505 if (rc != 0) {
1506 if (tcon) {
1507 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1508 tcon->need_reconnect = true;
1509 }
1510 goto tcon_error_exit;
1511 }
1512
Christophe JAILLETcd123002017-05-12 17:59:32 +02001513 switch (rsp->ShareType) {
1514 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001515 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001516 break;
1517 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001518 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001519 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001520 break;
1521 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001522 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001523 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001524 break;
1525 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001526 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001527 rc = -EOPNOTSUPP;
1528 goto tcon_error_exit;
1529 }
1530
1531 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001532 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001533 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1534 tcon->tidStatus = CifsGood;
1535 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001536 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001537 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001538
1539 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1540 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001541 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001542
1543 if (tcon->seal &&
1544 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1545 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1546
Steve Frenchde9f68d2013-11-15 11:26:24 -06001547 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001548 if (tcon->ses->server->ops->validate_negotiate)
1549 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001550tcon_exit:
1551 free_rsp_buf(resp_buftype, rsp);
1552 kfree(unc_path);
1553 return rc;
1554
1555tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001556 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001557 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001558 }
1559 goto tcon_exit;
1560}
1561
1562int
1563SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1564{
1565 struct smb2_tree_disconnect_req *req; /* response is trivial */
1566 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001567 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001568 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001569 unsigned int total_len;
1570 struct kvec iov[1];
1571 struct kvec rsp_iov;
1572 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001573
Joe Perchesf96637b2013-05-04 22:12:25 -05001574 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001575
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001576 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001577 return -EIO;
1578
1579 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1580 return 0;
1581
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001582 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1583 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001584 if (rc)
1585 return rc;
1586
Steve French5a77e752018-05-09 17:43:08 -05001587 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001588 flags |= CIFS_TRANSFORM_REQ;
1589
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001590 flags |= CIFS_NO_RESP;
1591
1592 iov[0].iov_base = (char *)req;
1593 iov[0].iov_len = total_len;
1594
1595 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001596 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001597 if (rc)
1598 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1599
1600 return rc;
1601}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001602
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001603
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001604static struct create_durable *
1605create_durable_buf(void)
1606{
1607 struct create_durable *buf;
1608
1609 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1610 if (!buf)
1611 return NULL;
1612
1613 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001614 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001615 buf->ccontext.DataLength = cpu_to_le32(16);
1616 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1617 (struct create_durable, Name));
1618 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001619 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001620 buf->Name[0] = 'D';
1621 buf->Name[1] = 'H';
1622 buf->Name[2] = 'n';
1623 buf->Name[3] = 'Q';
1624 return buf;
1625}
1626
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001627static struct create_durable *
1628create_reconnect_durable_buf(struct cifs_fid *fid)
1629{
1630 struct create_durable *buf;
1631
1632 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1633 if (!buf)
1634 return NULL;
1635
1636 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1637 (struct create_durable, Data));
1638 buf->ccontext.DataLength = cpu_to_le32(16);
1639 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1640 (struct create_durable, Name));
1641 buf->ccontext.NameLength = cpu_to_le16(4);
1642 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1643 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001644 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001645 buf->Name[0] = 'D';
1646 buf->Name[1] = 'H';
1647 buf->Name[2] = 'n';
1648 buf->Name[3] = 'C';
1649 return buf;
1650}
1651
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001652static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001653parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001654 unsigned int *epoch, char *lease_key)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001655{
1656 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001657 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001658 unsigned int next;
1659 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001660 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001661
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001662 data_offset = (char *)rsp + server->vals->header_preamble_size + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001663 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001664 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001665 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001666 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001667 if (le16_to_cpu(cc->NameLength) == 4 &&
1668 strncmp(name, "RqLs", 4) == 0)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001669 return server->ops->parse_lease_buf(cc, epoch,
1670 lease_key);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001671
1672 next = le32_to_cpu(cc->Next);
1673 if (!next)
1674 break;
1675 remaining -= next;
1676 cc = (struct create_context *)((char *)cc + next);
1677 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001678
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001679 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001680}
1681
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001682static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001683add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1684 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001685{
1686 struct smb2_create_req *req = iov[0].iov_base;
1687 unsigned int num = *num_iovec;
1688
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001689 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001690 if (iov[num].iov_base == NULL)
1691 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001692 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001693 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1694 if (!req->CreateContextsOffset)
1695 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001696 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001697 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001698 le32_add_cpu(&req->CreateContextsLength,
1699 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001700 *num_iovec = num + 1;
1701 return 0;
1702}
1703
Steve Frenchb56eae42015-11-03 09:26:27 -06001704static struct create_durable_v2 *
1705create_durable_v2_buf(struct cifs_fid *pfid)
1706{
1707 struct create_durable_v2 *buf;
1708
1709 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1710 if (!buf)
1711 return NULL;
1712
1713 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1714 (struct create_durable_v2, dcontext));
1715 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1716 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1717 (struct create_durable_v2, Name));
1718 buf->ccontext.NameLength = cpu_to_le16(4);
1719
1720 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1721 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001722 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001723 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1724
1725 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1726 buf->Name[0] = 'D';
1727 buf->Name[1] = 'H';
1728 buf->Name[2] = '2';
1729 buf->Name[3] = 'Q';
1730 return buf;
1731}
1732
1733static struct create_durable_handle_reconnect_v2 *
1734create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1735{
1736 struct create_durable_handle_reconnect_v2 *buf;
1737
1738 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1739 GFP_KERNEL);
1740 if (!buf)
1741 return NULL;
1742
1743 buf->ccontext.DataOffset =
1744 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1745 dcontext));
1746 buf->ccontext.DataLength =
1747 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1748 buf->ccontext.NameOffset =
1749 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1750 Name));
1751 buf->ccontext.NameLength = cpu_to_le16(4);
1752
1753 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1754 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1755 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1756 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1757
1758 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1759 buf->Name[0] = 'D';
1760 buf->Name[1] = 'H';
1761 buf->Name[2] = '2';
1762 buf->Name[3] = 'C';
1763 return buf;
1764}
1765
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001766static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001767add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001768 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001769{
1770 struct smb2_create_req *req = iov[0].iov_base;
1771 unsigned int num = *num_iovec;
1772
Steve Frenchb56eae42015-11-03 09:26:27 -06001773 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1774 if (iov[num].iov_base == NULL)
1775 return -ENOMEM;
1776 iov[num].iov_len = sizeof(struct create_durable_v2);
1777 if (!req->CreateContextsOffset)
1778 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001779 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001780 iov[1].iov_len);
1781 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001782 *num_iovec = num + 1;
1783 return 0;
1784}
1785
1786static int
1787add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1788 struct cifs_open_parms *oparms)
1789{
1790 struct smb2_create_req *req = iov[0].iov_base;
1791 unsigned int num = *num_iovec;
1792
1793 /* indicate that we don't need to relock the file */
1794 oparms->reconnect = false;
1795
1796 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1797 if (iov[num].iov_base == NULL)
1798 return -ENOMEM;
1799 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1800 if (!req->CreateContextsOffset)
1801 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001802 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001803 iov[1].iov_len);
1804 le32_add_cpu(&req->CreateContextsLength,
1805 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001806 *num_iovec = num + 1;
1807 return 0;
1808}
1809
1810static int
1811add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1812 struct cifs_open_parms *oparms, bool use_persistent)
1813{
1814 struct smb2_create_req *req = iov[0].iov_base;
1815 unsigned int num = *num_iovec;
1816
1817 if (use_persistent) {
1818 if (oparms->reconnect)
1819 return add_durable_reconnect_v2_context(iov, num_iovec,
1820 oparms);
1821 else
1822 return add_durable_v2_context(iov, num_iovec, oparms);
1823 }
1824
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001825 if (oparms->reconnect) {
1826 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1827 /* indicate that we don't need to relock the file */
1828 oparms->reconnect = false;
1829 } else
1830 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001831 if (iov[num].iov_base == NULL)
1832 return -ENOMEM;
1833 iov[num].iov_len = sizeof(struct create_durable);
1834 if (!req->CreateContextsOffset)
1835 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001836 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001837 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001838 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001839 *num_iovec = num + 1;
1840 return 0;
1841}
1842
Aurelien Aptelf0712922017-02-22 14:47:17 +01001843static int
1844alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1845 const char *treename, const __le16 *path)
1846{
1847 int treename_len, path_len;
1848 struct nls_table *cp;
1849 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1850
1851 /*
1852 * skip leading "\\"
1853 */
1854 treename_len = strlen(treename);
1855 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1856 return -EINVAL;
1857
1858 treename += 2;
1859 treename_len -= 2;
1860
1861 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1862
1863 /*
1864 * make room for one path separator between the treename and
1865 * path
1866 */
1867 *out_len = treename_len + 1 + path_len;
1868
1869 /*
1870 * final path needs to be null-terminated UTF16 with a
1871 * size aligned to 8
1872 */
1873
1874 *out_size = roundup((*out_len+1)*2, 8);
1875 *out_path = kzalloc(*out_size, GFP_KERNEL);
1876 if (!*out_path)
1877 return -ENOMEM;
1878
1879 cp = load_nls_default();
1880 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1881 UniStrcat(*out_path, sep);
1882 UniStrcat(*out_path, path);
1883 unload_nls(cp);
1884
1885 return 0;
1886}
1887
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001888int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001889SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001890 __u8 *oplock, struct smb2_file_all_info *buf,
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001891 struct kvec *err_iov)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001892{
1893 struct smb2_create_req *req;
1894 struct smb2_create_rsp *rsp;
1895 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001896 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001897 struct cifs_ses *ses = tcon->ses;
Steve Frenchce558b02018-05-31 19:16:54 -05001898 struct kvec iov[5]; /* make sure at least one for each open context */
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001899 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001900 int resp_buftype;
1901 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001902 __le16 *copy_path = NULL;
1903 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001904 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001905 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001906 __u32 file_attributes = 0;
Steve Frenchce558b02018-05-31 19:16:54 -05001907 char *dhc_buf = NULL, *lc_buf = NULL, *pc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001908 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001909 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001910
Joe Perchesf96637b2013-05-04 22:12:25 -05001911 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001912
1913 if (ses && (ses->server))
1914 server = ses->server;
1915 else
1916 return -EIO;
1917
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001918 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1919
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001920 if (rc)
1921 return rc;
1922
Steve French5a77e752018-05-09 17:43:08 -05001923 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001924 flags |= CIFS_TRANSFORM_REQ;
1925
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001926 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001927 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001928 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1929 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001930
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001931 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001932 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001933 /* File attributes ignored on open (used in create though) */
1934 req->FileAttributes = cpu_to_le32(file_attributes);
1935 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001936 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1937 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001938
1939 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001940 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001941 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001942
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001943 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01001944
1945 /* [MS-SMB2] 2.2.13 NameOffset:
1946 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1947 * the SMB2 header, the file name includes a prefix that will
1948 * be processed during DFS name normalization as specified in
1949 * section 3.3.5.9. Otherwise, the file name is relative to
1950 * the share that is identified by the TreeId in the SMB2
1951 * header.
1952 */
1953 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1954 int name_len;
1955
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001956 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001957 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1958 &name_len,
1959 tcon->treeName, path);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001960 if (rc) {
1961 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001962 return rc;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001963 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001964 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001965 uni_path_len = copy_size;
1966 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001967 } else {
1968 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1969 /* MUST set path len (NameLength) to 0 opening root of share */
1970 req->NameLength = cpu_to_le16(uni_path_len - 2);
1971 if (uni_path_len % 8 != 0) {
1972 copy_size = roundup(uni_path_len, 8);
1973 copy_path = kzalloc(copy_size, GFP_KERNEL);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001974 if (!copy_path) {
1975 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001976 return -ENOMEM;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001977 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001978 memcpy((char *)copy_path, (const char *)path,
1979 uni_path_len);
1980 uni_path_len = copy_size;
1981 path = copy_path;
1982 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001983 }
1984
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001985 iov[1].iov_len = uni_path_len;
1986 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001987
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001988 if (!server->oplocks)
1989 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1990
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001991 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001992 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1993 req->RequestedOplockLevel = *oplock;
1994 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001995 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001996 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001997 cifs_small_buf_release(req);
1998 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001999 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002000 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002001 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002002 }
2003
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002004 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2005 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002006 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002007 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002008 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002009 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002010 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002011 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002012
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002013 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002014 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002015 if (rc) {
2016 cifs_small_buf_release(req);
2017 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04002018 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002019 return rc;
2020 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002021 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002022 }
2023
Steve Frenchce558b02018-05-31 19:16:54 -05002024#ifdef CONFIG_CIFS_SMB311
2025 if (tcon->posix_extensions) {
2026 if (n_iov > 2) {
2027 struct create_context *ccontext =
2028 (struct create_context *)iov[n_iov-1].iov_base;
2029 ccontext->Next =
2030 cpu_to_le32(iov[n_iov-1].iov_len);
2031 }
2032
2033 rc = add_posix_context(iov, &n_iov, oparms->mode);
2034 if (rc) {
2035 cifs_small_buf_release(req);
2036 kfree(copy_path);
2037 kfree(lc_buf);
2038 kfree(dhc_buf);
2039 return rc;
2040 }
2041 pc_buf = iov[n_iov-1].iov_base;
2042 }
2043#endif /* SMB311 */
2044
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002045 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2046 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002047 cifs_small_buf_release(req);
2048 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002049
2050 if (rc != 0) {
2051 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002052 if (err_iov && rsp) {
2053 *err_iov = rsp_iov;
2054 resp_buftype = CIFS_NO_BUFFER;
2055 rsp = NULL;
2056 }
Steve French28d59362018-05-30 21:42:34 -05002057 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2058 oparms->create_options, oparms->desired_access, rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002059 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002060 } else
2061 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2062 ses->Suid, oparms->create_options,
2063 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002064
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002065 oparms->fid->persistent_fid = rsp->PersistentFileId;
2066 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002067
2068 if (buf) {
2069 memcpy(buf, &rsp->CreationTime, 32);
2070 buf->AllocationSize = rsp->AllocationSize;
2071 buf->EndOfFile = rsp->EndofFile;
2072 buf->Attributes = rsp->FileAttributes;
2073 buf->NumberOfLinks = cpu_to_le32(1);
2074 buf->DeletePending = 0;
2075 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002076
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002077 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002078 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch,
2079 oparms->fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002080 else
2081 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002082creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002083 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04002084 kfree(lc_buf);
2085 kfree(dhc_buf);
Steve Frenchce558b02018-05-31 19:16:54 -05002086 kfree(pc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002087 free_rsp_buf(resp_buftype, rsp);
2088 return rc;
2089}
2090
Steve French4a72daf2013-06-25 00:20:49 -05002091/*
2092 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2093 */
2094int
2095SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002096 u64 volatile_fid, u32 opcode, bool is_fsctl,
Aurelien Aptel51146622017-02-28 15:08:41 +01002097 char *in_data, u32 indatalen,
2098 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05002099{
2100 struct smb2_ioctl_req *req;
2101 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002102 struct smb2_sync_hdr *shdr;
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
2227 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2228 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
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002241 shdr = get_sync_hdr(rsp);
2242 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002243ioctl_exit:
2244 free_rsp_buf(resp_buftype, rsp);
2245 return rc;
2246}
2247
Steve French64a5cfa2013-10-14 15:31:32 -05002248/*
2249 * Individual callers to ioctl worker function follow
2250 */
2251
2252int
2253SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2254 u64 persistent_fid, u64 volatile_fid)
2255{
2256 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002257 struct compress_ioctl fsctl_input;
2258 char *ret_data = NULL;
2259
2260 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002261 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002262
2263 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2264 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2265 (char *)&fsctl_input /* data input */,
2266 2 /* in data len */, &ret_data /* out data */, NULL);
2267
2268 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002269
2270 return rc;
2271}
2272
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002273int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002274SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2275 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002276{
2277 struct smb2_close_req *req;
2278 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002279 struct cifs_ses *ses = tcon->ses;
2280 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002281 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002282 int resp_buftype;
2283 int rc = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002284 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002285
Joe Perchesf96637b2013-05-04 22:12:25 -05002286 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002287
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002288 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002289 return -EIO;
2290
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002291 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002292 if (rc)
2293 return rc;
2294
Steve French5a77e752018-05-09 17:43:08 -05002295 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002296 flags |= CIFS_TRANSFORM_REQ;
2297
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002298 req->PersistentFileId = persistent_fid;
2299 req->VolatileFileId = volatile_fid;
2300
2301 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002302 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002303
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002304 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002305 cifs_small_buf_release(req);
2306 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002307
2308 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002309 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002310 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2311 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002312 goto close_exit;
2313 }
2314
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002315 /* BB FIXME - decode close response, update inode for caching */
2316
2317close_exit:
2318 free_rsp_buf(resp_buftype, rsp);
2319 return rc;
2320}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002321
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002322int
2323SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2324 u64 persistent_fid, u64 volatile_fid)
2325{
2326 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2327}
2328
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002329static int
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002330validate_iov(struct TCP_Server_Info *server,
2331 unsigned int offset, unsigned int buffer_length,
2332 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002333{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002334 unsigned int smb_len = iov->iov_len;
2335 char *end_of_smb = smb_len + server->vals->header_preamble_size + (char *)iov->iov_base;
2336 char *begin_of_buf = server->vals->header_preamble_size + offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002337 char *end_of_buf = begin_of_buf + buffer_length;
2338
2339
2340 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002341 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2342 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002343 return -EINVAL;
2344 }
2345
2346 /* check if beyond RFC1001 maximum length */
2347 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002348 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2349 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002350 return -EINVAL;
2351 }
2352
2353 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002354 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002355 return -EINVAL;
2356 }
2357
2358 return 0;
2359}
2360
2361/*
2362 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2363 * Caller must free buffer.
2364 */
2365static int
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002366validate_and_copy_iov(struct TCP_Server_Info *server,
2367 unsigned int offset, unsigned int buffer_length,
2368 struct kvec *iov, unsigned int minbufsize,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002369 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002370{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002371 char *begin_of_buf = server->vals->header_preamble_size + offset + (char *)(iov->iov_base);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002372 int rc;
2373
2374 if (!data)
2375 return -EINVAL;
2376
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002377 rc = validate_iov(server, offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002378 if (rc)
2379 return rc;
2380
2381 memcpy(data, begin_of_buf, buffer_length);
2382
2383 return 0;
2384}
2385
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002386static int
2387query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002388 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2389 u32 additional_info, size_t output_len, size_t min_len, void **data,
2390 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002391{
2392 struct smb2_query_info_req *req;
2393 struct smb2_query_info_rsp *rsp = NULL;
2394 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002395 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002396 int rc = 0;
2397 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002398 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002399 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002400 unsigned int total_len;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002401
Joe Perchesf96637b2013-05-04 22:12:25 -05002402 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002403
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002404 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002405 return -EIO;
2406
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002407 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2408 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002409 if (rc)
2410 return rc;
2411
Steve French5a77e752018-05-09 17:43:08 -05002412 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002413 flags |= CIFS_TRANSFORM_REQ;
2414
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002415 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002416 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002417 req->PersistentFileId = persistent_fid;
2418 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002419 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002420
2421 /*
2422 * We do not use the input buffer (do not send extra byte)
2423 */
2424 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002425
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002426 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002427
2428 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002429 /* 1 for Buffer */
2430 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002431
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002432 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002433 cifs_small_buf_release(req);
2434 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002435
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002436 if (rc) {
2437 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002438 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2439 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002440 goto qinf_exit;
2441 }
2442
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002443 if (dlen) {
2444 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2445 if (!*data) {
2446 *data = kmalloc(*dlen, GFP_KERNEL);
2447 if (!*data) {
2448 cifs_dbg(VFS,
2449 "Error %d allocating memory for acl\n",
2450 rc);
2451 *dlen = 0;
2452 goto qinf_exit;
2453 }
2454 }
2455 }
2456
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002457 rc = validate_and_copy_iov(ses->server,
2458 le16_to_cpu(rsp->OutputBufferOffset),
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002459 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002460 &rsp_iov, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002461
2462qinf_exit:
2463 free_rsp_buf(resp_buftype, rsp);
2464 return rc;
2465}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002466
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002467int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002468 u64 persistent_fid, u64 volatile_fid,
2469 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002470{
2471 return query_info(xid, tcon, persistent_fid, volatile_fid,
2472 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002473 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002474 sizeof(struct smb2_file_full_ea_info),
2475 (void **)&data,
2476 NULL);
2477}
2478
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002479int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2480 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002481{
2482 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002483 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002484 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002485 sizeof(struct smb2_file_all_info), (void **)&data,
2486 NULL);
2487}
2488
2489int
2490SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2491 u64 persistent_fid, u64 volatile_fid,
2492 void **data, u32 *plen)
2493{
2494 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2495 *plen = 0;
2496
2497 return query_info(xid, tcon, persistent_fid, volatile_fid,
2498 0, SMB2_O_INFO_SECURITY, additional_info,
2499 SMB2_MAX_BUFFER_SIZE,
2500 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002501}
2502
2503int
2504SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2505 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2506{
2507 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002508 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002509 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002510 sizeof(struct smb2_file_internal_info),
2511 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002512}
2513
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002514/*
2515 * This is a no-op for now. We're not really interested in the reply, but
2516 * rather in the fact that the server sent one and that server->lstrp
2517 * gets updated.
2518 *
2519 * FIXME: maybe we should consider checking that the reply matches request?
2520 */
2521static void
2522smb2_echo_callback(struct mid_q_entry *mid)
2523{
2524 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002525 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002526 unsigned int credits_received = 1;
2527
2528 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002529 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002530
2531 DeleteMidQEntry(mid);
2532 add_credits(server, credits_received, CIFS_ECHO_OP);
2533}
2534
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002535void smb2_reconnect_server(struct work_struct *work)
2536{
2537 struct TCP_Server_Info *server = container_of(work,
2538 struct TCP_Server_Info, reconnect.work);
2539 struct cifs_ses *ses;
2540 struct cifs_tcon *tcon, *tcon2;
2541 struct list_head tmp_list;
2542 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002543 int rc;
2544 int resched = false;
2545
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002546
2547 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2548 mutex_lock(&server->reconnect_mutex);
2549
2550 INIT_LIST_HEAD(&tmp_list);
2551 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2552
2553 spin_lock(&cifs_tcp_ses_lock);
2554 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2555 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002556 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002557 tcon->tc_count++;
2558 list_add_tail(&tcon->rlist, &tmp_list);
2559 tcon_exist = true;
2560 }
2561 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002562 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2563 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2564 tcon_exist = true;
2565 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002566 }
2567 /*
2568 * Get the reference to server struct to be sure that the last call of
2569 * cifs_put_tcon() in the loop below won't release the server pointer.
2570 */
2571 if (tcon_exist)
2572 server->srv_count++;
2573
2574 spin_unlock(&cifs_tcp_ses_lock);
2575
2576 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002577 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2578 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002579 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002580 else
2581 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002582 list_del_init(&tcon->rlist);
2583 cifs_put_tcon(tcon);
2584 }
2585
2586 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002587 if (resched)
2588 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002589 mutex_unlock(&server->reconnect_mutex);
2590
2591 /* now we can safely release srv struct */
2592 if (tcon_exist)
2593 cifs_put_tcp_session(server, 1);
2594}
2595
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002596int
2597SMB2_echo(struct TCP_Server_Info *server)
2598{
2599 struct smb2_echo_req *req;
2600 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002601 struct kvec iov[2];
2602 struct smb_rqst rqst = { .rq_iov = iov,
2603 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002604 unsigned int total_len;
2605 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002606
Joe Perchesf96637b2013-05-04 22:12:25 -05002607 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002608
Steve French4fcd1812016-06-22 20:12:05 -05002609 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002610 /* No need to send echo on newly established connections */
2611 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2612 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002613 }
2614
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002615 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002616 if (rc)
2617 return rc;
2618
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002619 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002620
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002621 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002622 rfc1002_marker = cpu_to_be32(total_len);
2623 iov[0].iov_base = &rfc1002_marker;
2624 iov[1].iov_len = total_len;
2625 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002626
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002627 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2628 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002629 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002630 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002631
2632 cifs_small_buf_release(req);
2633 return rc;
2634}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002635
2636int
2637SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2638 u64 volatile_fid)
2639{
2640 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002641 struct cifs_ses *ses = tcon->ses;
2642 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002643 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002644 int resp_buftype;
2645 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002646 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002647 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002648
Joe Perchesf96637b2013-05-04 22:12:25 -05002649 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002650
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002651 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002652 return -EIO;
2653
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002654 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002655 if (rc)
2656 return rc;
2657
Steve French5a77e752018-05-09 17:43:08 -05002658 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002659 flags |= CIFS_TRANSFORM_REQ;
2660
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002661 req->PersistentFileId = persistent_fid;
2662 req->VolatileFileId = volatile_fid;
2663
2664 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002665 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002666
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002667 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002668 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002669
Steve Frencheccb4422018-05-17 21:16:55 -05002670 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002671 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002672 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
2673 rc);
2674 }
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002675
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002676 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002677 return rc;
2678}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002679
2680/*
2681 * To form a chain of read requests, any read requests after the first should
2682 * have the end_of_chain boolean set to true.
2683 */
2684static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002685smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002686 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2687 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002688{
2689 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002690 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002691 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002692 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002693
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002694 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2695 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002696 if (rc)
2697 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002698
2699 server = io_parms->tcon->ses->server;
2700 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002701 return -ECONNABORTED;
2702
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002703 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002704 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002705
2706 req->PersistentFileId = io_parms->persistent_fid;
2707 req->VolatileFileId = io_parms->volatile_fid;
2708 req->ReadChannelInfoOffset = 0; /* reserved */
2709 req->ReadChannelInfoLength = 0; /* reserved */
2710 req->Channel = 0; /* reserved */
2711 req->MinimumCount = 0;
2712 req->Length = cpu_to_le32(io_parms->length);
2713 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07002714#ifdef CONFIG_CIFS_SMB_DIRECT
2715 /*
2716 * If we want to do a RDMA write, fill in and append
2717 * smbd_buffer_descriptor_v1 to the end of read request
2718 */
Long Libb4c0412018-04-17 12:17:08 -07002719 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07002720 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002721
Long Libd3dcc62017-11-22 17:38:47 -07002722 struct smbd_buffer_descriptor_v1 *v1;
2723 bool need_invalidate =
2724 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2725
2726 rdata->mr = smbd_register_mr(
2727 server->smbd_conn, rdata->pages,
2728 rdata->nr_pages, rdata->tailsz,
2729 true, need_invalidate);
2730 if (!rdata->mr)
2731 return -ENOBUFS;
2732
2733 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2734 if (need_invalidate)
2735 req->Channel = SMB2_CHANNEL_RDMA_V1;
2736 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002737 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07002738 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002739 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07002740 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002741 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2742 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2743 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07002744
2745 *total_len += sizeof(*v1) - 1;
2746 }
2747#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002748 if (request_type & CHAINED_REQUEST) {
2749 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002750 /* next 8-byte aligned request */
2751 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2752 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002753 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002754 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002755 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002756 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002757 /*
2758 * Related requests use info from previous read request
2759 * in chain.
2760 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002761 shdr->SessionId = 0xFFFFFFFF;
2762 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002763 req->PersistentFileId = 0xFFFFFFFF;
2764 req->VolatileFileId = 0xFFFFFFFF;
2765 }
2766 }
2767 if (remaining_bytes > io_parms->length)
2768 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2769 else
2770 req->RemainingBytes = 0;
2771
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002772 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002773 return rc;
2774}
2775
2776static void
2777smb2_readv_callback(struct mid_q_entry *mid)
2778{
2779 struct cifs_readdata *rdata = mid->callback_data;
2780 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2781 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002782 struct smb2_sync_hdr *shdr =
2783 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002784 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002785 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2786 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002787 .rq_pages = rdata->pages,
2788 .rq_npages = rdata->nr_pages,
2789 .rq_pagesz = rdata->pagesz,
2790 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002791
Joe Perchesf96637b2013-05-04 22:12:25 -05002792 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2793 __func__, mid->mid, mid->mid_state, rdata->result,
2794 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002795
2796 switch (mid->mid_state) {
2797 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002798 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002799 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002800 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002801 int rc;
2802
Jeff Layton0b688cf2012-09-18 16:20:34 -07002803 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002804 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002805 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2806 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002807 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002808 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002809 task_io_account_read(rdata->got_bytes);
2810 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002811 break;
2812 case MID_REQUEST_SUBMITTED:
2813 case MID_RETRY_NEEDED:
2814 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002815 if (server->sign && rdata->got_bytes)
2816 /* reset bytes number since we can not check a sign */
2817 rdata->got_bytes = 0;
2818 /* FIXME: should this be counted toward the initiating task? */
2819 task_io_account_read(rdata->got_bytes);
2820 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002821 break;
2822 default:
2823 if (rdata->result != -ENODATA)
2824 rdata->result = -EIO;
2825 }
Long Libd3dcc62017-11-22 17:38:47 -07002826#ifdef CONFIG_CIFS_SMB_DIRECT
2827 /*
2828 * If this rdata has a memmory registered, the MR can be freed
2829 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2830 * because they have limited number and are used for future I/Os
2831 */
2832 if (rdata->mr) {
2833 smbd_deregister_mr(rdata->mr);
2834 rdata->mr = NULL;
2835 }
2836#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002837 if (rdata->result)
2838 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2839
2840 queue_work(cifsiod_wq, &rdata->work);
2841 DeleteMidQEntry(mid);
2842 add_credits(server, credits_received, 0);
2843}
2844
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002845/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002846int
2847smb2_async_readv(struct cifs_readdata *rdata)
2848{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002849 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002850 char *buf;
2851 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002852 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002853 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2854 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002855 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002856 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002857 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002858
Joe Perchesf96637b2013-05-04 22:12:25 -05002859 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2860 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002861
2862 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2863 io_parms.offset = rdata->offset;
2864 io_parms.length = rdata->bytes;
2865 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2866 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2867 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002868
2869 server = io_parms.tcon->ses->server;
2870
Long Li2dabfd52017-11-07 01:54:53 -07002871 rc = smb2_new_read_req(
2872 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002873 if (rc) {
2874 if (rc == -EAGAIN && rdata->credits) {
2875 /* credits was reset by reconnect */
2876 rdata->credits = 0;
2877 /* reduce in_flight value since we won't send the req */
2878 spin_lock(&server->req_lock);
2879 server->in_flight--;
2880 spin_unlock(&server->req_lock);
2881 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002882 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002883 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002884
Steve French5a77e752018-05-09 17:43:08 -05002885 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002886 flags |= CIFS_TRANSFORM_REQ;
2887
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002888 req_len = cpu_to_be32(total_len);
2889
2890 rdata->iov[0].iov_base = &req_len;
2891 rdata->iov[0].iov_len = sizeof(__be32);
2892 rdata->iov[1].iov_base = buf;
2893 rdata->iov[1].iov_len = total_len;
2894
2895 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002896
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002897 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002898 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002899 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002900 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002901 spin_lock(&server->req_lock);
2902 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002903 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002904 spin_unlock(&server->req_lock);
2905 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002906 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002907 }
2908
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002909 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002910 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002911 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002912 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002913 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002914 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002915 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002916 trace_smb3_read_err(rc, 0 /* xid */, io_parms.persistent_fid,
2917 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
2918 io_parms.offset, io_parms.length);
2919 } else
2920 trace_smb3_read_done(0 /* xid */, io_parms.persistent_fid,
2921 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
2922 io_parms.offset, io_parms.length);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002923
2924 cifs_small_buf_release(buf);
2925 return rc;
2926}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002927
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002928int
2929SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2930 unsigned int *nbytes, char **buf, int *buf_type)
2931{
2932 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002933 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002934 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002935 struct smb2_sync_hdr *shdr;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002936 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002937 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002938 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002939 int flags = CIFS_LOG_ERROR;
2940 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002941
2942 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07002943 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002944 if (rc)
2945 return rc;
2946
Steve French5a77e752018-05-09 17:43:08 -05002947 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002948 flags |= CIFS_TRANSFORM_REQ;
2949
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002950 iov[0].iov_base = (char *)req;
2951 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002952
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002953 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002954 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002955
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002956 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002957
2958 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002959 if (rc != -ENODATA) {
2960 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2961 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002962 }
Steve Frencheccb4422018-05-17 21:16:55 -05002963 trace_smb3_read_err(rc, xid, req->PersistentFileId,
2964 io_parms->tcon->tid, ses->Suid,
2965 io_parms->offset, io_parms->length);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002966 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2967 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05002968 } else
2969 trace_smb3_read_done(xid, req->PersistentFileId,
2970 io_parms->tcon->tid, ses->Suid,
2971 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002972
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002973 *nbytes = le32_to_cpu(rsp->DataLength);
2974 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2975 (*nbytes > io_parms->length)) {
2976 cifs_dbg(FYI, "bad length %d for count %d\n",
2977 *nbytes, io_parms->length);
2978 rc = -EIO;
2979 *nbytes = 0;
2980 }
2981
2982 shdr = get_sync_hdr(rsp);
2983
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002984 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002985 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002986 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002987 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002988 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002989 if (resp_buftype == CIFS_SMALL_BUFFER)
2990 *buf_type = CIFS_SMALL_BUFFER;
2991 else if (resp_buftype == CIFS_LARGE_BUFFER)
2992 *buf_type = CIFS_LARGE_BUFFER;
2993 }
2994 return rc;
2995}
2996
Pavel Shilovsky33319142012-09-18 16:20:29 -07002997/*
2998 * Check the mid_state and signature on received buffer (if any), and queue the
2999 * workqueue completion task.
3000 */
3001static void
3002smb2_writev_callback(struct mid_q_entry *mid)
3003{
3004 struct cifs_writedata *wdata = mid->callback_data;
3005 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3006 unsigned int written;
3007 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3008 unsigned int credits_received = 1;
3009
3010 switch (mid->mid_state) {
3011 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003012 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003013 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
3014 if (wdata->result != 0)
3015 break;
3016
3017 written = le32_to_cpu(rsp->DataLength);
3018 /*
3019 * Mask off high 16 bits when bytes written as returned
3020 * by the server is greater than bytes requested by the
3021 * client. OS/2 servers are known to set incorrect
3022 * CountHigh values.
3023 */
3024 if (written > wdata->bytes)
3025 written &= 0xFFFF;
3026
3027 if (written < wdata->bytes)
3028 wdata->result = -ENOSPC;
3029 else
3030 wdata->bytes = written;
3031 break;
3032 case MID_REQUEST_SUBMITTED:
3033 case MID_RETRY_NEEDED:
3034 wdata->result = -EAGAIN;
3035 break;
3036 default:
3037 wdata->result = -EIO;
3038 break;
3039 }
Long Lidb223a52017-11-22 17:38:45 -07003040#ifdef CONFIG_CIFS_SMB_DIRECT
3041 /*
3042 * If this wdata has a memory registered, the MR can be freed
3043 * The number of MRs available is limited, it's important to recover
3044 * used MR as soon as I/O is finished. Hold MR longer in the later
3045 * I/O process can possibly result in I/O deadlock due to lack of MR
3046 * to send request on I/O retry
3047 */
3048 if (wdata->mr) {
3049 smbd_deregister_mr(wdata->mr);
3050 wdata->mr = NULL;
3051 }
3052#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003053 if (wdata->result)
3054 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3055
3056 queue_work(cifsiod_wq, &wdata->work);
3057 DeleteMidQEntry(mid);
3058 add_credits(tcon->ses->server, credits_received, 0);
3059}
3060
3061/* smb2_async_writev - send an async write, and set up mid to handle result */
3062int
Steve French4a5c80d2014-02-07 20:45:12 -06003063smb2_async_writev(struct cifs_writedata *wdata,
3064 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07003065{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003066 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003067 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003068 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003069 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003070 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003071 struct kvec iov[2];
3072 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003073 unsigned int total_len;
3074 __be32 rfc1002_marker;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003075
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003076 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003077 if (rc) {
3078 if (rc == -EAGAIN && wdata->credits) {
3079 /* credits was reset by reconnect */
3080 wdata->credits = 0;
3081 /* reduce in_flight value since we won't send the req */
3082 spin_lock(&server->req_lock);
3083 server->in_flight--;
3084 spin_unlock(&server->req_lock);
3085 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003086 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003087 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003088
Steve French5a77e752018-05-09 17:43:08 -05003089 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003090 flags |= CIFS_TRANSFORM_REQ;
3091
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003092 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003093 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003094
3095 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3096 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3097 req->WriteChannelInfoOffset = 0;
3098 req->WriteChannelInfoLength = 0;
3099 req->Channel = 0;
3100 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003101 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003102 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003103 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07003104#ifdef CONFIG_CIFS_SMB_DIRECT
3105 /*
3106 * If we want to do a server RDMA read, fill in and append
3107 * smbd_buffer_descriptor_v1 to the end of write request
3108 */
Long Libb4c0412018-04-17 12:17:08 -07003109 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003110 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003111
Long Lidb223a52017-11-22 17:38:45 -07003112 struct smbd_buffer_descriptor_v1 *v1;
3113 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3114
3115 wdata->mr = smbd_register_mr(
3116 server->smbd_conn, wdata->pages,
3117 wdata->nr_pages, wdata->tailsz,
3118 false, need_invalidate);
3119 if (!wdata->mr) {
3120 rc = -ENOBUFS;
3121 goto async_writev_out;
3122 }
3123 req->Length = 0;
3124 req->DataOffset = 0;
3125 req->RemainingBytes =
Steve French2026b062018-01-24 23:07:41 -06003126 cpu_to_le32((wdata->nr_pages-1)*PAGE_SIZE + wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003127 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3128 if (need_invalidate)
3129 req->Channel = SMB2_CHANNEL_RDMA_V1;
3130 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003131 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003132 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003133 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003134 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003135 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3136 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3137 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003138 }
3139#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003140 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003141 iov[0].iov_len = 4;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003142 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
3143 iov[0].iov_base = &rfc1002_marker;
3144 iov[1].iov_len = total_len - 1;
3145 iov[1].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003146
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003147 rqst.rq_iov = iov;
3148 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003149 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07003150 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003151 rqst.rq_npages = wdata->nr_pages;
3152 rqst.rq_pagesz = wdata->pagesz;
3153 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003154#ifdef CONFIG_CIFS_SMB_DIRECT
3155 if (wdata->mr) {
3156 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3157 rqst.rq_npages = 0;
3158 }
3159#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003160 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3161 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003162
Long Lidb223a52017-11-22 17:38:45 -07003163#ifdef CONFIG_CIFS_SMB_DIRECT
3164 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3165 if (!wdata->mr)
3166 req->Length = cpu_to_le32(wdata->bytes);
3167#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003168 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003169#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003170
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003171 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003172 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003173 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003174 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003175 spin_lock(&server->req_lock);
3176 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003177 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003178 spin_unlock(&server->req_lock);
3179 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003180 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003181 }
3182
Pavel Shilovsky33319142012-09-18 16:20:29 -07003183 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003184 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3185 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003186
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003187 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003188 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3189 tcon->tid, tcon->ses->Suid, wdata->offset,
3190 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06003191 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003192 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003193 } else
3194 trace_smb3_write_done(0 /* no xid */, req->PersistentFileId,
3195 tcon->tid, tcon->ses->Suid, wdata->offset,
3196 wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003197
Pavel Shilovsky33319142012-09-18 16:20:29 -07003198async_writev_out:
3199 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003200 return rc;
3201}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003202
3203/*
3204 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3205 * The length field from io_parms must be at least 1 and indicates a number of
3206 * elements with data to write that begins with position 1 in iov array. All
3207 * data length is specified by count.
3208 */
3209int
3210SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3211 unsigned int *nbytes, struct kvec *iov, int n_vec)
3212{
3213 int rc = 0;
3214 struct smb2_write_req *req = NULL;
3215 struct smb2_write_rsp *rsp = NULL;
3216 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003217 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003218 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003219 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003220
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003221 *nbytes = 0;
3222
3223 if (n_vec < 1)
3224 return rc;
3225
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003226 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3227 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003228 if (rc)
3229 return rc;
3230
3231 if (io_parms->tcon->ses->server == NULL)
3232 return -ECONNABORTED;
3233
Steve French5a77e752018-05-09 17:43:08 -05003234 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003235 flags |= CIFS_TRANSFORM_REQ;
3236
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003237 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003238
3239 req->PersistentFileId = io_parms->persistent_fid;
3240 req->VolatileFileId = io_parms->volatile_fid;
3241 req->WriteChannelInfoOffset = 0;
3242 req->WriteChannelInfoLength = 0;
3243 req->Channel = 0;
3244 req->Length = cpu_to_le32(io_parms->length);
3245 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003246 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003247 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003248 req->RemainingBytes = 0;
3249
3250 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003251 /* 1 for Buffer */
3252 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003253
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003254 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
3255 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003256 cifs_small_buf_release(req);
3257 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003258
3259 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003260 trace_smb3_write_err(xid, req->PersistentFileId,
3261 io_parms->tcon->tid,
3262 io_parms->tcon->ses->Suid,
3263 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003264 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003265 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05003266 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003267 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05003268 trace_smb3_write_done(xid, req->PersistentFileId,
3269 io_parms->tcon->tid,
3270 io_parms->tcon->ses->Suid,
3271 io_parms->offset, *nbytes);
3272 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003273
3274 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003275 return rc;
3276}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003277
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003278static unsigned int
3279num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3280{
3281 int len;
3282 unsigned int entrycount = 0;
3283 unsigned int next_offset = 0;
3284 FILE_DIRECTORY_INFO *entryptr;
3285
3286 if (bufstart == NULL)
3287 return 0;
3288
3289 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3290
3291 while (1) {
3292 entryptr = (FILE_DIRECTORY_INFO *)
3293 ((char *)entryptr + next_offset);
3294
3295 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003296 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003297 break;
3298 }
3299
3300 len = le32_to_cpu(entryptr->FileNameLength);
3301 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003302 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3303 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003304 break;
3305 }
3306
3307 *lastentry = (char *)entryptr;
3308 entrycount++;
3309
3310 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3311 if (!next_offset)
3312 break;
3313 }
3314
3315 return entrycount;
3316}
3317
3318/*
3319 * Readdir/FindFirst
3320 */
3321int
3322SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3323 u64 persistent_fid, u64 volatile_fid, int index,
3324 struct cifs_search_info *srch_inf)
3325{
3326 struct smb2_query_directory_req *req;
3327 struct smb2_query_directory_rsp *rsp = NULL;
3328 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003329 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003330 int rc = 0;
3331 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003332 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003333 unsigned char *bufptr;
3334 struct TCP_Server_Info *server;
3335 struct cifs_ses *ses = tcon->ses;
3336 __le16 asteriks = cpu_to_le16('*');
3337 char *end_of_smb;
3338 unsigned int output_size = CIFSMaxBufSize;
3339 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003340 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003341 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003342
3343 if (ses && (ses->server))
3344 server = ses->server;
3345 else
3346 return -EIO;
3347
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003348 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3349 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003350 if (rc)
3351 return rc;
3352
Steve French5a77e752018-05-09 17:43:08 -05003353 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003354 flags |= CIFS_TRANSFORM_REQ;
3355
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003356 switch (srch_inf->info_level) {
3357 case SMB_FIND_FILE_DIRECTORY_INFO:
3358 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3359 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3360 break;
3361 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3362 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3363 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3364 break;
3365 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003366 cifs_dbg(VFS, "info level %u isn't supported\n",
3367 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003368 rc = -EINVAL;
3369 goto qdir_exit;
3370 }
3371
3372 req->FileIndex = cpu_to_le32(index);
3373 req->PersistentFileId = persistent_fid;
3374 req->VolatileFileId = volatile_fid;
3375
3376 len = 0x2;
3377 bufptr = req->Buffer;
3378 memcpy(bufptr, &asteriks, len);
3379
3380 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003381 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003382 req->FileNameLength = cpu_to_le16(len);
3383 /*
3384 * BB could be 30 bytes or so longer if we used SMB2 specific
3385 * buffer lengths, but this is safe and close enough.
3386 */
3387 output_size = min_t(unsigned int, output_size, server->maxBuf);
3388 output_size = min_t(unsigned int, output_size, 2 << 15);
3389 req->OutputBufferLength = cpu_to_le32(output_size);
3390
3391 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003392 /* 1 for Buffer */
3393 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003394
3395 iov[1].iov_base = (char *)(req->Buffer);
3396 iov[1].iov_len = len;
3397
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003398 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003399 cifs_small_buf_release(req);
3400 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003401
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003402 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003403 if (rc == -ENODATA &&
3404 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003405 srch_inf->endOfSearch = true;
3406 rc = 0;
3407 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003408 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3409 goto qdir_exit;
3410 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003411
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003412 rc = validate_iov(server,
3413 le16_to_cpu(rsp->OutputBufferOffset),
3414 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003415 info_buf_size);
3416 if (rc)
3417 goto qdir_exit;
3418
3419 srch_inf->unicode = true;
3420
3421 if (srch_inf->ntwrk_buf_start) {
3422 if (srch_inf->smallBuf)
3423 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3424 else
3425 cifs_buf_release(srch_inf->ntwrk_buf_start);
3426 }
3427 srch_inf->ntwrk_buf_start = (char *)rsp;
3428 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3429 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3430 /* 4 for rfc1002 length field */
3431 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3432 srch_inf->entries_in_buffer =
3433 num_entries(srch_inf->srch_entries_start, end_of_smb,
3434 &srch_inf->last_entry, info_buf_size);
3435 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003436 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3437 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3438 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003439 if (resp_buftype == CIFS_LARGE_BUFFER)
3440 srch_inf->smallBuf = false;
3441 else if (resp_buftype == CIFS_SMALL_BUFFER)
3442 srch_inf->smallBuf = true;
3443 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003444 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003445
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003446 return rc;
3447
3448qdir_exit:
3449 free_rsp_buf(resp_buftype, rsp);
3450 return rc;
3451}
3452
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003453static int
3454send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003455 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3456 u8 info_type, u32 additional_info, unsigned int num,
3457 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003458{
3459 struct smb2_set_info_req *req;
3460 struct smb2_set_info_rsp *rsp = NULL;
3461 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003462 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003463 int rc = 0;
3464 int resp_buftype;
3465 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003466 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003467 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003468 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003469
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003470 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003471 return -EIO;
3472
3473 if (!num)
3474 return -EINVAL;
3475
3476 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3477 if (!iov)
3478 return -ENOMEM;
3479
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003480 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003481 if (rc) {
3482 kfree(iov);
3483 return rc;
3484 }
3485
Steve French5a77e752018-05-09 17:43:08 -05003486 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003487 flags |= CIFS_TRANSFORM_REQ;
3488
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003489 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003490
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003491 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003492 req->FileInfoClass = info_class;
3493 req->PersistentFileId = persistent_fid;
3494 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003495 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003496
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003497 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003498 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003499 req->BufferLength = cpu_to_le32(*size);
3500
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003501 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003502 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003503
3504 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003505 /* 1 for Buffer */
3506 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003507
3508 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003509 le32_add_cpu(&req->BufferLength, size[i]);
3510 iov[i].iov_base = (char *)data[i];
3511 iov[i].iov_len = size[i];
3512 }
3513
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003514 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3515 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003516 cifs_small_buf_release(req);
3517 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003518
Steve Frencheccb4422018-05-17 21:16:55 -05003519 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003520 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003521 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
3522 ses->Suid, info_class, (__u32)info_type, rc);
3523 }
Steve French7d3fb242013-11-18 09:56:28 -06003524
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003525 free_rsp_buf(resp_buftype, rsp);
3526 kfree(iov);
3527 return rc;
3528}
3529
3530int
3531SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3532 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3533{
3534 struct smb2_file_rename_info info;
3535 void **data;
3536 unsigned int size[2];
3537 int rc;
3538 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3539
3540 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3541 if (!data)
3542 return -ENOMEM;
3543
3544 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3545 /* 0 = fail if target already exists */
3546 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3547 info.FileNameLength = cpu_to_le32(len);
3548
3549 data[0] = &info;
3550 size[0] = sizeof(struct smb2_file_rename_info);
3551
3552 data[1] = target_file;
3553 size[1] = len + 2 /* null */;
3554
3555 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003556 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3557 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003558 kfree(data);
3559 return rc;
3560}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003561
3562int
Steve French897fba12016-05-12 21:20:36 -05003563SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3564 u64 persistent_fid, u64 volatile_fid)
3565{
3566 __u8 delete_pending = 1;
3567 void *data;
3568 unsigned int size;
3569
3570 data = &delete_pending;
3571 size = 1; /* sizeof __u8 */
3572
3573 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003574 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3575 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003576}
3577
3578int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003579SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3580 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3581{
3582 struct smb2_file_link_info info;
3583 void **data;
3584 unsigned int size[2];
3585 int rc;
3586 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3587
3588 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3589 if (!data)
3590 return -ENOMEM;
3591
3592 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3593 /* 0 = fail if link already exists */
3594 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3595 info.FileNameLength = cpu_to_le32(len);
3596
3597 data[0] = &info;
3598 size[0] = sizeof(struct smb2_file_link_info);
3599
3600 data[1] = target_file;
3601 size[1] = len + 2 /* null */;
3602
3603 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003604 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3605 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003606 kfree(data);
3607 return rc;
3608}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003609
3610int
3611SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003612 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003613{
3614 struct smb2_file_eof_info info;
3615 void *data;
3616 unsigned int size;
3617
3618 info.EndOfFile = *eof;
3619
3620 data = &info;
3621 size = sizeof(struct smb2_file_eof_info);
3622
Steve Frenchf29ebb42014-07-19 21:44:58 -05003623 if (is_falloc)
3624 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003625 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3626 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003627 else
3628 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003629 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3630 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003631}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003632
3633int
3634SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3635 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3636{
3637 unsigned int size;
3638 size = sizeof(FILE_BASIC_INFO);
3639 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003640 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3641 0, 1, (void **)&buf, &size);
3642}
3643
3644int
3645SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3646 u64 persistent_fid, u64 volatile_fid,
3647 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3648{
3649 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3650 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3651 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003652}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003653
3654int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003655SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3656 u64 persistent_fid, u64 volatile_fid,
3657 struct smb2_file_full_ea_info *buf, int len)
3658{
3659 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3660 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3661 0, 1, (void **)&buf, &len);
3662}
3663
3664int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003665SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3666 const u64 persistent_fid, const u64 volatile_fid,
3667 __u8 oplock_level)
3668{
3669 int rc;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003670 struct smb2_oplock_break_req *req = NULL;
3671 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003672 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003673 unsigned int total_len;
3674 struct kvec iov[1];
3675 struct kvec rsp_iov;
3676 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003677
Joe Perchesf96637b2013-05-04 22:12:25 -05003678 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003679 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3680 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003681 if (rc)
3682 return rc;
3683
Steve French5a77e752018-05-09 17:43:08 -05003684 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003685 flags |= CIFS_TRANSFORM_REQ;
3686
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003687 req->VolatileFid = volatile_fid;
3688 req->PersistentFid = persistent_fid;
3689 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003690 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003691
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003692 flags |= CIFS_NO_RESP;
3693
3694 iov[0].iov_base = (char *)req;
3695 iov[0].iov_len = total_len;
3696
3697 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003698 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003699
3700 if (rc) {
3701 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003702 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003703 }
3704
3705 return rc;
3706}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003707
3708static void
3709copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3710 struct kstatfs *kst)
3711{
3712 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3713 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3714 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303715 kst->f_bfree = kst->f_bavail =
3716 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003717 return;
3718}
3719
3720static int
3721build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3722 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3723{
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003724 struct TCP_Server_Info *server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003725 int rc;
3726 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003727 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003728
Joe Perchesf96637b2013-05-04 22:12:25 -05003729 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003730
3731 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3732 return -EIO;
3733
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003734 server = tcon->ses->server;
3735
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003736 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3737 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003738 if (rc)
3739 return rc;
3740
3741 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3742 req->FileInfoClass = level;
3743 req->PersistentFileId = persistent_fid;
3744 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003745 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003746 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003747 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003748 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003749 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - server->vals->header_preamble_size);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003750
3751 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003752 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003753 return 0;
3754}
3755
3756int
3757SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3758 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3759{
3760 struct smb2_query_info_rsp *rsp = NULL;
3761 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003762 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003763 int rc = 0;
3764 int resp_buftype;
3765 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003766 struct TCP_Server_Info *server = ses->server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003767 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003768 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003769
3770 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3771 sizeof(struct smb2_fs_full_size_info),
3772 persistent_fid, volatile_fid);
3773 if (rc)
3774 return rc;
3775
Steve French5a77e752018-05-09 17:43:08 -05003776 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003777 flags |= CIFS_TRANSFORM_REQ;
3778
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003779 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003780 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003781 if (rc) {
3782 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003783 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003784 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003785 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003786
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003787 info = (struct smb2_fs_full_size_info *)(server->vals->header_preamble_size +
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003788 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003789 rc = validate_iov(server,
3790 le16_to_cpu(rsp->OutputBufferOffset),
3791 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003792 sizeof(struct smb2_fs_full_size_info));
3793 if (!rc)
3794 copy_fs_info_to_kstatfs(info, fsdata);
3795
Steve French34f62642013-10-09 02:07:00 -05003796qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003797 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003798 return rc;
3799}
3800
3801int
3802SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003803 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003804{
3805 struct smb2_query_info_rsp *rsp = NULL;
3806 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003807 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003808 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003809 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003810 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003811 struct TCP_Server_Info *server = ses->server;
Steve French34f62642013-10-09 02:07:00 -05003812 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003813 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003814
Steven French21671142013-10-09 13:36:35 -05003815 if (level == FS_DEVICE_INFORMATION) {
3816 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3817 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3818 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3819 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3820 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003821 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3822 max_len = sizeof(struct smb3_fs_ss_info);
3823 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003824 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003825 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003826 return -EINVAL;
3827 }
3828
3829 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003830 persistent_fid, volatile_fid);
3831 if (rc)
3832 return rc;
3833
Steve French5a77e752018-05-09 17:43:08 -05003834 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003835 flags |= CIFS_TRANSFORM_REQ;
3836
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003837 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003838 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003839 if (rc) {
3840 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3841 goto qfsattr_exit;
3842 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003843 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003844
3845 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3846 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003847 rc = validate_iov(server, offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05003848 if (rc)
3849 goto qfsattr_exit;
3850
3851 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003852 memcpy(&tcon->fsAttrInfo, server->vals->header_preamble_size + offset
Steve French34f62642013-10-09 02:07:00 -05003853 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003854 rsp_len, max_len));
3855 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003856 memcpy(&tcon->fsDevInfo, server->vals->header_preamble_size + offset
Steven French21671142013-10-09 13:36:35 -05003857 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003858 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3859 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003860 (server->vals->header_preamble_size + offset + (char *)&rsp->hdr);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003861 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3862 tcon->perf_sector_size =
3863 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3864 }
Steve French34f62642013-10-09 02:07:00 -05003865
3866qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003867 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003868 return rc;
3869}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003870
3871int
3872smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3873 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3874 const __u32 num_lock, struct smb2_lock_element *buf)
3875{
3876 int rc = 0;
3877 struct smb2_lock_req *req = NULL;
3878 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003879 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003880 int resp_buf_type;
3881 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003882 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003883 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003884
Joe Perchesf96637b2013-05-04 22:12:25 -05003885 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003886
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003887 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003888 if (rc)
3889 return rc;
3890
Steve French5a77e752018-05-09 17:43:08 -05003891 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003892 flags |= CIFS_TRANSFORM_REQ;
3893
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003894 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003895 req->LockCount = cpu_to_le16(num_lock);
3896
3897 req->PersistentFileId = persist_fid;
3898 req->VolatileFileId = volatile_fid;
3899
3900 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003901
3902 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003903 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003904 iov[1].iov_base = (char *)buf;
3905 iov[1].iov_len = count;
3906
3907 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003908 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3909 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003910 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003911 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003912 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003913 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003914 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
3915 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003916 }
3917
3918 return rc;
3919}
3920
3921int
3922SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3923 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3924 const __u64 length, const __u64 offset, const __u32 lock_flags,
3925 const bool wait)
3926{
3927 struct smb2_lock_element lock;
3928
3929 lock.Offset = cpu_to_le64(offset);
3930 lock.Length = cpu_to_le64(length);
3931 lock.Flags = cpu_to_le32(lock_flags);
3932 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3933 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3934
3935 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3936}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003937
3938int
3939SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3940 __u8 *lease_key, const __le32 lease_state)
3941{
3942 int rc;
3943 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003944 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003945 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003946 unsigned int total_len;
3947 struct kvec iov[1];
3948 struct kvec rsp_iov;
3949 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003950
Joe Perchesf96637b2013-05-04 22:12:25 -05003951 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003952 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3953 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003954 if (rc)
3955 return rc;
3956
Steve French5a77e752018-05-09 17:43:08 -05003957 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003958 flags |= CIFS_TRANSFORM_REQ;
3959
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003960 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003961 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003962 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003963
3964 memcpy(req->LeaseKey, lease_key, 16);
3965 req->LeaseState = lease_state;
3966
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003967 flags |= CIFS_NO_RESP;
3968
3969 iov[0].iov_base = (char *)req;
3970 iov[0].iov_len = total_len;
3971
3972 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003973 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003974
3975 if (rc) {
3976 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003977 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003978 }
3979
3980 return rc;
3981}