blob: c48608c5a0fb993f6354386d6ed3c7d4a9627af0 [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020036#include <linux/uuid.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070037#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040038#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070048#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070049#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050050#include "cifs_spnego.h"
Long Lidb223a52017-11-22 17:38:45 -070051#include "smbdirect.h"
Steve Frencheccb4422018-05-17 21:16:55 -050052#include "trace.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040053
54/*
55 * The following table defines the expected "StructureSize" of SMB2 requests
56 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
57 *
58 * Note that commands are defined in smb2pdu.h in le16 but the array below is
59 * indexed by command in host byte order.
60 */
61static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
62 /* SMB2_NEGOTIATE */ 36,
63 /* SMB2_SESSION_SETUP */ 25,
64 /* SMB2_LOGOFF */ 4,
65 /* SMB2_TREE_CONNECT */ 9,
66 /* SMB2_TREE_DISCONNECT */ 4,
67 /* SMB2_CREATE */ 57,
68 /* SMB2_CLOSE */ 24,
69 /* SMB2_FLUSH */ 24,
70 /* SMB2_READ */ 49,
71 /* SMB2_WRITE */ 49,
72 /* SMB2_LOCK */ 48,
73 /* SMB2_IOCTL */ 57,
74 /* SMB2_CANCEL */ 4,
75 /* SMB2_ECHO */ 4,
76 /* SMB2_QUERY_DIRECTORY */ 33,
77 /* SMB2_CHANGE_NOTIFY */ 32,
78 /* SMB2_QUERY_INFO */ 41,
79 /* SMB2_SET_INFO */ 33,
80 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
81};
82
Steve French5a77e752018-05-09 17:43:08 -050083static int smb3_encryption_required(const struct cifs_tcon *tcon)
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070084{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080085 if (!tcon)
86 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070087 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
88 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
89 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080090 if (tcon->seal &&
91 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
92 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070093 return 0;
94}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040095
96static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070097smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040098 const struct cifs_tcon *tcon)
99{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700100 shdr->ProtocolId = SMB2_PROTO_NUMBER;
101 shdr->StructureSize = cpu_to_le16(64);
102 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100103 if (tcon && tcon->ses && tcon->ses->server) {
104 struct TCP_Server_Info *server = tcon->ses->server;
105
106 spin_lock(&server->req_lock);
107 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500108 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700109 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100110 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700111 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500112 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100113 server->credits, 2));
114 spin_unlock(&server->req_lock);
115 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700116 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100117 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700118 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400119
120 if (!tcon)
121 goto out;
122
Steve French2b80d042013-06-23 18:43:37 -0500123 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
124 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500125 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500126 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700127 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500128 /* else CreditCharge MBZ */
129
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700130 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400131 /* Uid is not converted */
132 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700133 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500134
135 /*
136 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
137 * to pass the path on the Open SMB prefixed by \\server\share.
138 * Not sure when we would need to do the augmented path (if ever) and
139 * setting this flag breaks the SMB2 open operation since it is
140 * illegal to send an empty path name (without \\server\share prefix)
141 * when the DFS flag is set in the SMB open header. We could
142 * consider setting the flag on all operations other than open
143 * but it is safer to net set it for now.
144 */
145/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700146 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500147
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700148 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
Steve French5a77e752018-05-09 17:43:08 -0500149 !smb3_encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700150 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400151out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400152 return;
153}
154
155static int
156smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
157{
158 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400159 struct nls_table *nls_codepage;
160 struct cifs_ses *ses;
161 struct TCP_Server_Info *server;
162
163 /*
164 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
165 * check for tcp and smb session status done differently
166 * for those three - in the calling routine.
167 */
168 if (tcon == NULL)
169 return rc;
170
171 if (smb2_command == SMB2_TREE_CONNECT)
172 return rc;
173
174 if (tcon->tidStatus == CifsExiting) {
175 /*
176 * only tree disconnect, open, and write,
177 * (and ulogoff which does not have tcon)
178 * are allowed as we start force umount.
179 */
180 if ((smb2_command != SMB2_WRITE) &&
181 (smb2_command != SMB2_CREATE) &&
182 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500183 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
184 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400185 return -ENODEV;
186 }
187 }
188 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
189 (!tcon->ses->server))
190 return -EIO;
191
192 ses = tcon->ses;
193 server = ses->server;
194
195 /*
196 * Give demultiplex thread up to 10 seconds to reconnect, should be
197 * greater than cifs socket timeout which is 7 seconds
198 */
199 while (server->tcpStatus == CifsNeedReconnect) {
200 /*
201 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
202 * here since they are implicitly done when session drops.
203 */
204 switch (smb2_command) {
205 /*
206 * BB Should we keep oplock break and add flush to exceptions?
207 */
208 case SMB2_TREE_DISCONNECT:
209 case SMB2_CANCEL:
210 case SMB2_CLOSE:
211 case SMB2_OPLOCK_BREAK:
212 return -EAGAIN;
213 }
214
215 wait_event_interruptible_timeout(server->response_q,
216 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
217
218 /* are we still trying to reconnect? */
219 if (server->tcpStatus != CifsNeedReconnect)
220 break;
221
222 /*
223 * on "soft" mounts we wait once. Hard mounts keep
224 * retrying until process is killed or server comes
225 * back on-line
226 */
227 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500228 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400229 return -EHOSTDOWN;
230 }
231 }
232
233 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
234 return rc;
235
236 nls_codepage = load_nls_default();
237
238 /*
239 * need to prevent multiple threads trying to simultaneously reconnect
240 * the same SMB session
241 */
242 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200243
244 /*
245 * Recheck after acquire mutex. If another thread is negotiating
246 * and the server never sends an answer the socket will be closed
247 * and tcpStatus set to reconnect.
248 */
249 if (server->tcpStatus == CifsNeedReconnect) {
250 rc = -EHOSTDOWN;
251 mutex_unlock(&tcon->ses->session_mutex);
252 goto out;
253 }
254
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400255 rc = cifs_negotiate_protocol(0, tcon->ses);
256 if (!rc && tcon->ses->need_reconnect)
257 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
258
259 if (rc || !tcon->need_reconnect) {
260 mutex_unlock(&tcon->ses->session_mutex);
261 goto out;
262 }
263
264 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800265 if (tcon->use_persistent)
266 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500267
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400268 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
269 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500270
Joe Perchesf96637b2013-05-04 22:12:25 -0500271 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500272 if (rc) {
273 /* If sess reconnected but tcon didn't, something strange ... */
274 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400275 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500276 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800277
278 if (smb2_command != SMB2_INTERNAL_CMD)
279 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
280
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400281 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400282out:
283 /*
284 * Check if handle based operation so we know whether we can continue
285 * or not without returning to caller to reset file handle.
286 */
287 /*
288 * BB Is flush done by server on drop of tcp session? Should we special
289 * case it and skip above?
290 */
291 switch (smb2_command) {
292 case SMB2_FLUSH:
293 case SMB2_READ:
294 case SMB2_WRITE:
295 case SMB2_LOCK:
296 case SMB2_IOCTL:
297 case SMB2_QUERY_DIRECTORY:
298 case SMB2_CHANGE_NOTIFY:
299 case SMB2_QUERY_INFO:
300 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800301 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400302 }
303 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400304 return rc;
305}
306
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700307static void
308fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
309 unsigned int *total_len)
310{
311 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
312 /* lookup word count ie StructureSize from table */
313 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
314
315 /*
316 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
317 * largest operations (Create)
318 */
319 memset(buf, 0, 256);
320
321 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
322 spdu->StructureSize2 = cpu_to_le16(parmsize);
323
324 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
325}
326
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100327/*
328 * Allocate and return pointer to an SMB request hdr, and set basic
329 * SMB information in the SMB header. If the return code is zero, this
330 * function must have filled in request_buf pointer.
331 */
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800332static int
333smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
334 void **request_buf, unsigned int *total_len)
335{
336 int rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800337
338 rc = smb2_reconnect(smb2_command, tcon);
339 if (rc)
340 return rc;
341
342 /* BB eventually switch this to SMB2 specific small buf size */
343 *request_buf = cifs_small_buf_get();
344 if (*request_buf == NULL) {
345 /* BB should we add a retry in here if not a writepage? */
346 return -ENOMEM;
347 }
348
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100349 fill_small_buf(smb2_command, tcon,
350 (struct smb2_sync_hdr *)(*request_buf),
351 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400352
353 if (tcon != NULL) {
354#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400355 uint16_t com_code = le16_to_cpu(smb2_command);
356 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400357#endif
358 cifs_stats_inc(&tcon->num_smbs_sent);
359 }
360
361 return rc;
362}
363
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500364#ifdef CONFIG_CIFS_SMB311
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100365/* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
366#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500367
368
369#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
370#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
Steve Frenchfcef0db2018-05-19 20:45:27 -0500371#define SMB2_POSIX_EXTENSIONS_AVAILABLE cpu_to_le16(0x100)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500372
373static void
374build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
375{
376 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
377 pneg_ctxt->DataLength = cpu_to_le16(38);
378 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
379 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
380 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
381 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
382}
383
384static void
385build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
386{
387 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French23657ad2018-04-22 15:14:58 -0500388 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
389 pneg_ctxt->CipherCount = cpu_to_le16(1);
390/* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
391 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500392}
393
394static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500395build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
396{
397 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
398 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
399}
400
401static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100402assemble_neg_contexts(struct smb2_negotiate_req *req,
403 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500404{
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100405 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500406 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500407
Steve Frenchfcef0db2018-05-19 20:45:27 -0500408 *total_len += 2; /* Add 2 due to round to 8 byte boundary for 1st ctxt */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500409 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500410 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
411 *total_len += ctxt_len;
412 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100413
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500414 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500415 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
416 *total_len += ctxt_len;
417 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100418
Steve Frenchfcef0db2018-05-19 20:45:27 -0500419 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
420 *total_len += sizeof(struct smb2_posix_neg_context);
421
422 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
423 req->NegotiateContextCount = cpu_to_le16(3);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500424}
Steve French5100d8a2018-04-09 10:47:14 -0500425
426static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
427{
428 unsigned int len = le16_to_cpu(ctxt->DataLength);
429
430 /* If invalid preauth context warn but use what we requested, SHA-512 */
431 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
432 printk_once(KERN_WARNING "server sent bad preauth context\n");
433 return;
434 }
435 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
436 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
437 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
438 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
439}
440
441static int decode_encrypt_ctx(struct TCP_Server_Info *server,
442 struct smb2_encryption_neg_context *ctxt)
443{
444 unsigned int len = le16_to_cpu(ctxt->DataLength);
445
446 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
447 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
448 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
449 return -EINVAL;
450 }
451
452 if (le16_to_cpu(ctxt->CipherCount) != 1) {
453 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
454 return -EINVAL;
455 }
456 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
457 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
458 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
459 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
460 return -EINVAL;
461 }
462 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500463 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500464 return 0;
465}
466
467static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000468 struct TCP_Server_Info *server,
469 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500470{
471 struct smb2_neg_context *pctx;
472 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
473 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500474 unsigned int len_of_ctxts, i;
475 int rc = 0;
476
477 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
478 if (len_of_smb <= offset) {
479 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
480 return -EINVAL;
481 }
482
483 len_of_ctxts = len_of_smb - offset;
484
485 for (i = 0; i < ctxt_cnt; i++) {
486 int clen;
487 /* check that offset is not beyond end of SMB */
488 if (len_of_ctxts == 0)
489 break;
490
491 if (len_of_ctxts < sizeof(struct smb2_neg_context))
492 break;
493
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +1000494 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500495 clen = le16_to_cpu(pctx->DataLength);
496 if (clen > len_of_ctxts)
497 break;
498
499 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
500 decode_preauth_context(
501 (struct smb2_preauth_neg_context *)pctx);
502 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
503 rc = decode_encrypt_ctx(server,
504 (struct smb2_encryption_neg_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500505 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
506 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500507 else
508 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
509 le16_to_cpu(pctx->ContextType));
510
511 if (rc)
512 break;
513 /* offsets must be 8 byte aligned */
514 clen = (clen + 7) & ~0x7;
515 offset += clen + sizeof(struct smb2_neg_context);
516 len_of_ctxts -= clen;
517 }
518 return rc;
519}
520
Steve Frenchce558b02018-05-31 19:16:54 -0500521static struct create_posix *
522create_posix_buf(umode_t mode)
523{
524 struct create_posix *buf;
525
526 buf = kzalloc(sizeof(struct create_posix),
527 GFP_KERNEL);
528 if (!buf)
529 return NULL;
530
531 buf->ccontext.DataOffset =
532 cpu_to_le16(offsetof(struct create_posix, Mode));
533 buf->ccontext.DataLength = cpu_to_le32(4);
534 buf->ccontext.NameOffset =
535 cpu_to_le16(offsetof(struct create_posix, Name));
536 buf->ccontext.NameLength = cpu_to_le16(16);
537
538 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
539 buf->Name[0] = 0x93;
540 buf->Name[1] = 0xAD;
541 buf->Name[2] = 0x25;
542 buf->Name[3] = 0x50;
543 buf->Name[4] = 0x9C;
544 buf->Name[5] = 0xB4;
545 buf->Name[6] = 0x11;
546 buf->Name[7] = 0xE7;
547 buf->Name[8] = 0xB4;
548 buf->Name[9] = 0x23;
549 buf->Name[10] = 0x83;
550 buf->Name[11] = 0xDE;
551 buf->Name[12] = 0x96;
552 buf->Name[13] = 0x8B;
553 buf->Name[14] = 0xCD;
554 buf->Name[15] = 0x7C;
555 buf->Mode = cpu_to_le32(mode);
556 cifs_dbg(FYI, "mode on posix create 0%o", mode);
557 return buf;
558}
559
560static int
561add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
562{
563 struct smb2_create_req *req = iov[0].iov_base;
564 unsigned int num = *num_iovec;
565
566 iov[num].iov_base = create_posix_buf(mode);
567 if (iov[num].iov_base == NULL)
568 return -ENOMEM;
569 iov[num].iov_len = sizeof(struct create_posix);
570 if (!req->CreateContextsOffset)
571 req->CreateContextsOffset = cpu_to_le32(
572 sizeof(struct smb2_create_req) +
573 iov[num - 1].iov_len);
574 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
575 *num_iovec = num + 1;
576 return 0;
577}
578
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500579#else
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100580static void assemble_neg_contexts(struct smb2_negotiate_req *req,
581 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500582{
583 return;
584}
585#endif /* SMB311 */
586
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400587/*
588 *
589 * SMB2 Worker functions follow:
590 *
591 * The general structure of the worker functions is:
592 * 1) Call smb2_init (assembles SMB2 header)
593 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
594 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
595 * 4) Decode SMB2 command specific fields in the fixed length area
596 * 5) Decode variable length data area (if any for this SMB2 command type)
597 * 6) Call free smb buffer
598 * 7) return
599 *
600 */
601
602int
603SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
604{
605 struct smb2_negotiate_req *req;
606 struct smb2_negotiate_rsp *rsp;
607 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700608 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400609 int rc = 0;
610 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400611 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400612 int blob_offset, blob_length;
613 char *security_blob;
614 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100615 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400616
Joe Perchesf96637b2013-05-04 22:12:25 -0500617 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400618
Jeff Layton3534b852013-05-24 07:41:01 -0400619 if (!server) {
620 WARN(1, "%s: server is NULL!\n", __func__);
621 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400622 }
623
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100624 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400625 if (rc)
626 return rc;
627
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100628 req->sync_hdr.SessionId = 0;
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100629#ifdef CONFIG_CIFS_SMB311
630 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
631 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
632#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400633
Steve French9764c022017-09-17 10:41:35 -0500634 if (strcmp(ses->server->vals->version_string,
635 SMB3ANY_VERSION_STRING) == 0) {
636 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
637 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
638 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100639 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500640 } else if (strcmp(ses->server->vals->version_string,
641 SMBDEFAULT_VERSION_STRING) == 0) {
642 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
643 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
644 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
645 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100646 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500647 } else {
648 /* otherwise send specific dialect */
649 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
650 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100651 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500652 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400653
654 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400655 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500656 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400657 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500658 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400659 else
660 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400661
Steve Frenche4aa25e2012-10-01 12:26:22 -0500662 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400663
Steve French3c5f9be12014-05-13 13:37:45 -0700664 /* ClientGUID must be zero for SMB2.02 dialect */
665 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
666 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500667 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700668 memcpy(req->ClientGUID, server->client_guid,
669 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500670 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100671 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500672 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400673 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100674 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400675
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100676 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700677 cifs_small_buf_release(req);
678 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400679 /*
680 * No tcon so can't do
681 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
682 */
Steve French7e682f72017-08-31 21:34:24 -0500683 if (rc == -EOPNOTSUPP) {
684 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500685 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500686 " older servers\n");
687 goto neg_exit;
688 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400689 goto neg_exit;
690
Steve French9764c022017-09-17 10:41:35 -0500691 if (strcmp(ses->server->vals->version_string,
692 SMB3ANY_VERSION_STRING) == 0) {
693 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
694 cifs_dbg(VFS,
695 "SMB2 dialect returned but not requested\n");
696 return -EIO;
697 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
698 cifs_dbg(VFS,
699 "SMB2.1 dialect returned but not requested\n");
700 return -EIO;
701 }
702 } else if (strcmp(ses->server->vals->version_string,
703 SMBDEFAULT_VERSION_STRING) == 0) {
704 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
705 cifs_dbg(VFS,
706 "SMB2 dialect returned but not requested\n");
707 return -EIO;
708 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
709 /* ops set to 3.0 by default for default so update */
710 ses->server->ops = &smb21_operations;
711 }
Steve French590d08d2017-09-19 11:43:47 -0500712 } else if (le16_to_cpu(rsp->DialectRevision) !=
713 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500714 /* if requested single dialect ensure returned dialect matched */
715 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500716 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500717 return -EIO;
718 }
719
Joe Perchesf96637b2013-05-04 22:12:25 -0500720 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400721
Steve Frenche4aa25e2012-10-01 12:26:22 -0500722 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500723 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500724 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500725 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500726 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500727 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500728 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
729 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600730#ifdef CONFIG_CIFS_SMB311
731 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
732 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
733#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400734 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500735 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500736 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400737 rc = -EIO;
738 goto neg_exit;
739 }
740 server->dialect = le16_to_cpu(rsp->DialectRevision);
741
Steve French9764c022017-09-17 10:41:35 -0500742 /* BB: add check that dialect was valid given dialect(s) we asked for */
743
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100744#ifdef CONFIG_CIFS_SMB311
745 /*
746 * Keep a copy of the hash after negprot. This hash will be
747 * the starting hash value for all sessions made from this
748 * server.
749 */
750 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
751 SMB2_PREAUTH_HASH_SIZE);
752#endif
Jeff Laytone598d1d82013-05-26 07:00:59 -0400753 /* SMB2 only has an extended negflavor */
754 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400755 /* set it to the maximum buffer size value we can send with 1 credit */
756 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
757 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400758 server->max_read = le32_to_cpu(rsp->MaxReadSize);
759 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400760 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500761 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
762 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
763 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400764 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400765 /* Internal types */
766 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400767
768 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000769 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500770 /*
771 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
772 * for us will be
773 * ses->sectype = RawNTLMSSP;
774 * but for time being this is our only auth choice so doesn't matter.
775 * We just found a server which sets blob length to zero expecting raw.
776 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700777 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500778 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700779 server->sec_ntlmssp = true;
780 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700781
Jeff Layton38d77c52013-05-26 07:01:00 -0400782 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400783 if (rc)
784 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500785 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500786 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500787 if (rc == 1)
788 rc = 0;
789 else if (rc == 0)
790 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400791 }
Steve French5100d8a2018-04-09 10:47:14 -0500792
793#ifdef CONFIG_CIFS_SMB311
794 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
795 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000796 rc = smb311_decode_neg_context(rsp, server,
797 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -0500798 else
799 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
800 }
801#endif /* CONFIG_CIFS_SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400802neg_exit:
803 free_rsp_buf(resp_buftype, rsp);
804 return rc;
805}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400806
Steve Frenchff1c0382013-11-19 23:44:46 -0600807int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
808{
Long Li2796d302018-04-25 11:30:04 -0700809 int rc;
810 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200811 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600812 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500813 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600814
815 cifs_dbg(FYI, "validate negotiate\n");
816
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100817 /* In SMB3.11 preauth integrity supersedes validate negotiate */
818 if (tcon->ses->server->dialect == SMB311_PROT_ID)
819 return 0;
820
Steve Frenchff1c0382013-11-19 23:44:46 -0600821 /*
822 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500823 * can not sign it (ie are not known user). Even if signing is not
824 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600825 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500826 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600827 */
Steve French0603c962017-09-20 19:57:18 -0500828 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600829 return 0; /* validation requires signing */
830
Steve French0603c962017-09-20 19:57:18 -0500831 if (tcon->ses->user_name == NULL) {
832 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
833 return 0; /* validation requires signing */
834 }
835
836 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
837 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
838
Long Li2796d302018-04-25 11:30:04 -0700839 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
840 if (!pneg_inbuf)
841 return -ENOMEM;
842
843 pneg_inbuf->Capabilities =
Steve Frenchff1c0382013-11-19 23:44:46 -0600844 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Long Li2796d302018-04-25 11:30:04 -0700845 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100846 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600847
848 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -0700849 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600850 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
851 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -0700852 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600853 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
854 else
Long Li2796d302018-04-25 11:30:04 -0700855 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600856
Steve French9764c022017-09-17 10:41:35 -0500857
858 if (strcmp(tcon->ses->server->vals->version_string,
859 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700860 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
861 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
862 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -0500863 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -0700864 inbuflen = sizeof(*pneg_inbuf) -
865 sizeof(pneg_inbuf->Dialects[0]);
Steve French9764c022017-09-17 10:41:35 -0500866 } else if (strcmp(tcon->ses->server->vals->version_string,
867 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700868 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
869 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
870 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
871 pneg_inbuf->DialectCount = cpu_to_le16(3);
Steve French9764c022017-09-17 10:41:35 -0500872 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -0700873 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -0500874 } else {
875 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -0700876 pneg_inbuf->Dialects[0] =
Steve French9764c022017-09-17 10:41:35 -0500877 cpu_to_le16(tcon->ses->server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -0700878 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -0500879 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -0700880 inbuflen = sizeof(*pneg_inbuf) -
881 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -0500882 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600883
884 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
885 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Long Li2796d302018-04-25 11:30:04 -0700886 (char *)pneg_inbuf, inbuflen, (char **)&pneg_rsp, &rsplen);
Steve Frenchff1c0382013-11-19 23:44:46 -0600887
888 if (rc != 0) {
889 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -0700890 rc = -EIO;
891 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -0600892 }
893
Long Li2796d302018-04-25 11:30:04 -0700894 rc = -EIO;
895 if (rsplen != sizeof(*pneg_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500896 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
897 rsplen);
898
899 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -0700900 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
901 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -0600902 }
903
904 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +0100905 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600906 goto vneg_out;
907
908 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
909 goto vneg_out;
910
911 /* do not validate server guid because not saved at negprot time yet */
912
913 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
914 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
915 goto vneg_out;
916
917 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -0700918 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600919 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -0700920 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -0600921
922vneg_out:
923 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -0700924out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200925 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -0700926out_free_inbuf:
927 kfree(pneg_inbuf);
928 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -0600929}
930
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530931enum securityEnum
932smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
933{
934 switch (requested) {
935 case Kerberos:
936 case RawNTLMSSP:
937 return requested;
938 case NTLMv2:
939 return RawNTLMSSP;
940 case Unspecified:
941 if (server->sec_ntlmssp &&
942 (global_secflags & CIFSSEC_MAY_NTLMSSP))
943 return RawNTLMSSP;
944 if ((server->sec_kerberos || server->sec_mskerberos) &&
945 (global_secflags & CIFSSEC_MAY_KRB5))
946 return Kerberos;
947 /* Fallthrough */
948 default:
949 return Unspecified;
950 }
951}
952
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100953struct SMB2_sess_data {
954 unsigned int xid;
955 struct cifs_ses *ses;
956 struct nls_table *nls_cp;
957 void (*func)(struct SMB2_sess_data *);
958 int result;
959 u64 previous_session;
960
961 /* we will send the SMB in three pieces:
962 * a fixed length beginning part, an optional
963 * SPNEGO blob (which can be zero length), and a
964 * last part which will include the strings
965 * and rest of bcc area. This allows us to avoid
966 * a large buffer 17K allocation
967 */
968 int buf0_type;
969 struct kvec iov[2];
970};
971
972static int
973SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
974{
975 int rc;
976 struct cifs_ses *ses = sess_data->ses;
977 struct smb2_sess_setup_req *req;
978 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100979 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100980
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100981 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
982 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100983 if (rc)
984 return rc;
985
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700986 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100987 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100988
989 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
990 req->PreviousSessionId = sess_data->previous_session;
991
992 req->Flags = 0; /* MBZ */
Steve Frenchd4090142018-06-13 17:05:58 -0500993
994 /* enough to enable echos and oplocks and one max size write */
995 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100996
997 /* only one of SMB2 signing flags may be set in SMB2 request */
998 if (server->sign)
999 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1000 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1001 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1002 else
1003 req->SecurityMode = 0;
1004
1005 req->Capabilities = 0;
1006 req->Channel = 0; /* MBZ */
1007
1008 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001009 /* 1 for pad */
1010 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001011 /*
1012 * This variable will be used to clear the buffer
1013 * allocated above in case of any error in the calling function.
1014 */
1015 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1016
1017 return 0;
1018}
1019
1020static void
1021SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1022{
1023 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1024 sess_data->buf0_type = CIFS_NO_BUFFER;
1025}
1026
1027static int
1028SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1029{
1030 int rc;
1031 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001032 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001033
1034 /* Testing shows that buffer offset must be at location of Buffer[0] */
1035 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001036 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001037 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1038
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001039 /* BB add code to build os and lm fields */
1040
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001041 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
1042 sess_data->iov, 2,
1043 &sess_data->buf0_type,
1044 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001045 cifs_small_buf_release(sess_data->iov[0].iov_base);
1046 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001047
1048 return rc;
1049}
1050
1051static int
1052SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1053{
1054 int rc = 0;
1055 struct cifs_ses *ses = sess_data->ses;
1056
1057 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001058 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001059 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001060 if (rc) {
1061 cifs_dbg(FYI,
1062 "SMB3 session key generation failed\n");
1063 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001064 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001065 }
1066 }
1067 if (!ses->server->session_estab) {
1068 ses->server->sequence_number = 0x2;
1069 ses->server->session_estab = true;
1070 }
1071 mutex_unlock(&ses->server->srv_mutex);
1072
1073 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1074 spin_lock(&GlobalMid_Lock);
1075 ses->status = CifsGood;
1076 ses->need_reconnect = false;
1077 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001078 return rc;
1079}
1080
1081#ifdef CONFIG_CIFS_UPCALL
1082static void
1083SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1084{
1085 int rc;
1086 struct cifs_ses *ses = sess_data->ses;
1087 struct cifs_spnego_msg *msg;
1088 struct key *spnego_key = NULL;
1089 struct smb2_sess_setup_rsp *rsp = NULL;
1090
1091 rc = SMB2_sess_alloc_buffer(sess_data);
1092 if (rc)
1093 goto out;
1094
1095 spnego_key = cifs_get_spnego_key(ses);
1096 if (IS_ERR(spnego_key)) {
1097 rc = PTR_ERR(spnego_key);
1098 spnego_key = NULL;
1099 goto out;
1100 }
1101
1102 msg = spnego_key->payload.data[0];
1103 /*
1104 * check version field to make sure that cifs.upcall is
1105 * sending us a response in an expected form
1106 */
1107 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1108 cifs_dbg(VFS,
1109 "bad cifs.upcall version. Expected %d got %d",
1110 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1111 rc = -EKEYREJECTED;
1112 goto out_put_spnego_key;
1113 }
1114
1115 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1116 GFP_KERNEL);
1117 if (!ses->auth_key.response) {
1118 cifs_dbg(VFS,
1119 "Kerberos can't allocate (%u bytes) memory",
1120 msg->sesskey_len);
1121 rc = -ENOMEM;
1122 goto out_put_spnego_key;
1123 }
1124 ses->auth_key.len = msg->sesskey_len;
1125
1126 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1127 sess_data->iov[1].iov_len = msg->secblob_len;
1128
1129 rc = SMB2_sess_sendreceive(sess_data);
1130 if (rc)
1131 goto out_put_spnego_key;
1132
1133 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001134 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001135
1136 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001137
1138 rc = SMB2_sess_establish_session(sess_data);
1139out_put_spnego_key:
1140 key_invalidate(spnego_key);
1141 key_put(spnego_key);
1142out:
1143 sess_data->result = rc;
1144 sess_data->func = NULL;
1145 SMB2_sess_free_buffer(sess_data);
1146}
1147#else
1148static void
1149SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1150{
1151 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1152 sess_data->result = -EOPNOTSUPP;
1153 sess_data->func = NULL;
1154}
1155#endif
1156
Sachin Prabhu166cea42016-10-07 19:11:22 +01001157static void
1158SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1159
1160static void
1161SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1162{
1163 int rc;
1164 struct cifs_ses *ses = sess_data->ses;
1165 struct smb2_sess_setup_rsp *rsp = NULL;
1166 char *ntlmssp_blob = NULL;
1167 bool use_spnego = false; /* else use raw ntlmssp */
1168 u16 blob_length = 0;
1169
1170 /*
1171 * If memory allocation is successful, caller of this function
1172 * frees it.
1173 */
1174 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1175 if (!ses->ntlmssp) {
1176 rc = -ENOMEM;
1177 goto out_err;
1178 }
1179 ses->ntlmssp->sesskey_per_smbsess = true;
1180
1181 rc = SMB2_sess_alloc_buffer(sess_data);
1182 if (rc)
1183 goto out_err;
1184
1185 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1186 GFP_KERNEL);
1187 if (ntlmssp_blob == NULL) {
1188 rc = -ENOMEM;
1189 goto out;
1190 }
1191
1192 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1193 if (use_spnego) {
1194 /* BB eventually need to add this */
1195 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1196 rc = -EOPNOTSUPP;
1197 goto out;
1198 } else {
1199 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1200 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1201 }
1202 sess_data->iov[1].iov_base = ntlmssp_blob;
1203 sess_data->iov[1].iov_len = blob_length;
1204
1205 rc = SMB2_sess_sendreceive(sess_data);
1206 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1207
1208 /* If true, rc here is expected and not an error */
1209 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001210 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001211 rc = 0;
1212
1213 if (rc)
1214 goto out;
1215
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10001216 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001217 le16_to_cpu(rsp->SecurityBufferOffset)) {
1218 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1219 le16_to_cpu(rsp->SecurityBufferOffset));
1220 rc = -EIO;
1221 goto out;
1222 }
1223 rc = decode_ntlmssp_challenge(rsp->Buffer,
1224 le16_to_cpu(rsp->SecurityBufferLength), ses);
1225 if (rc)
1226 goto out;
1227
1228 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1229
1230
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001231 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001232 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001233
1234out:
1235 kfree(ntlmssp_blob);
1236 SMB2_sess_free_buffer(sess_data);
1237 if (!rc) {
1238 sess_data->result = 0;
1239 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1240 return;
1241 }
1242out_err:
1243 kfree(ses->ntlmssp);
1244 ses->ntlmssp = NULL;
1245 sess_data->result = rc;
1246 sess_data->func = NULL;
1247}
1248
1249static void
1250SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1251{
1252 int rc;
1253 struct cifs_ses *ses = sess_data->ses;
1254 struct smb2_sess_setup_req *req;
1255 struct smb2_sess_setup_rsp *rsp = NULL;
1256 unsigned char *ntlmssp_blob = NULL;
1257 bool use_spnego = false; /* else use raw ntlmssp */
1258 u16 blob_length = 0;
1259
1260 rc = SMB2_sess_alloc_buffer(sess_data);
1261 if (rc)
1262 goto out;
1263
1264 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001265 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001266
1267 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1268 sess_data->nls_cp);
1269 if (rc) {
1270 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1271 goto out;
1272 }
1273
1274 if (use_spnego) {
1275 /* BB eventually need to add this */
1276 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1277 rc = -EOPNOTSUPP;
1278 goto out;
1279 }
1280 sess_data->iov[1].iov_base = ntlmssp_blob;
1281 sess_data->iov[1].iov_len = blob_length;
1282
1283 rc = SMB2_sess_sendreceive(sess_data);
1284 if (rc)
1285 goto out;
1286
1287 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1288
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001289 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001290 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001291
1292 rc = SMB2_sess_establish_session(sess_data);
1293out:
1294 kfree(ntlmssp_blob);
1295 SMB2_sess_free_buffer(sess_data);
1296 kfree(ses->ntlmssp);
1297 ses->ntlmssp = NULL;
1298 sess_data->result = rc;
1299 sess_data->func = NULL;
1300}
1301
1302static int
1303SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1304{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301305 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001306
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301307 type = smb2_select_sectype(ses->server, ses->sectype);
1308 cifs_dbg(FYI, "sess setup type %d\n", type);
1309 if (type == Unspecified) {
1310 cifs_dbg(VFS,
1311 "Unable to select appropriate authentication method!");
1312 return -EINVAL;
1313 }
1314
1315 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001316 case Kerberos:
1317 sess_data->func = SMB2_auth_kerberos;
1318 break;
1319 case RawNTLMSSP:
1320 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1321 break;
1322 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301323 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001324 return -EOPNOTSUPP;
1325 }
1326
1327 return 0;
1328}
1329
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001330int
1331SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1332 const struct nls_table *nls_cp)
1333{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001334 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001335 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001336 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001337
Joe Perchesf96637b2013-05-04 22:12:25 -05001338 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001339
Jeff Layton3534b852013-05-24 07:41:01 -04001340 if (!server) {
1341 WARN(1, "%s: server is NULL!\n", __func__);
1342 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001343 }
1344
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001345 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1346 if (!sess_data)
1347 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001348
1349 rc = SMB2_select_sec(ses, sess_data);
1350 if (rc)
1351 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001352 sess_data->xid = xid;
1353 sess_data->ses = ses;
1354 sess_data->buf0_type = CIFS_NO_BUFFER;
1355 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001356 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001357
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001358#ifdef CONFIG_CIFS_SMB311
1359 /*
1360 * Initialize the session hash with the server one.
1361 */
1362 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1363 SMB2_PREAUTH_HASH_SIZE);
1364#endif
1365
Sachin Prabhu166cea42016-10-07 19:11:22 +01001366 while (sess_data->func)
1367 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001368
Steve Frenchc721c382017-09-19 18:40:03 -05001369 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1370 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001371 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001372out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001373 kfree(sess_data);
1374 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001375}
1376
1377int
1378SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1379{
1380 struct smb2_logoff_req *req; /* response is also trivial struct */
1381 int rc = 0;
1382 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001383 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001384 unsigned int total_len;
1385 struct kvec iov[1];
1386 struct kvec rsp_iov;
1387 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001388
Joe Perchesf96637b2013-05-04 22:12:25 -05001389 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001390
1391 if (ses && (ses->server))
1392 server = ses->server;
1393 else
1394 return -EIO;
1395
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001396 /* no need to send SMB logoff if uid already closed due to reconnect */
1397 if (ses->need_reconnect)
1398 goto smb2_session_already_dead;
1399
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001400 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001401 if (rc)
1402 return rc;
1403
1404 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001405 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001406
1407 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1408 flags |= CIFS_TRANSFORM_REQ;
1409 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001410 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001411
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001412 flags |= CIFS_NO_RESP;
1413
1414 iov[0].iov_base = (char *)req;
1415 iov[0].iov_len = total_len;
1416
1417 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001418 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001419 /*
1420 * No tcon so can't do
1421 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1422 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001423
1424smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001425 return rc;
1426}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001427
1428static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1429{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001430 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001431}
1432
1433#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1434
Steve Frenchde9f68d2013-11-15 11:26:24 -06001435/* These are similar values to what Windows uses */
1436static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1437{
1438 tcon->max_chunks = 256;
1439 tcon->max_bytes_chunk = 1048576;
1440 tcon->max_bytes_copy = 16777216;
1441}
1442
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001443int
1444SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1445 struct cifs_tcon *tcon, const struct nls_table *cp)
1446{
1447 struct smb2_tree_connect_req *req;
1448 struct smb2_tree_connect_rsp *rsp = NULL;
1449 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001450 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001451 int rc = 0;
1452 int resp_buftype;
1453 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001454 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001455 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001456 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001457
Joe Perchesf96637b2013-05-04 22:12:25 -05001458 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001459
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001460 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001461 return -EIO;
1462
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001463 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1464 if (unc_path == NULL)
1465 return -ENOMEM;
1466
1467 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1468 unc_path_len *= 2;
1469 if (unc_path_len < 2) {
1470 kfree(unc_path);
1471 return -EINVAL;
1472 }
1473
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001474 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001475 tcon->tid = 0;
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001476
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001477 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1478 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001479 if (rc) {
1480 kfree(unc_path);
1481 return rc;
1482 }
1483
Steve French5a77e752018-05-09 17:43:08 -05001484 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001485 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001486
1487 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001488 /* 1 for pad */
1489 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001490
1491 /* Testing shows that buffer offset must be at location of Buffer[0] */
1492 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001493 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001494 req->PathLength = cpu_to_le16(unc_path_len - 2);
1495 iov[1].iov_base = unc_path;
1496 iov[1].iov_len = unc_path_len;
1497
Steve French6188f282018-03-13 02:29:36 -05001498 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1499 if ((ses->server->dialect == SMB311_PROT_ID) &&
Steve French5a77e752018-05-09 17:43:08 -05001500 !smb3_encryption_required(tcon))
Steve French6188f282018-03-13 02:29:36 -05001501 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1502
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001503 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001504 cifs_small_buf_release(req);
1505 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001506
1507 if (rc != 0) {
1508 if (tcon) {
1509 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1510 tcon->need_reconnect = true;
1511 }
1512 goto tcon_error_exit;
1513 }
1514
Christophe JAILLETcd123002017-05-12 17:59:32 +02001515 switch (rsp->ShareType) {
1516 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001517 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001518 break;
1519 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001520 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001521 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001522 break;
1523 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001524 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001525 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001526 break;
1527 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001528 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001529 rc = -EOPNOTSUPP;
1530 goto tcon_error_exit;
1531 }
1532
1533 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001534 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001535 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1536 tcon->tidStatus = CifsGood;
1537 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001538 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001539 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001540
1541 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1542 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001543 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001544
1545 if (tcon->seal &&
1546 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1547 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1548
Steve Frenchde9f68d2013-11-15 11:26:24 -06001549 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001550 if (tcon->ses->server->ops->validate_negotiate)
1551 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001552tcon_exit:
1553 free_rsp_buf(resp_buftype, rsp);
1554 kfree(unc_path);
1555 return rc;
1556
1557tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001558 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001559 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001560 }
1561 goto tcon_exit;
1562}
1563
1564int
1565SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1566{
1567 struct smb2_tree_disconnect_req *req; /* response is trivial */
1568 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001569 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001570 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001571 unsigned int total_len;
1572 struct kvec iov[1];
1573 struct kvec rsp_iov;
1574 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001575
Joe Perchesf96637b2013-05-04 22:12:25 -05001576 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001577
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001578 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001579 return -EIO;
1580
1581 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1582 return 0;
1583
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001584 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1585 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001586 if (rc)
1587 return rc;
1588
Steve French5a77e752018-05-09 17:43:08 -05001589 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001590 flags |= CIFS_TRANSFORM_REQ;
1591
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001592 flags |= CIFS_NO_RESP;
1593
1594 iov[0].iov_base = (char *)req;
1595 iov[0].iov_len = total_len;
1596
1597 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001598 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001599 if (rc)
1600 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1601
1602 return rc;
1603}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001604
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001605
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001606static struct create_durable *
1607create_durable_buf(void)
1608{
1609 struct create_durable *buf;
1610
1611 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1612 if (!buf)
1613 return NULL;
1614
1615 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001616 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001617 buf->ccontext.DataLength = cpu_to_le32(16);
1618 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1619 (struct create_durable, Name));
1620 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001621 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001622 buf->Name[0] = 'D';
1623 buf->Name[1] = 'H';
1624 buf->Name[2] = 'n';
1625 buf->Name[3] = 'Q';
1626 return buf;
1627}
1628
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001629static struct create_durable *
1630create_reconnect_durable_buf(struct cifs_fid *fid)
1631{
1632 struct create_durable *buf;
1633
1634 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1635 if (!buf)
1636 return NULL;
1637
1638 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1639 (struct create_durable, Data));
1640 buf->ccontext.DataLength = cpu_to_le32(16);
1641 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1642 (struct create_durable, Name));
1643 buf->ccontext.NameLength = cpu_to_le16(4);
1644 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1645 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001646 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001647 buf->Name[0] = 'D';
1648 buf->Name[1] = 'H';
1649 buf->Name[2] = 'n';
1650 buf->Name[3] = 'C';
1651 return buf;
1652}
1653
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001654static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001655parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001656 unsigned int *epoch, char *lease_key)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001657{
1658 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001659 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001660 unsigned int next;
1661 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001662 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001663
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10001664 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001665 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001666 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001667 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001668 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001669 if (le16_to_cpu(cc->NameLength) == 4 &&
1670 strncmp(name, "RqLs", 4) == 0)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001671 return server->ops->parse_lease_buf(cc, epoch,
1672 lease_key);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001673
1674 next = le32_to_cpu(cc->Next);
1675 if (!next)
1676 break;
1677 remaining -= next;
1678 cc = (struct create_context *)((char *)cc + next);
1679 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001680
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001681 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001682}
1683
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001684static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001685add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1686 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001687{
1688 struct smb2_create_req *req = iov[0].iov_base;
1689 unsigned int num = *num_iovec;
1690
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001691 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001692 if (iov[num].iov_base == NULL)
1693 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001694 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001695 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1696 if (!req->CreateContextsOffset)
1697 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001698 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001699 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001700 le32_add_cpu(&req->CreateContextsLength,
1701 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001702 *num_iovec = num + 1;
1703 return 0;
1704}
1705
Steve Frenchb56eae42015-11-03 09:26:27 -06001706static struct create_durable_v2 *
1707create_durable_v2_buf(struct cifs_fid *pfid)
1708{
1709 struct create_durable_v2 *buf;
1710
1711 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1712 if (!buf)
1713 return NULL;
1714
1715 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1716 (struct create_durable_v2, dcontext));
1717 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1718 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1719 (struct create_durable_v2, Name));
1720 buf->ccontext.NameLength = cpu_to_le16(4);
1721
1722 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1723 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001724 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001725 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1726
1727 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1728 buf->Name[0] = 'D';
1729 buf->Name[1] = 'H';
1730 buf->Name[2] = '2';
1731 buf->Name[3] = 'Q';
1732 return buf;
1733}
1734
1735static struct create_durable_handle_reconnect_v2 *
1736create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1737{
1738 struct create_durable_handle_reconnect_v2 *buf;
1739
1740 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1741 GFP_KERNEL);
1742 if (!buf)
1743 return NULL;
1744
1745 buf->ccontext.DataOffset =
1746 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1747 dcontext));
1748 buf->ccontext.DataLength =
1749 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1750 buf->ccontext.NameOffset =
1751 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1752 Name));
1753 buf->ccontext.NameLength = cpu_to_le16(4);
1754
1755 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1756 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1757 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1758 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1759
1760 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1761 buf->Name[0] = 'D';
1762 buf->Name[1] = 'H';
1763 buf->Name[2] = '2';
1764 buf->Name[3] = 'C';
1765 return buf;
1766}
1767
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001768static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001769add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001770 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001771{
1772 struct smb2_create_req *req = iov[0].iov_base;
1773 unsigned int num = *num_iovec;
1774
Steve Frenchb56eae42015-11-03 09:26:27 -06001775 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1776 if (iov[num].iov_base == NULL)
1777 return -ENOMEM;
1778 iov[num].iov_len = sizeof(struct create_durable_v2);
1779 if (!req->CreateContextsOffset)
1780 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001781 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001782 iov[1].iov_len);
1783 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001784 *num_iovec = num + 1;
1785 return 0;
1786}
1787
1788static int
1789add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1790 struct cifs_open_parms *oparms)
1791{
1792 struct smb2_create_req *req = iov[0].iov_base;
1793 unsigned int num = *num_iovec;
1794
1795 /* indicate that we don't need to relock the file */
1796 oparms->reconnect = false;
1797
1798 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1799 if (iov[num].iov_base == NULL)
1800 return -ENOMEM;
1801 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1802 if (!req->CreateContextsOffset)
1803 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001804 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001805 iov[1].iov_len);
1806 le32_add_cpu(&req->CreateContextsLength,
1807 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001808 *num_iovec = num + 1;
1809 return 0;
1810}
1811
1812static int
1813add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1814 struct cifs_open_parms *oparms, bool use_persistent)
1815{
1816 struct smb2_create_req *req = iov[0].iov_base;
1817 unsigned int num = *num_iovec;
1818
1819 if (use_persistent) {
1820 if (oparms->reconnect)
1821 return add_durable_reconnect_v2_context(iov, num_iovec,
1822 oparms);
1823 else
1824 return add_durable_v2_context(iov, num_iovec, oparms);
1825 }
1826
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001827 if (oparms->reconnect) {
1828 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1829 /* indicate that we don't need to relock the file */
1830 oparms->reconnect = false;
1831 } else
1832 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001833 if (iov[num].iov_base == NULL)
1834 return -ENOMEM;
1835 iov[num].iov_len = sizeof(struct create_durable);
1836 if (!req->CreateContextsOffset)
1837 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001838 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001839 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001840 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001841 *num_iovec = num + 1;
1842 return 0;
1843}
1844
Aurelien Aptelf0712922017-02-22 14:47:17 +01001845static int
1846alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1847 const char *treename, const __le16 *path)
1848{
1849 int treename_len, path_len;
1850 struct nls_table *cp;
1851 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1852
1853 /*
1854 * skip leading "\\"
1855 */
1856 treename_len = strlen(treename);
1857 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1858 return -EINVAL;
1859
1860 treename += 2;
1861 treename_len -= 2;
1862
1863 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1864
1865 /*
1866 * make room for one path separator between the treename and
1867 * path
1868 */
1869 *out_len = treename_len + 1 + path_len;
1870
1871 /*
1872 * final path needs to be null-terminated UTF16 with a
1873 * size aligned to 8
1874 */
1875
1876 *out_size = roundup((*out_len+1)*2, 8);
1877 *out_path = kzalloc(*out_size, GFP_KERNEL);
1878 if (!*out_path)
1879 return -ENOMEM;
1880
1881 cp = load_nls_default();
1882 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1883 UniStrcat(*out_path, sep);
1884 UniStrcat(*out_path, path);
1885 unload_nls(cp);
1886
1887 return 0;
1888}
1889
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001890int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001891SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001892 __u8 *oplock, struct smb2_file_all_info *buf,
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001893 struct kvec *err_iov, int *buftype)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001894{
1895 struct smb2_create_req *req;
1896 struct smb2_create_rsp *rsp;
1897 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001898 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001899 struct cifs_ses *ses = tcon->ses;
Steve Frenchce558b02018-05-31 19:16:54 -05001900 struct kvec iov[5]; /* make sure at least one for each open context */
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001901 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001902 int resp_buftype;
1903 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001904 __le16 *copy_path = NULL;
1905 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001906 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001907 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001908 __u32 file_attributes = 0;
Steve Frenchce558b02018-05-31 19:16:54 -05001909 char *dhc_buf = NULL, *lc_buf = NULL, *pc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001910 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001911 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001912
Joe Perchesf96637b2013-05-04 22:12:25 -05001913 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001914
1915 if (ses && (ses->server))
1916 server = ses->server;
1917 else
1918 return -EIO;
1919
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001920 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1921
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001922 if (rc)
1923 return rc;
1924
Steve French5a77e752018-05-09 17:43:08 -05001925 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001926 flags |= CIFS_TRANSFORM_REQ;
1927
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001928 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001929 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001930 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1931 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001932
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001933 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001934 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001935 /* File attributes ignored on open (used in create though) */
1936 req->FileAttributes = cpu_to_le32(file_attributes);
1937 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001938 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1939 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001940
1941 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001942 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001943 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001944
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001945 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01001946
1947 /* [MS-SMB2] 2.2.13 NameOffset:
1948 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1949 * the SMB2 header, the file name includes a prefix that will
1950 * be processed during DFS name normalization as specified in
1951 * section 3.3.5.9. Otherwise, the file name is relative to
1952 * the share that is identified by the TreeId in the SMB2
1953 * header.
1954 */
1955 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1956 int name_len;
1957
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001958 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001959 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1960 &name_len,
1961 tcon->treeName, path);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001962 if (rc) {
1963 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001964 return rc;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001965 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001966 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001967 uni_path_len = copy_size;
1968 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001969 } else {
1970 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1971 /* MUST set path len (NameLength) to 0 opening root of share */
1972 req->NameLength = cpu_to_le16(uni_path_len - 2);
1973 if (uni_path_len % 8 != 0) {
1974 copy_size = roundup(uni_path_len, 8);
1975 copy_path = kzalloc(copy_size, GFP_KERNEL);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001976 if (!copy_path) {
1977 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001978 return -ENOMEM;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001979 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001980 memcpy((char *)copy_path, (const char *)path,
1981 uni_path_len);
1982 uni_path_len = copy_size;
1983 path = copy_path;
1984 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001985 }
1986
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001987 iov[1].iov_len = uni_path_len;
1988 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001989
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001990 if (!server->oplocks)
1991 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1992
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001993 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001994 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1995 req->RequestedOplockLevel = *oplock;
1996 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001997 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001998 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001999 cifs_small_buf_release(req);
2000 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002001 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002002 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002003 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002004 }
2005
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002006 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2007 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002008 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002009 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002010 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002011 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002012 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002013 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002014
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002015 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002016 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002017 if (rc) {
2018 cifs_small_buf_release(req);
2019 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04002020 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002021 return rc;
2022 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002023 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002024 }
2025
Steve Frenchce558b02018-05-31 19:16:54 -05002026#ifdef CONFIG_CIFS_SMB311
2027 if (tcon->posix_extensions) {
2028 if (n_iov > 2) {
2029 struct create_context *ccontext =
2030 (struct create_context *)iov[n_iov-1].iov_base;
2031 ccontext->Next =
2032 cpu_to_le32(iov[n_iov-1].iov_len);
2033 }
2034
2035 rc = add_posix_context(iov, &n_iov, oparms->mode);
2036 if (rc) {
2037 cifs_small_buf_release(req);
2038 kfree(copy_path);
2039 kfree(lc_buf);
2040 kfree(dhc_buf);
2041 return rc;
2042 }
2043 pc_buf = iov[n_iov-1].iov_base;
2044 }
2045#endif /* SMB311 */
2046
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002047 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2048 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002049 cifs_small_buf_release(req);
2050 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002051
2052 if (rc != 0) {
2053 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002054 if (err_iov && rsp) {
2055 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002056 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002057 resp_buftype = CIFS_NO_BUFFER;
2058 rsp = NULL;
2059 }
Steve French28d59362018-05-30 21:42:34 -05002060 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2061 oparms->create_options, oparms->desired_access, rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002062 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002063 } else
2064 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2065 ses->Suid, oparms->create_options,
2066 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002067
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002068 oparms->fid->persistent_fid = rsp->PersistentFileId;
2069 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002070
2071 if (buf) {
2072 memcpy(buf, &rsp->CreationTime, 32);
2073 buf->AllocationSize = rsp->AllocationSize;
2074 buf->EndOfFile = rsp->EndofFile;
2075 buf->Attributes = rsp->FileAttributes;
2076 buf->NumberOfLinks = cpu_to_le32(1);
2077 buf->DeletePending = 0;
2078 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002079
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002080 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002081 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch,
2082 oparms->fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002083 else
2084 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002085creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002086 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04002087 kfree(lc_buf);
2088 kfree(dhc_buf);
Steve Frenchce558b02018-05-31 19:16:54 -05002089 kfree(pc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002090 free_rsp_buf(resp_buftype, rsp);
2091 return rc;
2092}
2093
Steve French4a72daf2013-06-25 00:20:49 -05002094/*
2095 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2096 */
2097int
2098SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002099 u64 volatile_fid, u32 opcode, bool is_fsctl,
Aurelien Aptel51146622017-02-28 15:08:41 +01002100 char *in_data, u32 indatalen,
2101 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05002102{
2103 struct smb2_ioctl_req *req;
2104 struct smb2_ioctl_rsp *rsp;
Steve French8e353102015-03-26 19:47:02 -05002105 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05002106 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002107 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002108 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002109 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002110 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002111 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002112 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05002113
2114 cifs_dbg(FYI, "SMB2 IOCTL\n");
2115
Steve French3d1a3742014-08-11 21:05:25 -05002116 if (out_data != NULL)
2117 *out_data = NULL;
2118
Steve French4a72daf2013-06-25 00:20:49 -05002119 /* zero out returned data len, in case of error */
2120 if (plen)
2121 *plen = 0;
2122
Steve French8e353102015-03-26 19:47:02 -05002123 if (tcon)
2124 ses = tcon->ses;
2125 else
2126 return -EIO;
2127
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002128 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05002129 return -EIO;
2130
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002131 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002132 if (rc)
2133 return rc;
2134
Steve French5a77e752018-05-09 17:43:08 -05002135 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002136 flags |= CIFS_TRANSFORM_REQ;
2137
Steve French4a72daf2013-06-25 00:20:49 -05002138 req->CtlCode = cpu_to_le32(opcode);
2139 req->PersistentFileId = persistent_fid;
2140 req->VolatileFileId = volatile_fid;
2141
2142 if (indatalen) {
2143 req->InputCount = cpu_to_le32(indatalen);
2144 /* do not set InputOffset if no input data */
2145 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002146 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05002147 iov[1].iov_base = in_data;
2148 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002149 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05002150 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002151 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05002152
2153 req->OutputOffset = 0;
2154 req->OutputCount = 0; /* MBZ */
2155
2156 /*
2157 * Could increase MaxOutputResponse, but that would require more
2158 * than one credit. Windows typically sets this smaller, but for some
2159 * ioctls it may be useful to allow server to send more. No point
2160 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05002161 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2162 * (by default, note that it can be overridden to make max larger)
2163 * in responses (except for read responses which can be bigger.
2164 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05002165 */
Steve French7db0a6e2017-05-03 21:12:20 -05002166 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05002167
2168 if (is_fsctl)
2169 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2170 else
2171 req->Flags = 0;
2172
2173 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05002174
Steve French7ff8d452013-10-14 00:44:19 -05002175 /*
2176 * If no input data, the size of ioctl struct in
2177 * protocol spec still includes a 1 byte data buffer,
2178 * but if input data passed to ioctl, we do not
2179 * want to double count this, so we do not send
2180 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002181 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05002182 */
2183
2184 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002185 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05002186 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002187 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05002188
Steve French4587eee2017-10-25 15:58:31 -05002189 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2190 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002191 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002192
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002193 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2194 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002195 cifs_small_buf_release(req);
2196 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002197
Steve Frencheccb4422018-05-17 21:16:55 -05002198 if (rc != 0)
2199 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2200 ses->Suid, 0, opcode, rc);
2201
Steve French9bf0c9c2013-11-16 18:05:28 -06002202 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002203 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002204 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002205 } else if (rc == -EINVAL) {
2206 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2207 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002208 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002209 goto ioctl_exit;
2210 }
Steve French4a72daf2013-06-25 00:20:49 -05002211 }
2212
2213 /* check if caller wants to look at return data or just return rc */
2214 if ((plen == NULL) || (out_data == NULL))
2215 goto ioctl_exit;
2216
2217 *plen = le32_to_cpu(rsp->OutputCount);
2218
2219 /* We check for obvious errors in the output buffer length and offset */
2220 if (*plen == 0)
2221 goto ioctl_exit; /* server returned no data */
2222 else if (*plen > 0xFF00) {
2223 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2224 *plen = 0;
2225 rc = -EIO;
2226 goto ioctl_exit;
2227 }
2228
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002229 if (rsp_iov.iov_len < le32_to_cpu(rsp->OutputOffset) + *plen) {
Steve French4a72daf2013-06-25 00:20:49 -05002230 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2231 le32_to_cpu(rsp->OutputOffset));
2232 *plen = 0;
2233 rc = -EIO;
2234 goto ioctl_exit;
2235 }
2236
2237 *out_data = kmalloc(*plen, GFP_KERNEL);
2238 if (*out_data == NULL) {
2239 rc = -ENOMEM;
2240 goto ioctl_exit;
2241 }
2242
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002243 memcpy(*out_data, (char *)rsp + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002244ioctl_exit:
2245 free_rsp_buf(resp_buftype, rsp);
2246 return rc;
2247}
2248
Steve French64a5cfa2013-10-14 15:31:32 -05002249/*
2250 * Individual callers to ioctl worker function follow
2251 */
2252
2253int
2254SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2255 u64 persistent_fid, u64 volatile_fid)
2256{
2257 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002258 struct compress_ioctl fsctl_input;
2259 char *ret_data = NULL;
2260
2261 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002262 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002263
2264 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2265 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2266 (char *)&fsctl_input /* data input */,
2267 2 /* in data len */, &ret_data /* out data */, NULL);
2268
2269 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002270
2271 return rc;
2272}
2273
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002274int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002275SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2276 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002277{
2278 struct smb2_close_req *req;
2279 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002280 struct cifs_ses *ses = tcon->ses;
2281 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002282 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002283 int resp_buftype;
2284 int rc = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002285 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002286
Joe Perchesf96637b2013-05-04 22:12:25 -05002287 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002288
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002289 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002290 return -EIO;
2291
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002292 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002293 if (rc)
2294 return rc;
2295
Steve French5a77e752018-05-09 17:43:08 -05002296 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002297 flags |= CIFS_TRANSFORM_REQ;
2298
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002299 req->PersistentFileId = persistent_fid;
2300 req->VolatileFileId = volatile_fid;
2301
2302 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002303 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002304
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002305 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002306 cifs_small_buf_release(req);
2307 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002308
2309 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002310 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002311 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2312 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002313 goto close_exit;
2314 }
2315
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002316 /* BB FIXME - decode close response, update inode for caching */
2317
2318close_exit:
2319 free_rsp_buf(resp_buftype, rsp);
2320 return rc;
2321}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002322
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002323int
2324SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2325 u64 persistent_fid, u64 volatile_fid)
2326{
2327 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2328}
2329
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002330static int
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002331validate_iov(unsigned int offset, unsigned int buffer_length,
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002332 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;
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002335 char *end_of_smb = smb_len + (char *)iov->iov_base;
2336 char *begin_of_buf = 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 Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002366validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002367 struct kvec *iov, unsigned int minbufsize,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002368 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002369{
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002370 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002371 int rc;
2372
2373 if (!data)
2374 return -EINVAL;
2375
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002376 rc = validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002377 if (rc)
2378 return rc;
2379
2380 memcpy(data, begin_of_buf, buffer_length);
2381
2382 return 0;
2383}
2384
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002385static int
2386query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002387 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2388 u32 additional_info, size_t output_len, size_t min_len, void **data,
2389 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002390{
2391 struct smb2_query_info_req *req;
2392 struct smb2_query_info_rsp *rsp = NULL;
2393 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002394 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002395 int rc = 0;
2396 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002397 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002398 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002399 unsigned int total_len;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002400
Joe Perchesf96637b2013-05-04 22:12:25 -05002401 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002402
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002403 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002404 return -EIO;
2405
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002406 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2407 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002408 if (rc)
2409 return rc;
2410
Steve French5a77e752018-05-09 17:43:08 -05002411 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002412 flags |= CIFS_TRANSFORM_REQ;
2413
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002414 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002415 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002416 req->PersistentFileId = persistent_fid;
2417 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002418 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002419
2420 /*
2421 * We do not use the input buffer (do not send extra byte)
2422 */
2423 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002424
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002425 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002426
2427 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002428 /* 1 for Buffer */
2429 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002430
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002431 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002432 cifs_small_buf_release(req);
2433 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002434
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002435 if (rc) {
2436 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002437 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2438 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002439 goto qinf_exit;
2440 }
2441
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002442 if (dlen) {
2443 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2444 if (!*data) {
2445 *data = kmalloc(*dlen, GFP_KERNEL);
2446 if (!*data) {
2447 cifs_dbg(VFS,
2448 "Error %d allocating memory for acl\n",
2449 rc);
2450 *dlen = 0;
2451 goto qinf_exit;
2452 }
2453 }
2454 }
2455
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002456 rc = validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002457 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002458 &rsp_iov, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002459
2460qinf_exit:
2461 free_rsp_buf(resp_buftype, rsp);
2462 return rc;
2463}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002464
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002465int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002466 u64 persistent_fid, u64 volatile_fid,
2467 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002468{
2469 return query_info(xid, tcon, persistent_fid, volatile_fid,
2470 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002471 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002472 sizeof(struct smb2_file_full_ea_info),
2473 (void **)&data,
2474 NULL);
2475}
2476
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002477int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2478 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002479{
2480 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002481 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002482 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002483 sizeof(struct smb2_file_all_info), (void **)&data,
2484 NULL);
2485}
2486
2487int
2488SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2489 u64 persistent_fid, u64 volatile_fid,
2490 void **data, u32 *plen)
2491{
2492 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2493 *plen = 0;
2494
2495 return query_info(xid, tcon, persistent_fid, volatile_fid,
2496 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05002497 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002498}
2499
2500int
2501SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2502 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2503{
2504 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002505 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002506 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002507 sizeof(struct smb2_file_internal_info),
2508 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002509}
2510
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002511/*
2512 * This is a no-op for now. We're not really interested in the reply, but
2513 * rather in the fact that the server sent one and that server->lstrp
2514 * gets updated.
2515 *
2516 * FIXME: maybe we should consider checking that the reply matches request?
2517 */
2518static void
2519smb2_echo_callback(struct mid_q_entry *mid)
2520{
2521 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002522 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002523 unsigned int credits_received = 1;
2524
2525 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002526 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002527
2528 DeleteMidQEntry(mid);
2529 add_credits(server, credits_received, CIFS_ECHO_OP);
2530}
2531
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002532void smb2_reconnect_server(struct work_struct *work)
2533{
2534 struct TCP_Server_Info *server = container_of(work,
2535 struct TCP_Server_Info, reconnect.work);
2536 struct cifs_ses *ses;
2537 struct cifs_tcon *tcon, *tcon2;
2538 struct list_head tmp_list;
2539 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002540 int rc;
2541 int resched = false;
2542
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002543
2544 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2545 mutex_lock(&server->reconnect_mutex);
2546
2547 INIT_LIST_HEAD(&tmp_list);
2548 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2549
2550 spin_lock(&cifs_tcp_ses_lock);
2551 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2552 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002553 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002554 tcon->tc_count++;
2555 list_add_tail(&tcon->rlist, &tmp_list);
2556 tcon_exist = true;
2557 }
2558 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002559 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2560 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2561 tcon_exist = true;
2562 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002563 }
2564 /*
2565 * Get the reference to server struct to be sure that the last call of
2566 * cifs_put_tcon() in the loop below won't release the server pointer.
2567 */
2568 if (tcon_exist)
2569 server->srv_count++;
2570
2571 spin_unlock(&cifs_tcp_ses_lock);
2572
2573 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002574 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2575 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002576 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002577 else
2578 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002579 list_del_init(&tcon->rlist);
2580 cifs_put_tcon(tcon);
2581 }
2582
2583 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002584 if (resched)
2585 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002586 mutex_unlock(&server->reconnect_mutex);
2587
2588 /* now we can safely release srv struct */
2589 if (tcon_exist)
2590 cifs_put_tcp_session(server, 1);
2591}
2592
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002593int
2594SMB2_echo(struct TCP_Server_Info *server)
2595{
2596 struct smb2_echo_req *req;
2597 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002598 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002599 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002600 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002601 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002602
Joe Perchesf96637b2013-05-04 22:12:25 -05002603 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002604
Steve French4fcd1812016-06-22 20:12:05 -05002605 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002606 /* No need to send echo on newly established connections */
2607 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2608 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002609 }
2610
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002611 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002612 if (rc)
2613 return rc;
2614
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002615 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002616
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002617 iov[0].iov_len = total_len;
2618 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002619
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002620 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2621 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002622 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002623 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002624
2625 cifs_small_buf_release(req);
2626 return rc;
2627}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002628
2629int
2630SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2631 u64 volatile_fid)
2632{
2633 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002634 struct cifs_ses *ses = tcon->ses;
2635 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002636 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002637 int resp_buftype;
2638 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002639 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002640 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002641
Joe Perchesf96637b2013-05-04 22:12:25 -05002642 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002643
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002644 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002645 return -EIO;
2646
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002647 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002648 if (rc)
2649 return rc;
2650
Steve French5a77e752018-05-09 17:43:08 -05002651 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002652 flags |= CIFS_TRANSFORM_REQ;
2653
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002654 req->PersistentFileId = persistent_fid;
2655 req->VolatileFileId = volatile_fid;
2656
2657 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002658 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002659
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002660 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002661 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002662
Steve Frencheccb4422018-05-17 21:16:55 -05002663 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002664 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002665 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
2666 rc);
2667 }
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002668
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002669 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002670 return rc;
2671}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002672
2673/*
2674 * To form a chain of read requests, any read requests after the first should
2675 * have the end_of_chain boolean set to true.
2676 */
2677static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002678smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002679 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2680 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002681{
2682 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002683 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002684 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002685 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002686
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002687 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2688 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002689 if (rc)
2690 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002691
2692 server = io_parms->tcon->ses->server;
2693 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002694 return -ECONNABORTED;
2695
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002696 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002697 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002698
2699 req->PersistentFileId = io_parms->persistent_fid;
2700 req->VolatileFileId = io_parms->volatile_fid;
2701 req->ReadChannelInfoOffset = 0; /* reserved */
2702 req->ReadChannelInfoLength = 0; /* reserved */
2703 req->Channel = 0; /* reserved */
2704 req->MinimumCount = 0;
2705 req->Length = cpu_to_le32(io_parms->length);
2706 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07002707#ifdef CONFIG_CIFS_SMB_DIRECT
2708 /*
2709 * If we want to do a RDMA write, fill in and append
2710 * smbd_buffer_descriptor_v1 to the end of read request
2711 */
Long Libb4c0412018-04-17 12:17:08 -07002712 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07002713 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002714
Long Libd3dcc62017-11-22 17:38:47 -07002715 struct smbd_buffer_descriptor_v1 *v1;
2716 bool need_invalidate =
2717 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2718
2719 rdata->mr = smbd_register_mr(
2720 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07002721 rdata->nr_pages, rdata->page_offset,
2722 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07002723 if (!rdata->mr)
2724 return -ENOBUFS;
2725
2726 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2727 if (need_invalidate)
2728 req->Channel = SMB2_CHANNEL_RDMA_V1;
2729 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002730 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07002731 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002732 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07002733 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002734 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2735 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2736 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07002737
2738 *total_len += sizeof(*v1) - 1;
2739 }
2740#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002741 if (request_type & CHAINED_REQUEST) {
2742 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002743 /* next 8-byte aligned request */
2744 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2745 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002746 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002747 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002748 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002749 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002750 /*
2751 * Related requests use info from previous read request
2752 * in chain.
2753 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002754 shdr->SessionId = 0xFFFFFFFF;
2755 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002756 req->PersistentFileId = 0xFFFFFFFF;
2757 req->VolatileFileId = 0xFFFFFFFF;
2758 }
2759 }
2760 if (remaining_bytes > io_parms->length)
2761 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2762 else
2763 req->RemainingBytes = 0;
2764
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002765 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002766 return rc;
2767}
2768
2769static void
2770smb2_readv_callback(struct mid_q_entry *mid)
2771{
2772 struct cifs_readdata *rdata = mid->callback_data;
2773 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2774 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002775 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002776 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002777 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002778 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2779 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002780 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07002781 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07002782 .rq_npages = rdata->nr_pages,
2783 .rq_pagesz = rdata->pagesz,
2784 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002785
Joe Perchesf96637b2013-05-04 22:12:25 -05002786 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2787 __func__, mid->mid, mid->mid_state, rdata->result,
2788 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002789
2790 switch (mid->mid_state) {
2791 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002792 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002793 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002794 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002795 int rc;
2796
Jeff Layton0b688cf2012-09-18 16:20:34 -07002797 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002798 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002799 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2800 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002801 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002802 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002803 task_io_account_read(rdata->got_bytes);
2804 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002805 break;
2806 case MID_REQUEST_SUBMITTED:
2807 case MID_RETRY_NEEDED:
2808 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002809 if (server->sign && rdata->got_bytes)
2810 /* reset bytes number since we can not check a sign */
2811 rdata->got_bytes = 0;
2812 /* FIXME: should this be counted toward the initiating task? */
2813 task_io_account_read(rdata->got_bytes);
2814 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002815 break;
2816 default:
2817 if (rdata->result != -ENODATA)
2818 rdata->result = -EIO;
2819 }
Long Libd3dcc62017-11-22 17:38:47 -07002820#ifdef CONFIG_CIFS_SMB_DIRECT
2821 /*
2822 * If this rdata has a memmory registered, the MR can be freed
2823 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2824 * because they have limited number and are used for future I/Os
2825 */
2826 if (rdata->mr) {
2827 smbd_deregister_mr(rdata->mr);
2828 rdata->mr = NULL;
2829 }
2830#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002831 if (rdata->result)
2832 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2833
2834 queue_work(cifsiod_wq, &rdata->work);
2835 DeleteMidQEntry(mid);
2836 add_credits(server, credits_received, 0);
2837}
2838
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002839/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002840int
2841smb2_async_readv(struct cifs_readdata *rdata)
2842{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002843 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002844 char *buf;
2845 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002846 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002847 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002848 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002849 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002850 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002851
Joe Perchesf96637b2013-05-04 22:12:25 -05002852 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2853 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002854
2855 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2856 io_parms.offset = rdata->offset;
2857 io_parms.length = rdata->bytes;
2858 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2859 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2860 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002861
2862 server = io_parms.tcon->ses->server;
2863
Long Li2dabfd52017-11-07 01:54:53 -07002864 rc = smb2_new_read_req(
2865 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002866 if (rc) {
2867 if (rc == -EAGAIN && rdata->credits) {
2868 /* credits was reset by reconnect */
2869 rdata->credits = 0;
2870 /* reduce in_flight value since we won't send the req */
2871 spin_lock(&server->req_lock);
2872 server->in_flight--;
2873 spin_unlock(&server->req_lock);
2874 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002875 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002876 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002877
Steve French5a77e752018-05-09 17:43:08 -05002878 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002879 flags |= CIFS_TRANSFORM_REQ;
2880
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002881 rdata->iov[0].iov_base = buf;
2882 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002883
2884 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002885
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002886 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002887 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002888 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002889 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002890 spin_lock(&server->req_lock);
2891 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002892 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002893 spin_unlock(&server->req_lock);
2894 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002895 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002896 }
2897
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002898 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002899 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002900 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002901 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002902 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002903 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002904 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002905 trace_smb3_read_err(rc, 0 /* xid */, io_parms.persistent_fid,
2906 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
2907 io_parms.offset, io_parms.length);
2908 } else
2909 trace_smb3_read_done(0 /* xid */, io_parms.persistent_fid,
2910 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
2911 io_parms.offset, io_parms.length);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002912
2913 cifs_small_buf_release(buf);
2914 return rc;
2915}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002916
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002917int
2918SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2919 unsigned int *nbytes, char **buf, int *buf_type)
2920{
2921 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002922 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002923 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002924 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002925 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002926 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002927 int flags = CIFS_LOG_ERROR;
2928 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002929
2930 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07002931 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002932 if (rc)
2933 return rc;
2934
Steve French5a77e752018-05-09 17:43:08 -05002935 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002936 flags |= CIFS_TRANSFORM_REQ;
2937
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002938 iov[0].iov_base = (char *)req;
2939 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002940
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002941 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002942 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002943
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002944 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002945
2946 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002947 if (rc != -ENODATA) {
2948 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2949 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002950 }
Steve Frencheccb4422018-05-17 21:16:55 -05002951 trace_smb3_read_err(rc, xid, req->PersistentFileId,
2952 io_parms->tcon->tid, ses->Suid,
2953 io_parms->offset, io_parms->length);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002954 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2955 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05002956 } else
2957 trace_smb3_read_done(xid, req->PersistentFileId,
2958 io_parms->tcon->tid, ses->Suid,
2959 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002960
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002961 *nbytes = le32_to_cpu(rsp->DataLength);
2962 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2963 (*nbytes > io_parms->length)) {
2964 cifs_dbg(FYI, "bad length %d for count %d\n",
2965 *nbytes, io_parms->length);
2966 rc = -EIO;
2967 *nbytes = 0;
2968 }
2969
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002970 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002971 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002972 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002973 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002974 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002975 if (resp_buftype == CIFS_SMALL_BUFFER)
2976 *buf_type = CIFS_SMALL_BUFFER;
2977 else if (resp_buftype == CIFS_LARGE_BUFFER)
2978 *buf_type = CIFS_LARGE_BUFFER;
2979 }
2980 return rc;
2981}
2982
Pavel Shilovsky33319142012-09-18 16:20:29 -07002983/*
2984 * Check the mid_state and signature on received buffer (if any), and queue the
2985 * workqueue completion task.
2986 */
2987static void
2988smb2_writev_callback(struct mid_q_entry *mid)
2989{
2990 struct cifs_writedata *wdata = mid->callback_data;
2991 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2992 unsigned int written;
2993 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2994 unsigned int credits_received = 1;
2995
2996 switch (mid->mid_state) {
2997 case MID_RESPONSE_RECEIVED:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002998 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002999 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
3000 if (wdata->result != 0)
3001 break;
3002
3003 written = le32_to_cpu(rsp->DataLength);
3004 /*
3005 * Mask off high 16 bits when bytes written as returned
3006 * by the server is greater than bytes requested by the
3007 * client. OS/2 servers are known to set incorrect
3008 * CountHigh values.
3009 */
3010 if (written > wdata->bytes)
3011 written &= 0xFFFF;
3012
3013 if (written < wdata->bytes)
3014 wdata->result = -ENOSPC;
3015 else
3016 wdata->bytes = written;
3017 break;
3018 case MID_REQUEST_SUBMITTED:
3019 case MID_RETRY_NEEDED:
3020 wdata->result = -EAGAIN;
3021 break;
3022 default:
3023 wdata->result = -EIO;
3024 break;
3025 }
Long Lidb223a52017-11-22 17:38:45 -07003026#ifdef CONFIG_CIFS_SMB_DIRECT
3027 /*
3028 * If this wdata has a memory registered, the MR can be freed
3029 * The number of MRs available is limited, it's important to recover
3030 * used MR as soon as I/O is finished. Hold MR longer in the later
3031 * I/O process can possibly result in I/O deadlock due to lack of MR
3032 * to send request on I/O retry
3033 */
3034 if (wdata->mr) {
3035 smbd_deregister_mr(wdata->mr);
3036 wdata->mr = NULL;
3037 }
3038#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003039 if (wdata->result)
3040 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3041
3042 queue_work(cifsiod_wq, &wdata->work);
3043 DeleteMidQEntry(mid);
3044 add_credits(tcon->ses->server, credits_received, 0);
3045}
3046
3047/* smb2_async_writev - send an async write, and set up mid to handle result */
3048int
Steve French4a5c80d2014-02-07 20:45:12 -06003049smb2_async_writev(struct cifs_writedata *wdata,
3050 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07003051{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003052 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003053 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003054 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003055 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003056 struct TCP_Server_Info *server = tcon->ses->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003057 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003058 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003059 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003060
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003061 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003062 if (rc) {
3063 if (rc == -EAGAIN && wdata->credits) {
3064 /* credits was reset by reconnect */
3065 wdata->credits = 0;
3066 /* reduce in_flight value since we won't send the req */
3067 spin_lock(&server->req_lock);
3068 server->in_flight--;
3069 spin_unlock(&server->req_lock);
3070 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003071 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003072 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003073
Steve French5a77e752018-05-09 17:43:08 -05003074 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003075 flags |= CIFS_TRANSFORM_REQ;
3076
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003077 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003078 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003079
3080 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3081 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3082 req->WriteChannelInfoOffset = 0;
3083 req->WriteChannelInfoLength = 0;
3084 req->Channel = 0;
3085 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003086 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003087 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003088 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07003089#ifdef CONFIG_CIFS_SMB_DIRECT
3090 /*
3091 * If we want to do a server RDMA read, fill in and append
3092 * smbd_buffer_descriptor_v1 to the end of write request
3093 */
Long Libb4c0412018-04-17 12:17:08 -07003094 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003095 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003096
Long Lidb223a52017-11-22 17:38:45 -07003097 struct smbd_buffer_descriptor_v1 *v1;
3098 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3099
3100 wdata->mr = smbd_register_mr(
3101 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003102 wdata->nr_pages, wdata->page_offset,
3103 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07003104 if (!wdata->mr) {
3105 rc = -ENOBUFS;
3106 goto async_writev_out;
3107 }
3108 req->Length = 0;
3109 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07003110 if (wdata->nr_pages > 1)
3111 req->RemainingBytes =
3112 cpu_to_le32(
3113 (wdata->nr_pages - 1) * wdata->pagesz -
3114 wdata->page_offset + wdata->tailsz
3115 );
3116 else
3117 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003118 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3119 if (need_invalidate)
3120 req->Channel = SMB2_CHANNEL_RDMA_V1;
3121 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003122 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003123 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003124 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003125 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003126 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3127 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3128 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003129 }
3130#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003131 iov[0].iov_len = total_len - 1;
3132 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003133
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003134 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003135 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003136 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07003137 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003138 rqst.rq_npages = wdata->nr_pages;
3139 rqst.rq_pagesz = wdata->pagesz;
3140 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003141#ifdef CONFIG_CIFS_SMB_DIRECT
3142 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003143 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07003144 rqst.rq_npages = 0;
3145 }
3146#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003147 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3148 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003149
Long Lidb223a52017-11-22 17:38:45 -07003150#ifdef CONFIG_CIFS_SMB_DIRECT
3151 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3152 if (!wdata->mr)
3153 req->Length = cpu_to_le32(wdata->bytes);
3154#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003155 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003156#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003157
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003158 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003159 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003160 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003161 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003162 spin_lock(&server->req_lock);
3163 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003164 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003165 spin_unlock(&server->req_lock);
3166 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003167 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003168 }
3169
Pavel Shilovsky33319142012-09-18 16:20:29 -07003170 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003171 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3172 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003173
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003174 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003175 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3176 tcon->tid, tcon->ses->Suid, wdata->offset,
3177 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06003178 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003179 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003180 } else
3181 trace_smb3_write_done(0 /* no xid */, req->PersistentFileId,
3182 tcon->tid, tcon->ses->Suid, wdata->offset,
3183 wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003184
Pavel Shilovsky33319142012-09-18 16:20:29 -07003185async_writev_out:
3186 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003187 return rc;
3188}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003189
3190/*
3191 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3192 * The length field from io_parms must be at least 1 and indicates a number of
3193 * elements with data to write that begins with position 1 in iov array. All
3194 * data length is specified by count.
3195 */
3196int
3197SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3198 unsigned int *nbytes, struct kvec *iov, int n_vec)
3199{
3200 int rc = 0;
3201 struct smb2_write_req *req = NULL;
3202 struct smb2_write_rsp *rsp = NULL;
3203 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003204 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003205 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003206 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003207
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003208 *nbytes = 0;
3209
3210 if (n_vec < 1)
3211 return rc;
3212
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003213 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3214 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003215 if (rc)
3216 return rc;
3217
3218 if (io_parms->tcon->ses->server == NULL)
3219 return -ECONNABORTED;
3220
Steve French5a77e752018-05-09 17:43:08 -05003221 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003222 flags |= CIFS_TRANSFORM_REQ;
3223
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003224 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003225
3226 req->PersistentFileId = io_parms->persistent_fid;
3227 req->VolatileFileId = io_parms->volatile_fid;
3228 req->WriteChannelInfoOffset = 0;
3229 req->WriteChannelInfoLength = 0;
3230 req->Channel = 0;
3231 req->Length = cpu_to_le32(io_parms->length);
3232 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003233 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003234 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003235 req->RemainingBytes = 0;
3236
3237 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003238 /* 1 for Buffer */
3239 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003240
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003241 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
3242 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003243 cifs_small_buf_release(req);
3244 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003245
3246 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003247 trace_smb3_write_err(xid, req->PersistentFileId,
3248 io_parms->tcon->tid,
3249 io_parms->tcon->ses->Suid,
3250 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003251 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003252 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05003253 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003254 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05003255 trace_smb3_write_done(xid, req->PersistentFileId,
3256 io_parms->tcon->tid,
3257 io_parms->tcon->ses->Suid,
3258 io_parms->offset, *nbytes);
3259 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003260
3261 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003262 return rc;
3263}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003264
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003265static unsigned int
3266num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3267{
3268 int len;
3269 unsigned int entrycount = 0;
3270 unsigned int next_offset = 0;
3271 FILE_DIRECTORY_INFO *entryptr;
3272
3273 if (bufstart == NULL)
3274 return 0;
3275
3276 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3277
3278 while (1) {
3279 entryptr = (FILE_DIRECTORY_INFO *)
3280 ((char *)entryptr + next_offset);
3281
3282 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003283 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003284 break;
3285 }
3286
3287 len = le32_to_cpu(entryptr->FileNameLength);
3288 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003289 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3290 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003291 break;
3292 }
3293
3294 *lastentry = (char *)entryptr;
3295 entrycount++;
3296
3297 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3298 if (!next_offset)
3299 break;
3300 }
3301
3302 return entrycount;
3303}
3304
3305/*
3306 * Readdir/FindFirst
3307 */
3308int
3309SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3310 u64 persistent_fid, u64 volatile_fid, int index,
3311 struct cifs_search_info *srch_inf)
3312{
3313 struct smb2_query_directory_req *req;
3314 struct smb2_query_directory_rsp *rsp = NULL;
3315 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003316 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003317 int rc = 0;
3318 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003319 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003320 unsigned char *bufptr;
3321 struct TCP_Server_Info *server;
3322 struct cifs_ses *ses = tcon->ses;
3323 __le16 asteriks = cpu_to_le16('*');
3324 char *end_of_smb;
3325 unsigned int output_size = CIFSMaxBufSize;
3326 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003327 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003328 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003329
3330 if (ses && (ses->server))
3331 server = ses->server;
3332 else
3333 return -EIO;
3334
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003335 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3336 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003337 if (rc)
3338 return rc;
3339
Steve French5a77e752018-05-09 17:43:08 -05003340 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003341 flags |= CIFS_TRANSFORM_REQ;
3342
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003343 switch (srch_inf->info_level) {
3344 case SMB_FIND_FILE_DIRECTORY_INFO:
3345 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3346 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3347 break;
3348 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3349 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3350 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3351 break;
3352 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003353 cifs_dbg(VFS, "info level %u isn't supported\n",
3354 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003355 rc = -EINVAL;
3356 goto qdir_exit;
3357 }
3358
3359 req->FileIndex = cpu_to_le32(index);
3360 req->PersistentFileId = persistent_fid;
3361 req->VolatileFileId = volatile_fid;
3362
3363 len = 0x2;
3364 bufptr = req->Buffer;
3365 memcpy(bufptr, &asteriks, len);
3366
3367 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003368 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003369 req->FileNameLength = cpu_to_le16(len);
3370 /*
3371 * BB could be 30 bytes or so longer if we used SMB2 specific
3372 * buffer lengths, but this is safe and close enough.
3373 */
3374 output_size = min_t(unsigned int, output_size, server->maxBuf);
3375 output_size = min_t(unsigned int, output_size, 2 << 15);
3376 req->OutputBufferLength = cpu_to_le32(output_size);
3377
3378 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003379 /* 1 for Buffer */
3380 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003381
3382 iov[1].iov_base = (char *)(req->Buffer);
3383 iov[1].iov_len = len;
3384
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003385 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003386 cifs_small_buf_release(req);
3387 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003388
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003389 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003390 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003391 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003392 srch_inf->endOfSearch = true;
3393 rc = 0;
3394 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003395 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3396 goto qdir_exit;
3397 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003398
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10003399 rc = validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003400 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003401 info_buf_size);
3402 if (rc)
3403 goto qdir_exit;
3404
3405 srch_inf->unicode = true;
3406
3407 if (srch_inf->ntwrk_buf_start) {
3408 if (srch_inf->smallBuf)
3409 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3410 else
3411 cifs_buf_release(srch_inf->ntwrk_buf_start);
3412 }
3413 srch_inf->ntwrk_buf_start = (char *)rsp;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003414 srch_inf->srch_entries_start = srch_inf->last_entry =
3415 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
3416 end_of_smb = rsp_iov.iov_len + (char *)rsp;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003417 srch_inf->entries_in_buffer =
3418 num_entries(srch_inf->srch_entries_start, end_of_smb,
3419 &srch_inf->last_entry, info_buf_size);
3420 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003421 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3422 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3423 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003424 if (resp_buftype == CIFS_LARGE_BUFFER)
3425 srch_inf->smallBuf = false;
3426 else if (resp_buftype == CIFS_SMALL_BUFFER)
3427 srch_inf->smallBuf = true;
3428 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003429 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003430
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003431 return rc;
3432
3433qdir_exit:
3434 free_rsp_buf(resp_buftype, rsp);
3435 return rc;
3436}
3437
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003438static int
3439send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003440 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3441 u8 info_type, u32 additional_info, unsigned int num,
3442 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003443{
3444 struct smb2_set_info_req *req;
3445 struct smb2_set_info_rsp *rsp = NULL;
3446 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003447 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003448 int rc = 0;
3449 int resp_buftype;
3450 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003451 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003452 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003453 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003454
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003455 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003456 return -EIO;
3457
3458 if (!num)
3459 return -EINVAL;
3460
Kees Cook6da2ec52018-06-12 13:55:00 -07003461 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003462 if (!iov)
3463 return -ENOMEM;
3464
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003465 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003466 if (rc) {
3467 kfree(iov);
3468 return rc;
3469 }
3470
Steve French5a77e752018-05-09 17:43:08 -05003471 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003472 flags |= CIFS_TRANSFORM_REQ;
3473
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003474 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003475
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003476 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003477 req->FileInfoClass = info_class;
3478 req->PersistentFileId = persistent_fid;
3479 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003480 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003481
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003482 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003483 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003484 req->BufferLength = cpu_to_le32(*size);
3485
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003486 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003487 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003488
3489 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003490 /* 1 for Buffer */
3491 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003492
3493 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003494 le32_add_cpu(&req->BufferLength, size[i]);
3495 iov[i].iov_base = (char *)data[i];
3496 iov[i].iov_len = size[i];
3497 }
3498
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003499 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3500 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003501 cifs_small_buf_release(req);
3502 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003503
Steve Frencheccb4422018-05-17 21:16:55 -05003504 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003505 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003506 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
3507 ses->Suid, info_class, (__u32)info_type, rc);
3508 }
Steve French7d3fb242013-11-18 09:56:28 -06003509
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003510 free_rsp_buf(resp_buftype, rsp);
3511 kfree(iov);
3512 return rc;
3513}
3514
3515int
3516SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3517 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3518{
3519 struct smb2_file_rename_info info;
3520 void **data;
3521 unsigned int size[2];
3522 int rc;
3523 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3524
Kees Cook6da2ec52018-06-12 13:55:00 -07003525 data = kmalloc_array(2, sizeof(void *), GFP_KERNEL);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003526 if (!data)
3527 return -ENOMEM;
3528
3529 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3530 /* 0 = fail if target already exists */
3531 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3532 info.FileNameLength = cpu_to_le32(len);
3533
3534 data[0] = &info;
3535 size[0] = sizeof(struct smb2_file_rename_info);
3536
3537 data[1] = target_file;
3538 size[1] = len + 2 /* null */;
3539
3540 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003541 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3542 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003543 kfree(data);
3544 return rc;
3545}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003546
3547int
Steve French897fba12016-05-12 21:20:36 -05003548SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3549 u64 persistent_fid, u64 volatile_fid)
3550{
3551 __u8 delete_pending = 1;
3552 void *data;
3553 unsigned int size;
3554
3555 data = &delete_pending;
3556 size = 1; /* sizeof __u8 */
3557
3558 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003559 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3560 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003561}
3562
3563int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003564SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3565 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3566{
3567 struct smb2_file_link_info info;
3568 void **data;
3569 unsigned int size[2];
3570 int rc;
3571 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3572
Kees Cook6da2ec52018-06-12 13:55:00 -07003573 data = kmalloc_array(2, sizeof(void *), GFP_KERNEL);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003574 if (!data)
3575 return -ENOMEM;
3576
3577 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3578 /* 0 = fail if link already exists */
3579 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3580 info.FileNameLength = cpu_to_le32(len);
3581
3582 data[0] = &info;
3583 size[0] = sizeof(struct smb2_file_link_info);
3584
3585 data[1] = target_file;
3586 size[1] = len + 2 /* null */;
3587
3588 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003589 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3590 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003591 kfree(data);
3592 return rc;
3593}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003594
3595int
3596SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003597 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003598{
3599 struct smb2_file_eof_info info;
3600 void *data;
3601 unsigned int size;
3602
3603 info.EndOfFile = *eof;
3604
3605 data = &info;
3606 size = sizeof(struct smb2_file_eof_info);
3607
Steve Frenchf29ebb42014-07-19 21:44:58 -05003608 if (is_falloc)
3609 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003610 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3611 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003612 else
3613 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003614 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3615 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003616}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003617
3618int
3619SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3620 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3621{
3622 unsigned int size;
3623 size = sizeof(FILE_BASIC_INFO);
3624 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003625 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3626 0, 1, (void **)&buf, &size);
3627}
3628
3629int
3630SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3631 u64 persistent_fid, u64 volatile_fid,
3632 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3633{
3634 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3635 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3636 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003637}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003638
3639int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003640SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3641 u64 persistent_fid, u64 volatile_fid,
3642 struct smb2_file_full_ea_info *buf, int len)
3643{
3644 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3645 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3646 0, 1, (void **)&buf, &len);
3647}
3648
3649int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003650SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3651 const u64 persistent_fid, const u64 volatile_fid,
3652 __u8 oplock_level)
3653{
3654 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10003655 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003656 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003657 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003658 unsigned int total_len;
3659 struct kvec iov[1];
3660 struct kvec rsp_iov;
3661 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003662
Joe Perchesf96637b2013-05-04 22:12:25 -05003663 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003664 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3665 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003666 if (rc)
3667 return rc;
3668
Steve French5a77e752018-05-09 17:43:08 -05003669 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003670 flags |= CIFS_TRANSFORM_REQ;
3671
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003672 req->VolatileFid = volatile_fid;
3673 req->PersistentFid = persistent_fid;
3674 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003675 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003676
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003677 flags |= CIFS_NO_RESP;
3678
3679 iov[0].iov_base = (char *)req;
3680 iov[0].iov_len = total_len;
3681
3682 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003683 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003684
3685 if (rc) {
3686 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003687 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003688 }
3689
3690 return rc;
3691}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003692
3693static void
3694copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3695 struct kstatfs *kst)
3696{
3697 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3698 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3699 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303700 kst->f_bfree = kst->f_bavail =
3701 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003702 return;
3703}
3704
3705static int
3706build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3707 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3708{
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003709 struct TCP_Server_Info *server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003710 int rc;
3711 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003712 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003713
Joe Perchesf96637b2013-05-04 22:12:25 -05003714 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003715
3716 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3717 return -EIO;
3718
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003719 server = tcon->ses->server;
3720
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003721 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3722 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003723 if (rc)
3724 return rc;
3725
3726 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3727 req->FileInfoClass = level;
3728 req->PersistentFileId = persistent_fid;
3729 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003730 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003731 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003732 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003733 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10003734 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003735
3736 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003737 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003738 return 0;
3739}
3740
3741int
3742SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3743 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3744{
3745 struct smb2_query_info_rsp *rsp = NULL;
3746 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003747 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003748 int rc = 0;
3749 int resp_buftype;
3750 struct cifs_ses *ses = tcon->ses;
3751 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003752 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003753
3754 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3755 sizeof(struct smb2_fs_full_size_info),
3756 persistent_fid, volatile_fid);
3757 if (rc)
3758 return rc;
3759
Steve French5a77e752018-05-09 17:43:08 -05003760 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003761 flags |= CIFS_TRANSFORM_REQ;
3762
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003763 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003764 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003765 if (rc) {
3766 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003767 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003768 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003769 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003770
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10003771 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003772 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10003773 rc = validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003774 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003775 sizeof(struct smb2_fs_full_size_info));
3776 if (!rc)
3777 copy_fs_info_to_kstatfs(info, fsdata);
3778
Steve French34f62642013-10-09 02:07:00 -05003779qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003780 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003781 return rc;
3782}
3783
3784int
3785SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003786 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003787{
3788 struct smb2_query_info_rsp *rsp = NULL;
3789 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003790 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003791 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003792 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003793 struct cifs_ses *ses = tcon->ses;
3794 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003795 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003796
Steven French21671142013-10-09 13:36:35 -05003797 if (level == FS_DEVICE_INFORMATION) {
3798 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3799 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3800 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3801 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3802 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003803 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3804 max_len = sizeof(struct smb3_fs_ss_info);
3805 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003806 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003807 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003808 return -EINVAL;
3809 }
3810
3811 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003812 persistent_fid, volatile_fid);
3813 if (rc)
3814 return rc;
3815
Steve French5a77e752018-05-09 17:43:08 -05003816 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003817 flags |= CIFS_TRANSFORM_REQ;
3818
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003819 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003820 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003821 if (rc) {
3822 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3823 goto qfsattr_exit;
3824 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003825 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003826
3827 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3828 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10003829 rc = validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05003830 if (rc)
3831 goto qfsattr_exit;
3832
3833 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10003834 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003835 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003836 rsp_len, max_len));
3837 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10003838 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003839 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003840 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3841 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10003842 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003843 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3844 tcon->perf_sector_size =
3845 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3846 }
Steve French34f62642013-10-09 02:07:00 -05003847
3848qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003849 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003850 return rc;
3851}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003852
3853int
3854smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3855 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3856 const __u32 num_lock, struct smb2_lock_element *buf)
3857{
3858 int rc = 0;
3859 struct smb2_lock_req *req = NULL;
3860 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003861 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003862 int resp_buf_type;
3863 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003864 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003865 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003866
Joe Perchesf96637b2013-05-04 22:12:25 -05003867 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003868
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003869 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003870 if (rc)
3871 return rc;
3872
Steve French5a77e752018-05-09 17:43:08 -05003873 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003874 flags |= CIFS_TRANSFORM_REQ;
3875
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003876 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003877 req->LockCount = cpu_to_le16(num_lock);
3878
3879 req->PersistentFileId = persist_fid;
3880 req->VolatileFileId = volatile_fid;
3881
3882 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003883
3884 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003885 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003886 iov[1].iov_base = (char *)buf;
3887 iov[1].iov_len = count;
3888
3889 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003890 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3891 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003892 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003893 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003894 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003895 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003896 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
3897 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003898 }
3899
3900 return rc;
3901}
3902
3903int
3904SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3905 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3906 const __u64 length, const __u64 offset, const __u32 lock_flags,
3907 const bool wait)
3908{
3909 struct smb2_lock_element lock;
3910
3911 lock.Offset = cpu_to_le64(offset);
3912 lock.Length = cpu_to_le64(length);
3913 lock.Flags = cpu_to_le32(lock_flags);
3914 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3915 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3916
3917 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3918}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003919
3920int
3921SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3922 __u8 *lease_key, const __le32 lease_state)
3923{
3924 int rc;
3925 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003926 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003927 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003928 unsigned int total_len;
3929 struct kvec iov[1];
3930 struct kvec rsp_iov;
3931 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003932
Joe Perchesf96637b2013-05-04 22:12:25 -05003933 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003934 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3935 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003936 if (rc)
3937 return rc;
3938
Steve French5a77e752018-05-09 17:43:08 -05003939 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003940 flags |= CIFS_TRANSFORM_REQ;
3941
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003942 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003943 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003944 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003945
3946 memcpy(req->LeaseKey, lease_key, 16);
3947 req->LeaseState = lease_state;
3948
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003949 flags |= CIFS_NO_RESP;
3950
3951 iov[0].iov_base = (char *)req;
3952 iov[0].iov_len = total_len;
3953
3954 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003955 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003956
3957 if (rc) {
3958 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003959 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003960 }
3961
3962 return rc;
3963}