blob: dbfb83ae613786a62b29137e7e0cbed0ab11bf63 [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 */
Stefano Briviof46ecbd2018-07-05 11:46:42 +0200343 if (smb2_command == SMB2_SET_INFO)
344 *request_buf = cifs_buf_get();
345 else
346 *request_buf = cifs_small_buf_get();
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800347 if (*request_buf == NULL) {
348 /* BB should we add a retry in here if not a writepage? */
349 return -ENOMEM;
350 }
351
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100352 fill_small_buf(smb2_command, tcon,
353 (struct smb2_sync_hdr *)(*request_buf),
354 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400355
356 if (tcon != NULL) {
357#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400358 uint16_t com_code = le16_to_cpu(smb2_command);
359 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400360#endif
361 cifs_stats_inc(&tcon->num_smbs_sent);
362 }
363
364 return rc;
365}
366
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500367#ifdef CONFIG_CIFS_SMB311
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100368/* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
369#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500370
371
372#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
373#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
Steve Frenchfcef0db2018-05-19 20:45:27 -0500374#define SMB2_POSIX_EXTENSIONS_AVAILABLE cpu_to_le16(0x100)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500375
376static void
377build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
378{
379 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
380 pneg_ctxt->DataLength = cpu_to_le16(38);
381 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
382 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
383 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
384 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
385}
386
387static void
388build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
389{
390 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French23657ad2018-04-22 15:14:58 -0500391 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
392 pneg_ctxt->CipherCount = cpu_to_le16(1);
393/* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
394 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500395}
396
397static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500398build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
399{
400 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
401 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
402}
403
404static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100405assemble_neg_contexts(struct smb2_negotiate_req *req,
406 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500407{
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100408 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500409 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500410
Steve Frenchfcef0db2018-05-19 20:45:27 -0500411 *total_len += 2; /* Add 2 due to round to 8 byte boundary for 1st ctxt */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500412 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500413 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
414 *total_len += ctxt_len;
415 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100416
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500417 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500418 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
419 *total_len += ctxt_len;
420 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100421
Steve Frenchfcef0db2018-05-19 20:45:27 -0500422 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
423 *total_len += sizeof(struct smb2_posix_neg_context);
424
425 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
426 req->NegotiateContextCount = cpu_to_le16(3);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500427}
Steve French5100d8a2018-04-09 10:47:14 -0500428
429static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
430{
431 unsigned int len = le16_to_cpu(ctxt->DataLength);
432
433 /* If invalid preauth context warn but use what we requested, SHA-512 */
434 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
435 printk_once(KERN_WARNING "server sent bad preauth context\n");
436 return;
437 }
438 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
439 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
440 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
441 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
442}
443
444static int decode_encrypt_ctx(struct TCP_Server_Info *server,
445 struct smb2_encryption_neg_context *ctxt)
446{
447 unsigned int len = le16_to_cpu(ctxt->DataLength);
448
449 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
450 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
451 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
452 return -EINVAL;
453 }
454
455 if (le16_to_cpu(ctxt->CipherCount) != 1) {
456 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
457 return -EINVAL;
458 }
459 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
460 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
461 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
462 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
463 return -EINVAL;
464 }
465 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500466 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500467 return 0;
468}
469
470static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000471 struct TCP_Server_Info *server,
472 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500473{
474 struct smb2_neg_context *pctx;
475 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
476 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500477 unsigned int len_of_ctxts, i;
478 int rc = 0;
479
480 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
481 if (len_of_smb <= offset) {
482 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
483 return -EINVAL;
484 }
485
486 len_of_ctxts = len_of_smb - offset;
487
488 for (i = 0; i < ctxt_cnt; i++) {
489 int clen;
490 /* check that offset is not beyond end of SMB */
491 if (len_of_ctxts == 0)
492 break;
493
494 if (len_of_ctxts < sizeof(struct smb2_neg_context))
495 break;
496
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +1000497 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500498 clen = le16_to_cpu(pctx->DataLength);
499 if (clen > len_of_ctxts)
500 break;
501
502 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
503 decode_preauth_context(
504 (struct smb2_preauth_neg_context *)pctx);
505 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
506 rc = decode_encrypt_ctx(server,
507 (struct smb2_encryption_neg_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500508 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
509 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500510 else
511 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
512 le16_to_cpu(pctx->ContextType));
513
514 if (rc)
515 break;
516 /* offsets must be 8 byte aligned */
517 clen = (clen + 7) & ~0x7;
518 offset += clen + sizeof(struct smb2_neg_context);
519 len_of_ctxts -= clen;
520 }
521 return rc;
522}
523
Steve Frenchce558b02018-05-31 19:16:54 -0500524static struct create_posix *
525create_posix_buf(umode_t mode)
526{
527 struct create_posix *buf;
528
529 buf = kzalloc(sizeof(struct create_posix),
530 GFP_KERNEL);
531 if (!buf)
532 return NULL;
533
534 buf->ccontext.DataOffset =
535 cpu_to_le16(offsetof(struct create_posix, Mode));
536 buf->ccontext.DataLength = cpu_to_le32(4);
537 buf->ccontext.NameOffset =
538 cpu_to_le16(offsetof(struct create_posix, Name));
539 buf->ccontext.NameLength = cpu_to_le16(16);
540
541 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
542 buf->Name[0] = 0x93;
543 buf->Name[1] = 0xAD;
544 buf->Name[2] = 0x25;
545 buf->Name[3] = 0x50;
546 buf->Name[4] = 0x9C;
547 buf->Name[5] = 0xB4;
548 buf->Name[6] = 0x11;
549 buf->Name[7] = 0xE7;
550 buf->Name[8] = 0xB4;
551 buf->Name[9] = 0x23;
552 buf->Name[10] = 0x83;
553 buf->Name[11] = 0xDE;
554 buf->Name[12] = 0x96;
555 buf->Name[13] = 0x8B;
556 buf->Name[14] = 0xCD;
557 buf->Name[15] = 0x7C;
558 buf->Mode = cpu_to_le32(mode);
559 cifs_dbg(FYI, "mode on posix create 0%o", mode);
560 return buf;
561}
562
563static int
564add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
565{
566 struct smb2_create_req *req = iov[0].iov_base;
567 unsigned int num = *num_iovec;
568
569 iov[num].iov_base = create_posix_buf(mode);
570 if (iov[num].iov_base == NULL)
571 return -ENOMEM;
572 iov[num].iov_len = sizeof(struct create_posix);
573 if (!req->CreateContextsOffset)
574 req->CreateContextsOffset = cpu_to_le32(
575 sizeof(struct smb2_create_req) +
576 iov[num - 1].iov_len);
577 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
578 *num_iovec = num + 1;
579 return 0;
580}
581
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500582#else
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100583static void assemble_neg_contexts(struct smb2_negotiate_req *req,
584 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500585{
586 return;
587}
588#endif /* SMB311 */
589
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400590/*
591 *
592 * SMB2 Worker functions follow:
593 *
594 * The general structure of the worker functions is:
595 * 1) Call smb2_init (assembles SMB2 header)
596 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
597 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
598 * 4) Decode SMB2 command specific fields in the fixed length area
599 * 5) Decode variable length data area (if any for this SMB2 command type)
600 * 6) Call free smb buffer
601 * 7) return
602 *
603 */
604
605int
606SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
607{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000608 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400609 struct smb2_negotiate_req *req;
610 struct smb2_negotiate_rsp *rsp;
611 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700612 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400613 int rc = 0;
614 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400615 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400616 int blob_offset, blob_length;
617 char *security_blob;
618 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100619 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400620
Joe Perchesf96637b2013-05-04 22:12:25 -0500621 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400622
Jeff Layton3534b852013-05-24 07:41:01 -0400623 if (!server) {
624 WARN(1, "%s: server is NULL!\n", __func__);
625 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400626 }
627
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100628 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400629 if (rc)
630 return rc;
631
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100632 req->sync_hdr.SessionId = 0;
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100633#ifdef CONFIG_CIFS_SMB311
634 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
635 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
636#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400637
Steve French9764c022017-09-17 10:41:35 -0500638 if (strcmp(ses->server->vals->version_string,
639 SMB3ANY_VERSION_STRING) == 0) {
640 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
641 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
642 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100643 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500644 } else if (strcmp(ses->server->vals->version_string,
645 SMBDEFAULT_VERSION_STRING) == 0) {
646 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
647 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
648 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
649 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100650 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500651 } else {
652 /* otherwise send specific dialect */
653 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
654 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100655 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500656 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400657
658 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400659 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500660 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400661 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500662 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400663 else
664 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400665
Steve Frenche4aa25e2012-10-01 12:26:22 -0500666 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400667
Steve French3c5f9be12014-05-13 13:37:45 -0700668 /* ClientGUID must be zero for SMB2.02 dialect */
669 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
670 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500671 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700672 memcpy(req->ClientGUID, server->client_guid,
673 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500674 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100675 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500676 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400677 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100678 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400679
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000680 memset(&rqst, 0, sizeof(struct smb_rqst));
681 rqst.rq_iov = iov;
682 rqst.rq_nvec = 1;
683
684 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700685 cifs_small_buf_release(req);
686 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400687 /*
688 * No tcon so can't do
689 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
690 */
Steve French7e682f72017-08-31 21:34:24 -0500691 if (rc == -EOPNOTSUPP) {
692 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500693 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500694 " older servers\n");
695 goto neg_exit;
696 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400697 goto neg_exit;
698
Steve French9764c022017-09-17 10:41:35 -0500699 if (strcmp(ses->server->vals->version_string,
700 SMB3ANY_VERSION_STRING) == 0) {
701 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
702 cifs_dbg(VFS,
703 "SMB2 dialect returned but not requested\n");
704 return -EIO;
705 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
706 cifs_dbg(VFS,
707 "SMB2.1 dialect returned but not requested\n");
708 return -EIO;
709 }
710 } else if (strcmp(ses->server->vals->version_string,
711 SMBDEFAULT_VERSION_STRING) == 0) {
712 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
713 cifs_dbg(VFS,
714 "SMB2 dialect returned but not requested\n");
715 return -EIO;
716 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
717 /* ops set to 3.0 by default for default so update */
718 ses->server->ops = &smb21_operations;
719 }
Steve French590d08d2017-09-19 11:43:47 -0500720 } else if (le16_to_cpu(rsp->DialectRevision) !=
721 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500722 /* if requested single dialect ensure returned dialect matched */
723 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500724 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500725 return -EIO;
726 }
727
Joe Perchesf96637b2013-05-04 22:12:25 -0500728 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400729
Steve Frenche4aa25e2012-10-01 12:26:22 -0500730 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500731 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500732 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500733 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500734 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500735 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500736 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
737 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600738#ifdef CONFIG_CIFS_SMB311
739 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
740 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
741#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400742 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500743 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500744 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400745 rc = -EIO;
746 goto neg_exit;
747 }
748 server->dialect = le16_to_cpu(rsp->DialectRevision);
749
Steve French9764c022017-09-17 10:41:35 -0500750 /* BB: add check that dialect was valid given dialect(s) we asked for */
751
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100752#ifdef CONFIG_CIFS_SMB311
753 /*
754 * Keep a copy of the hash after negprot. This hash will be
755 * the starting hash value for all sessions made from this
756 * server.
757 */
758 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
759 SMB2_PREAUTH_HASH_SIZE);
760#endif
Jeff Laytone598d1d82013-05-26 07:00:59 -0400761 /* SMB2 only has an extended negflavor */
762 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400763 /* set it to the maximum buffer size value we can send with 1 credit */
764 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
765 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400766 server->max_read = le32_to_cpu(rsp->MaxReadSize);
767 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400768 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500769 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
770 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
771 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400772 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400773 /* Internal types */
774 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400775
776 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000777 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500778 /*
779 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
780 * for us will be
781 * ses->sectype = RawNTLMSSP;
782 * but for time being this is our only auth choice so doesn't matter.
783 * We just found a server which sets blob length to zero expecting raw.
784 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700785 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500786 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700787 server->sec_ntlmssp = true;
788 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700789
Jeff Layton38d77c52013-05-26 07:01:00 -0400790 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400791 if (rc)
792 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500793 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500794 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500795 if (rc == 1)
796 rc = 0;
797 else if (rc == 0)
798 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400799 }
Steve French5100d8a2018-04-09 10:47:14 -0500800
801#ifdef CONFIG_CIFS_SMB311
802 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
803 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000804 rc = smb311_decode_neg_context(rsp, server,
805 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -0500806 else
807 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
808 }
809#endif /* CONFIG_CIFS_SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400810neg_exit:
811 free_rsp_buf(resp_buftype, rsp);
812 return rc;
813}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400814
Steve Frenchff1c0382013-11-19 23:44:46 -0600815int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
816{
Long Li2796d302018-04-25 11:30:04 -0700817 int rc;
818 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200819 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600820 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500821 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600822
823 cifs_dbg(FYI, "validate negotiate\n");
824
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100825 /* In SMB3.11 preauth integrity supersedes validate negotiate */
826 if (tcon->ses->server->dialect == SMB311_PROT_ID)
827 return 0;
828
Steve Frenchff1c0382013-11-19 23:44:46 -0600829 /*
830 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500831 * can not sign it (ie are not known user). Even if signing is not
832 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600833 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500834 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600835 */
Steve French0603c962017-09-20 19:57:18 -0500836 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600837 return 0; /* validation requires signing */
838
Steve French0603c962017-09-20 19:57:18 -0500839 if (tcon->ses->user_name == NULL) {
840 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
841 return 0; /* validation requires signing */
842 }
843
844 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
845 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
846
Long Li2796d302018-04-25 11:30:04 -0700847 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
848 if (!pneg_inbuf)
849 return -ENOMEM;
850
851 pneg_inbuf->Capabilities =
Steve Frenchff1c0382013-11-19 23:44:46 -0600852 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Long Li2796d302018-04-25 11:30:04 -0700853 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100854 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600855
856 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -0700857 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600858 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
859 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -0700860 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600861 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
862 else
Long Li2796d302018-04-25 11:30:04 -0700863 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600864
Steve French9764c022017-09-17 10:41:35 -0500865
866 if (strcmp(tcon->ses->server->vals->version_string,
867 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700868 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
869 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
870 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -0500871 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -0700872 inbuflen = sizeof(*pneg_inbuf) -
873 sizeof(pneg_inbuf->Dialects[0]);
Steve French9764c022017-09-17 10:41:35 -0500874 } else if (strcmp(tcon->ses->server->vals->version_string,
875 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700876 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
877 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
878 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
879 pneg_inbuf->DialectCount = cpu_to_le16(3);
Steve French9764c022017-09-17 10:41:35 -0500880 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -0700881 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -0500882 } else {
883 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -0700884 pneg_inbuf->Dialects[0] =
Steve French9764c022017-09-17 10:41:35 -0500885 cpu_to_le16(tcon->ses->server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -0700886 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -0500887 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -0700888 inbuflen = sizeof(*pneg_inbuf) -
889 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -0500890 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600891
892 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
893 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Long Li2796d302018-04-25 11:30:04 -0700894 (char *)pneg_inbuf, inbuflen, (char **)&pneg_rsp, &rsplen);
Steve Frenchff1c0382013-11-19 23:44:46 -0600895
896 if (rc != 0) {
897 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -0700898 rc = -EIO;
899 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -0600900 }
901
Long Li2796d302018-04-25 11:30:04 -0700902 rc = -EIO;
903 if (rsplen != sizeof(*pneg_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500904 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
905 rsplen);
906
907 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -0700908 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
909 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -0600910 }
911
912 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +0100913 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600914 goto vneg_out;
915
916 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
917 goto vneg_out;
918
919 /* do not validate server guid because not saved at negprot time yet */
920
921 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
922 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
923 goto vneg_out;
924
925 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -0700926 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600927 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -0700928 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -0600929
930vneg_out:
931 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -0700932out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200933 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -0700934out_free_inbuf:
935 kfree(pneg_inbuf);
936 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -0600937}
938
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530939enum securityEnum
940smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
941{
942 switch (requested) {
943 case Kerberos:
944 case RawNTLMSSP:
945 return requested;
946 case NTLMv2:
947 return RawNTLMSSP;
948 case Unspecified:
949 if (server->sec_ntlmssp &&
950 (global_secflags & CIFSSEC_MAY_NTLMSSP))
951 return RawNTLMSSP;
952 if ((server->sec_kerberos || server->sec_mskerberos) &&
953 (global_secflags & CIFSSEC_MAY_KRB5))
954 return Kerberos;
955 /* Fallthrough */
956 default:
957 return Unspecified;
958 }
959}
960
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100961struct SMB2_sess_data {
962 unsigned int xid;
963 struct cifs_ses *ses;
964 struct nls_table *nls_cp;
965 void (*func)(struct SMB2_sess_data *);
966 int result;
967 u64 previous_session;
968
969 /* we will send the SMB in three pieces:
970 * a fixed length beginning part, an optional
971 * SPNEGO blob (which can be zero length), and a
972 * last part which will include the strings
973 * and rest of bcc area. This allows us to avoid
974 * a large buffer 17K allocation
975 */
976 int buf0_type;
977 struct kvec iov[2];
978};
979
980static int
981SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
982{
983 int rc;
984 struct cifs_ses *ses = sess_data->ses;
985 struct smb2_sess_setup_req *req;
986 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100987 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100988
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100989 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
990 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100991 if (rc)
992 return rc;
993
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700994 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100995 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100996
997 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
998 req->PreviousSessionId = sess_data->previous_session;
999
1000 req->Flags = 0; /* MBZ */
Steve Frenchd4090142018-06-13 17:05:58 -05001001
1002 /* enough to enable echos and oplocks and one max size write */
1003 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001004
1005 /* only one of SMB2 signing flags may be set in SMB2 request */
1006 if (server->sign)
1007 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1008 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1009 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1010 else
1011 req->SecurityMode = 0;
1012
1013 req->Capabilities = 0;
1014 req->Channel = 0; /* MBZ */
1015
1016 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001017 /* 1 for pad */
1018 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001019 /*
1020 * This variable will be used to clear the buffer
1021 * allocated above in case of any error in the calling function.
1022 */
1023 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1024
1025 return 0;
1026}
1027
1028static void
1029SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1030{
1031 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1032 sess_data->buf0_type = CIFS_NO_BUFFER;
1033}
1034
1035static int
1036SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1037{
1038 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001039 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001040 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001041 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001042
1043 /* Testing shows that buffer offset must be at location of Buffer[0] */
1044 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001045 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001046 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1047
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001048 memset(&rqst, 0, sizeof(struct smb_rqst));
1049 rqst.rq_iov = sess_data->iov;
1050 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001051
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001052 /* BB add code to build os and lm fields */
1053 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1054 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001055 &sess_data->buf0_type,
1056 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001057 cifs_small_buf_release(sess_data->iov[0].iov_base);
1058 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001059
1060 return rc;
1061}
1062
1063static int
1064SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1065{
1066 int rc = 0;
1067 struct cifs_ses *ses = sess_data->ses;
1068
1069 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001070 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001071 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001072 if (rc) {
1073 cifs_dbg(FYI,
1074 "SMB3 session key generation failed\n");
1075 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001076 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001077 }
1078 }
1079 if (!ses->server->session_estab) {
1080 ses->server->sequence_number = 0x2;
1081 ses->server->session_estab = true;
1082 }
1083 mutex_unlock(&ses->server->srv_mutex);
1084
1085 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1086 spin_lock(&GlobalMid_Lock);
1087 ses->status = CifsGood;
1088 ses->need_reconnect = false;
1089 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001090 return rc;
1091}
1092
1093#ifdef CONFIG_CIFS_UPCALL
1094static void
1095SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1096{
1097 int rc;
1098 struct cifs_ses *ses = sess_data->ses;
1099 struct cifs_spnego_msg *msg;
1100 struct key *spnego_key = NULL;
1101 struct smb2_sess_setup_rsp *rsp = NULL;
1102
1103 rc = SMB2_sess_alloc_buffer(sess_data);
1104 if (rc)
1105 goto out;
1106
1107 spnego_key = cifs_get_spnego_key(ses);
1108 if (IS_ERR(spnego_key)) {
1109 rc = PTR_ERR(spnego_key);
1110 spnego_key = NULL;
1111 goto out;
1112 }
1113
1114 msg = spnego_key->payload.data[0];
1115 /*
1116 * check version field to make sure that cifs.upcall is
1117 * sending us a response in an expected form
1118 */
1119 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1120 cifs_dbg(VFS,
1121 "bad cifs.upcall version. Expected %d got %d",
1122 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1123 rc = -EKEYREJECTED;
1124 goto out_put_spnego_key;
1125 }
1126
1127 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1128 GFP_KERNEL);
1129 if (!ses->auth_key.response) {
1130 cifs_dbg(VFS,
1131 "Kerberos can't allocate (%u bytes) memory",
1132 msg->sesskey_len);
1133 rc = -ENOMEM;
1134 goto out_put_spnego_key;
1135 }
1136 ses->auth_key.len = msg->sesskey_len;
1137
1138 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1139 sess_data->iov[1].iov_len = msg->secblob_len;
1140
1141 rc = SMB2_sess_sendreceive(sess_data);
1142 if (rc)
1143 goto out_put_spnego_key;
1144
1145 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001146 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001147
1148 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001149
1150 rc = SMB2_sess_establish_session(sess_data);
1151out_put_spnego_key:
1152 key_invalidate(spnego_key);
1153 key_put(spnego_key);
1154out:
1155 sess_data->result = rc;
1156 sess_data->func = NULL;
1157 SMB2_sess_free_buffer(sess_data);
1158}
1159#else
1160static void
1161SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1162{
1163 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1164 sess_data->result = -EOPNOTSUPP;
1165 sess_data->func = NULL;
1166}
1167#endif
1168
Sachin Prabhu166cea42016-10-07 19:11:22 +01001169static void
1170SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1171
1172static void
1173SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1174{
1175 int rc;
1176 struct cifs_ses *ses = sess_data->ses;
1177 struct smb2_sess_setup_rsp *rsp = NULL;
1178 char *ntlmssp_blob = NULL;
1179 bool use_spnego = false; /* else use raw ntlmssp */
1180 u16 blob_length = 0;
1181
1182 /*
1183 * If memory allocation is successful, caller of this function
1184 * frees it.
1185 */
1186 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1187 if (!ses->ntlmssp) {
1188 rc = -ENOMEM;
1189 goto out_err;
1190 }
1191 ses->ntlmssp->sesskey_per_smbsess = true;
1192
1193 rc = SMB2_sess_alloc_buffer(sess_data);
1194 if (rc)
1195 goto out_err;
1196
1197 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1198 GFP_KERNEL);
1199 if (ntlmssp_blob == NULL) {
1200 rc = -ENOMEM;
1201 goto out;
1202 }
1203
1204 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1205 if (use_spnego) {
1206 /* BB eventually need to add this */
1207 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1208 rc = -EOPNOTSUPP;
1209 goto out;
1210 } else {
1211 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1212 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1213 }
1214 sess_data->iov[1].iov_base = ntlmssp_blob;
1215 sess_data->iov[1].iov_len = blob_length;
1216
1217 rc = SMB2_sess_sendreceive(sess_data);
1218 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1219
1220 /* If true, rc here is expected and not an error */
1221 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001222 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001223 rc = 0;
1224
1225 if (rc)
1226 goto out;
1227
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10001228 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001229 le16_to_cpu(rsp->SecurityBufferOffset)) {
1230 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1231 le16_to_cpu(rsp->SecurityBufferOffset));
1232 rc = -EIO;
1233 goto out;
1234 }
1235 rc = decode_ntlmssp_challenge(rsp->Buffer,
1236 le16_to_cpu(rsp->SecurityBufferLength), ses);
1237 if (rc)
1238 goto out;
1239
1240 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1241
1242
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001243 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001244 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001245
1246out:
1247 kfree(ntlmssp_blob);
1248 SMB2_sess_free_buffer(sess_data);
1249 if (!rc) {
1250 sess_data->result = 0;
1251 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1252 return;
1253 }
1254out_err:
1255 kfree(ses->ntlmssp);
1256 ses->ntlmssp = NULL;
1257 sess_data->result = rc;
1258 sess_data->func = NULL;
1259}
1260
1261static void
1262SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1263{
1264 int rc;
1265 struct cifs_ses *ses = sess_data->ses;
1266 struct smb2_sess_setup_req *req;
1267 struct smb2_sess_setup_rsp *rsp = NULL;
1268 unsigned char *ntlmssp_blob = NULL;
1269 bool use_spnego = false; /* else use raw ntlmssp */
1270 u16 blob_length = 0;
1271
1272 rc = SMB2_sess_alloc_buffer(sess_data);
1273 if (rc)
1274 goto out;
1275
1276 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001277 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001278
1279 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1280 sess_data->nls_cp);
1281 if (rc) {
1282 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1283 goto out;
1284 }
1285
1286 if (use_spnego) {
1287 /* BB eventually need to add this */
1288 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1289 rc = -EOPNOTSUPP;
1290 goto out;
1291 }
1292 sess_data->iov[1].iov_base = ntlmssp_blob;
1293 sess_data->iov[1].iov_len = blob_length;
1294
1295 rc = SMB2_sess_sendreceive(sess_data);
1296 if (rc)
1297 goto out;
1298
1299 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1300
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001301 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001302 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001303
1304 rc = SMB2_sess_establish_session(sess_data);
1305out:
1306 kfree(ntlmssp_blob);
1307 SMB2_sess_free_buffer(sess_data);
1308 kfree(ses->ntlmssp);
1309 ses->ntlmssp = NULL;
1310 sess_data->result = rc;
1311 sess_data->func = NULL;
1312}
1313
1314static int
1315SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1316{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301317 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001318
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301319 type = smb2_select_sectype(ses->server, ses->sectype);
1320 cifs_dbg(FYI, "sess setup type %d\n", type);
1321 if (type == Unspecified) {
1322 cifs_dbg(VFS,
1323 "Unable to select appropriate authentication method!");
1324 return -EINVAL;
1325 }
1326
1327 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001328 case Kerberos:
1329 sess_data->func = SMB2_auth_kerberos;
1330 break;
1331 case RawNTLMSSP:
1332 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1333 break;
1334 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301335 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001336 return -EOPNOTSUPP;
1337 }
1338
1339 return 0;
1340}
1341
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001342int
1343SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1344 const struct nls_table *nls_cp)
1345{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001346 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001347 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001348 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001349
Joe Perchesf96637b2013-05-04 22:12:25 -05001350 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001351
Jeff Layton3534b852013-05-24 07:41:01 -04001352 if (!server) {
1353 WARN(1, "%s: server is NULL!\n", __func__);
1354 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001355 }
1356
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001357 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1358 if (!sess_data)
1359 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001360
1361 rc = SMB2_select_sec(ses, sess_data);
1362 if (rc)
1363 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001364 sess_data->xid = xid;
1365 sess_data->ses = ses;
1366 sess_data->buf0_type = CIFS_NO_BUFFER;
1367 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001368 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001369
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001370#ifdef CONFIG_CIFS_SMB311
1371 /*
1372 * Initialize the session hash with the server one.
1373 */
1374 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1375 SMB2_PREAUTH_HASH_SIZE);
1376#endif
1377
Sachin Prabhu166cea42016-10-07 19:11:22 +01001378 while (sess_data->func)
1379 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001380
Steve Frenchc721c382017-09-19 18:40:03 -05001381 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1382 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001383 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001384out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001385 kfree(sess_data);
1386 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001387}
1388
1389int
1390SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1391{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001392 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001393 struct smb2_logoff_req *req; /* response is also trivial struct */
1394 int rc = 0;
1395 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001396 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001397 unsigned int total_len;
1398 struct kvec iov[1];
1399 struct kvec rsp_iov;
1400 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001401
Joe Perchesf96637b2013-05-04 22:12:25 -05001402 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001403
1404 if (ses && (ses->server))
1405 server = ses->server;
1406 else
1407 return -EIO;
1408
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001409 /* no need to send SMB logoff if uid already closed due to reconnect */
1410 if (ses->need_reconnect)
1411 goto smb2_session_already_dead;
1412
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001413 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001414 if (rc)
1415 return rc;
1416
1417 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001418 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001419
1420 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1421 flags |= CIFS_TRANSFORM_REQ;
1422 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001423 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001424
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001425 flags |= CIFS_NO_RESP;
1426
1427 iov[0].iov_base = (char *)req;
1428 iov[0].iov_len = total_len;
1429
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001430 memset(&rqst, 0, sizeof(struct smb_rqst));
1431 rqst.rq_iov = iov;
1432 rqst.rq_nvec = 1;
1433
1434 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001435 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001436 /*
1437 * No tcon so can't do
1438 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1439 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001440
1441smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001442 return rc;
1443}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001444
1445static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1446{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001447 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001448}
1449
1450#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1451
Steve Frenchde9f68d2013-11-15 11:26:24 -06001452/* These are similar values to what Windows uses */
1453static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1454{
1455 tcon->max_chunks = 256;
1456 tcon->max_bytes_chunk = 1048576;
1457 tcon->max_bytes_copy = 16777216;
1458}
1459
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001460int
1461SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1462 struct cifs_tcon *tcon, const struct nls_table *cp)
1463{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001464 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001465 struct smb2_tree_connect_req *req;
1466 struct smb2_tree_connect_rsp *rsp = NULL;
1467 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001468 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001469 int rc = 0;
1470 int resp_buftype;
1471 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001472 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001473 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001474 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001475
Joe Perchesf96637b2013-05-04 22:12:25 -05001476 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001477
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001478 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001479 return -EIO;
1480
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001481 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1482 if (unc_path == NULL)
1483 return -ENOMEM;
1484
1485 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1486 unc_path_len *= 2;
1487 if (unc_path_len < 2) {
1488 kfree(unc_path);
1489 return -EINVAL;
1490 }
1491
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001492 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001493 tcon->tid = 0;
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001494
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001495 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1496 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001497 if (rc) {
1498 kfree(unc_path);
1499 return rc;
1500 }
1501
Steve French5a77e752018-05-09 17:43:08 -05001502 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001503 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001504
1505 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001506 /* 1 for pad */
1507 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001508
1509 /* Testing shows that buffer offset must be at location of Buffer[0] */
1510 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001511 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001512 req->PathLength = cpu_to_le16(unc_path_len - 2);
1513 iov[1].iov_base = unc_path;
1514 iov[1].iov_len = unc_path_len;
1515
Steve French6188f282018-03-13 02:29:36 -05001516 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1517 if ((ses->server->dialect == SMB311_PROT_ID) &&
Steve French5a77e752018-05-09 17:43:08 -05001518 !smb3_encryption_required(tcon))
Steve French6188f282018-03-13 02:29:36 -05001519 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1520
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001521 memset(&rqst, 0, sizeof(struct smb_rqst));
1522 rqst.rq_iov = iov;
1523 rqst.rq_nvec = 2;
1524
1525 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001526 cifs_small_buf_release(req);
1527 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001528
1529 if (rc != 0) {
1530 if (tcon) {
1531 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1532 tcon->need_reconnect = true;
1533 }
1534 goto tcon_error_exit;
1535 }
1536
Christophe JAILLETcd123002017-05-12 17:59:32 +02001537 switch (rsp->ShareType) {
1538 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001539 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001540 break;
1541 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001542 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001543 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001544 break;
1545 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001546 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001547 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001548 break;
1549 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001550 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001551 rc = -EOPNOTSUPP;
1552 goto tcon_error_exit;
1553 }
1554
1555 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001556 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001557 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1558 tcon->tidStatus = CifsGood;
1559 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001560 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001561 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001562
1563 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1564 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001565 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001566
1567 if (tcon->seal &&
1568 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1569 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1570
Steve Frenchde9f68d2013-11-15 11:26:24 -06001571 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001572 if (tcon->ses->server->ops->validate_negotiate)
1573 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001574tcon_exit:
1575 free_rsp_buf(resp_buftype, rsp);
1576 kfree(unc_path);
1577 return rc;
1578
1579tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001580 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001581 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001582 }
1583 goto tcon_exit;
1584}
1585
1586int
1587SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1588{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001589 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001590 struct smb2_tree_disconnect_req *req; /* response is trivial */
1591 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001592 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001593 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001594 unsigned int total_len;
1595 struct kvec iov[1];
1596 struct kvec rsp_iov;
1597 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001598
Joe Perchesf96637b2013-05-04 22:12:25 -05001599 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001600
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001601 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001602 return -EIO;
1603
1604 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1605 return 0;
1606
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001607 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1608 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001609 if (rc)
1610 return rc;
1611
Steve French5a77e752018-05-09 17:43:08 -05001612 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001613 flags |= CIFS_TRANSFORM_REQ;
1614
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001615 flags |= CIFS_NO_RESP;
1616
1617 iov[0].iov_base = (char *)req;
1618 iov[0].iov_len = total_len;
1619
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001620 memset(&rqst, 0, sizeof(struct smb_rqst));
1621 rqst.rq_iov = iov;
1622 rqst.rq_nvec = 1;
1623
1624 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001625 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001626 if (rc)
1627 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1628
1629 return rc;
1630}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001631
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001632
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001633static struct create_durable *
1634create_durable_buf(void)
1635{
1636 struct create_durable *buf;
1637
1638 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1639 if (!buf)
1640 return NULL;
1641
1642 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001643 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001644 buf->ccontext.DataLength = cpu_to_le32(16);
1645 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1646 (struct create_durable, Name));
1647 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001648 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001649 buf->Name[0] = 'D';
1650 buf->Name[1] = 'H';
1651 buf->Name[2] = 'n';
1652 buf->Name[3] = 'Q';
1653 return buf;
1654}
1655
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001656static struct create_durable *
1657create_reconnect_durable_buf(struct cifs_fid *fid)
1658{
1659 struct create_durable *buf;
1660
1661 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1662 if (!buf)
1663 return NULL;
1664
1665 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1666 (struct create_durable, Data));
1667 buf->ccontext.DataLength = cpu_to_le32(16);
1668 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1669 (struct create_durable, Name));
1670 buf->ccontext.NameLength = cpu_to_le16(4);
1671 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1672 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001673 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001674 buf->Name[0] = 'D';
1675 buf->Name[1] = 'H';
1676 buf->Name[2] = 'n';
1677 buf->Name[3] = 'C';
1678 return buf;
1679}
1680
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001681static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001682parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001683 unsigned int *epoch, char *lease_key)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001684{
1685 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001686 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001687 unsigned int next;
1688 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001689 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001690
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10001691 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001692 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001693 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001694 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001695 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001696 if (le16_to_cpu(cc->NameLength) == 4 &&
1697 strncmp(name, "RqLs", 4) == 0)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001698 return server->ops->parse_lease_buf(cc, epoch,
1699 lease_key);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001700
1701 next = le32_to_cpu(cc->Next);
1702 if (!next)
1703 break;
1704 remaining -= next;
1705 cc = (struct create_context *)((char *)cc + next);
1706 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001707
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001708 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001709}
1710
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001711static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001712add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1713 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001714{
1715 struct smb2_create_req *req = iov[0].iov_base;
1716 unsigned int num = *num_iovec;
1717
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001718 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001719 if (iov[num].iov_base == NULL)
1720 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001721 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001722 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1723 if (!req->CreateContextsOffset)
1724 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001725 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001726 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001727 le32_add_cpu(&req->CreateContextsLength,
1728 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001729 *num_iovec = num + 1;
1730 return 0;
1731}
1732
Steve Frenchb56eae42015-11-03 09:26:27 -06001733static struct create_durable_v2 *
1734create_durable_v2_buf(struct cifs_fid *pfid)
1735{
1736 struct create_durable_v2 *buf;
1737
1738 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1739 if (!buf)
1740 return NULL;
1741
1742 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1743 (struct create_durable_v2, dcontext));
1744 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1745 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1746 (struct create_durable_v2, Name));
1747 buf->ccontext.NameLength = cpu_to_le16(4);
1748
1749 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1750 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001751 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001752 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1753
1754 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1755 buf->Name[0] = 'D';
1756 buf->Name[1] = 'H';
1757 buf->Name[2] = '2';
1758 buf->Name[3] = 'Q';
1759 return buf;
1760}
1761
1762static struct create_durable_handle_reconnect_v2 *
1763create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1764{
1765 struct create_durable_handle_reconnect_v2 *buf;
1766
1767 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1768 GFP_KERNEL);
1769 if (!buf)
1770 return NULL;
1771
1772 buf->ccontext.DataOffset =
1773 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1774 dcontext));
1775 buf->ccontext.DataLength =
1776 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1777 buf->ccontext.NameOffset =
1778 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1779 Name));
1780 buf->ccontext.NameLength = cpu_to_le16(4);
1781
1782 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1783 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1784 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1785 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1786
1787 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1788 buf->Name[0] = 'D';
1789 buf->Name[1] = 'H';
1790 buf->Name[2] = '2';
1791 buf->Name[3] = 'C';
1792 return buf;
1793}
1794
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001795static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001796add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001797 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001798{
1799 struct smb2_create_req *req = iov[0].iov_base;
1800 unsigned int num = *num_iovec;
1801
Steve Frenchb56eae42015-11-03 09:26:27 -06001802 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1803 if (iov[num].iov_base == NULL)
1804 return -ENOMEM;
1805 iov[num].iov_len = sizeof(struct create_durable_v2);
1806 if (!req->CreateContextsOffset)
1807 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001808 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001809 iov[1].iov_len);
1810 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001811 *num_iovec = num + 1;
1812 return 0;
1813}
1814
1815static int
1816add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1817 struct cifs_open_parms *oparms)
1818{
1819 struct smb2_create_req *req = iov[0].iov_base;
1820 unsigned int num = *num_iovec;
1821
1822 /* indicate that we don't need to relock the file */
1823 oparms->reconnect = false;
1824
1825 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1826 if (iov[num].iov_base == NULL)
1827 return -ENOMEM;
1828 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1829 if (!req->CreateContextsOffset)
1830 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001831 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001832 iov[1].iov_len);
1833 le32_add_cpu(&req->CreateContextsLength,
1834 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001835 *num_iovec = num + 1;
1836 return 0;
1837}
1838
1839static int
1840add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1841 struct cifs_open_parms *oparms, bool use_persistent)
1842{
1843 struct smb2_create_req *req = iov[0].iov_base;
1844 unsigned int num = *num_iovec;
1845
1846 if (use_persistent) {
1847 if (oparms->reconnect)
1848 return add_durable_reconnect_v2_context(iov, num_iovec,
1849 oparms);
1850 else
1851 return add_durable_v2_context(iov, num_iovec, oparms);
1852 }
1853
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001854 if (oparms->reconnect) {
1855 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1856 /* indicate that we don't need to relock the file */
1857 oparms->reconnect = false;
1858 } else
1859 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001860 if (iov[num].iov_base == NULL)
1861 return -ENOMEM;
1862 iov[num].iov_len = sizeof(struct create_durable);
1863 if (!req->CreateContextsOffset)
1864 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001865 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001866 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001867 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001868 *num_iovec = num + 1;
1869 return 0;
1870}
1871
Aurelien Aptelf0712922017-02-22 14:47:17 +01001872static int
1873alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1874 const char *treename, const __le16 *path)
1875{
1876 int treename_len, path_len;
1877 struct nls_table *cp;
1878 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1879
1880 /*
1881 * skip leading "\\"
1882 */
1883 treename_len = strlen(treename);
1884 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1885 return -EINVAL;
1886
1887 treename += 2;
1888 treename_len -= 2;
1889
1890 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1891
1892 /*
1893 * make room for one path separator between the treename and
1894 * path
1895 */
1896 *out_len = treename_len + 1 + path_len;
1897
1898 /*
1899 * final path needs to be null-terminated UTF16 with a
1900 * size aligned to 8
1901 */
1902
1903 *out_size = roundup((*out_len+1)*2, 8);
1904 *out_path = kzalloc(*out_size, GFP_KERNEL);
1905 if (!*out_path)
1906 return -ENOMEM;
1907
1908 cp = load_nls_default();
1909 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1910 UniStrcat(*out_path, sep);
1911 UniStrcat(*out_path, path);
1912 unload_nls(cp);
1913
1914 return 0;
1915}
1916
Steve Frenchbea851b2018-06-14 21:56:32 -05001917#ifdef CONFIG_CIFS_SMB311
1918int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
1919 umode_t mode, struct cifs_tcon *tcon,
1920 const char *full_path,
1921 struct cifs_sb_info *cifs_sb)
1922{
1923 struct smb_rqst rqst;
1924 struct smb2_create_req *req;
1925 struct smb2_create_rsp *rsp;
1926 struct TCP_Server_Info *server;
1927 struct cifs_ses *ses = tcon->ses;
1928 struct kvec iov[3]; /* make sure at least one for each open context */
1929 struct kvec rsp_iov = {NULL, 0};
1930 int resp_buftype;
1931 int uni_path_len;
1932 __le16 *copy_path = NULL;
1933 int copy_size;
1934 int rc = 0;
1935 unsigned int n_iov = 2;
1936 __u32 file_attributes = 0;
1937 char *pc_buf = NULL;
1938 int flags = 0;
1939 unsigned int total_len;
1940 __le16 *path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1941
1942 if (!path)
1943 return -ENOMEM;
1944
1945 cifs_dbg(FYI, "mkdir\n");
1946
1947 if (ses && (ses->server))
1948 server = ses->server;
1949 else
1950 return -EIO;
1951
1952 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1953
1954 if (rc)
1955 return rc;
1956
1957 if (smb3_encryption_required(tcon))
1958 flags |= CIFS_TRANSFORM_REQ;
1959
1960
1961 req->ImpersonationLevel = IL_IMPERSONATION;
1962 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
1963 /* File attributes ignored on open (used in create though) */
1964 req->FileAttributes = cpu_to_le32(file_attributes);
1965 req->ShareAccess = FILE_SHARE_ALL_LE;
1966 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
1967 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
1968
1969 iov[0].iov_base = (char *)req;
1970 /* -1 since last byte is buf[0] which is sent below (path) */
1971 iov[0].iov_len = total_len - 1;
1972
1973 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
1974
1975 /* [MS-SMB2] 2.2.13 NameOffset:
1976 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1977 * the SMB2 header, the file name includes a prefix that will
1978 * be processed during DFS name normalization as specified in
1979 * section 3.3.5.9. Otherwise, the file name is relative to
1980 * the share that is identified by the TreeId in the SMB2
1981 * header.
1982 */
1983 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1984 int name_len;
1985
1986 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1987 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1988 &name_len,
1989 tcon->treeName, path);
1990 if (rc) {
1991 cifs_small_buf_release(req);
1992 return rc;
1993 }
1994 req->NameLength = cpu_to_le16(name_len * 2);
1995 uni_path_len = copy_size;
1996 path = copy_path;
1997 } else {
1998 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1999 /* MUST set path len (NameLength) to 0 opening root of share */
2000 req->NameLength = cpu_to_le16(uni_path_len - 2);
2001 if (uni_path_len % 8 != 0) {
2002 copy_size = roundup(uni_path_len, 8);
2003 copy_path = kzalloc(copy_size, GFP_KERNEL);
2004 if (!copy_path) {
2005 cifs_small_buf_release(req);
2006 return -ENOMEM;
2007 }
2008 memcpy((char *)copy_path, (const char *)path,
2009 uni_path_len);
2010 uni_path_len = copy_size;
2011 path = copy_path;
2012 }
2013 }
2014
2015 iov[1].iov_len = uni_path_len;
2016 iov[1].iov_base = path;
2017 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2018
2019 if (tcon->posix_extensions) {
2020 if (n_iov > 2) {
2021 struct create_context *ccontext =
2022 (struct create_context *)iov[n_iov-1].iov_base;
2023 ccontext->Next =
2024 cpu_to_le32(iov[n_iov-1].iov_len);
2025 }
2026
2027 rc = add_posix_context(iov, &n_iov, mode);
2028 if (rc) {
2029 cifs_small_buf_release(req);
2030 kfree(copy_path);
2031 return rc;
2032 }
2033 pc_buf = iov[n_iov-1].iov_base;
2034 }
2035
2036
2037 memset(&rqst, 0, sizeof(struct smb_rqst));
2038 rqst.rq_iov = iov;
2039 rqst.rq_nvec = n_iov;
2040
2041 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2042 &rsp_iov);
2043
2044 cifs_small_buf_release(req);
2045 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2046
2047 if (rc != 0) {
2048 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2049 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2050 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES, rc);
2051 goto smb311_mkdir_exit;
2052 } else
2053 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2054 ses->Suid, CREATE_NOT_FILE,
2055 FILE_WRITE_ATTRIBUTES);
2056
2057 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2058
2059 /* Eventually save off posix specific response info and timestaps */
2060
2061smb311_mkdir_exit:
2062 kfree(copy_path);
2063 kfree(pc_buf);
2064 free_rsp_buf(resp_buftype, rsp);
2065 return rc;
2066
2067}
2068#endif /* SMB311 */
2069
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002070int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002071SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002072 __u8 *oplock, struct smb2_file_all_info *buf,
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002073 struct kvec *err_iov, int *buftype)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002074{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002075 struct smb_rqst rqst;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002076 struct smb2_create_req *req;
2077 struct smb2_create_rsp *rsp;
2078 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002079 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002080 struct cifs_ses *ses = tcon->ses;
Steve Frenchce558b02018-05-31 19:16:54 -05002081 struct kvec iov[5]; /* make sure at least one for each open context */
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10002082 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002083 int resp_buftype;
2084 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002085 __le16 *copy_path = NULL;
2086 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002087 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002088 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002089 __u32 file_attributes = 0;
Steve Frenchce558b02018-05-31 19:16:54 -05002090 char *dhc_buf = NULL, *lc_buf = NULL, *pc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002091 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002092 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002093
Joe Perchesf96637b2013-05-04 22:12:25 -05002094 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002095
2096 if (ses && (ses->server))
2097 server = ses->server;
2098 else
2099 return -EIO;
2100
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002101 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2102
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002103 if (rc)
2104 return rc;
2105
Steve French5a77e752018-05-09 17:43:08 -05002106 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002107 flags |= CIFS_TRANSFORM_REQ;
2108
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002109 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002110 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002111 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2112 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002113
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002114 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002115 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002116 /* File attributes ignored on open (used in create though) */
2117 req->FileAttributes = cpu_to_le32(file_attributes);
2118 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002119 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2120 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002121
2122 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002123 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002124 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002125
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002126 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002127
2128 /* [MS-SMB2] 2.2.13 NameOffset:
2129 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2130 * the SMB2 header, the file name includes a prefix that will
2131 * be processed during DFS name normalization as specified in
2132 * section 3.3.5.9. Otherwise, the file name is relative to
2133 * the share that is identified by the TreeId in the SMB2
2134 * header.
2135 */
2136 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2137 int name_len;
2138
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002139 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002140 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2141 &name_len,
2142 tcon->treeName, path);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11002143 if (rc) {
2144 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01002145 return rc;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11002146 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01002147 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002148 uni_path_len = copy_size;
2149 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002150 } else {
2151 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2152 /* MUST set path len (NameLength) to 0 opening root of share */
2153 req->NameLength = cpu_to_le16(uni_path_len - 2);
2154 if (uni_path_len % 8 != 0) {
2155 copy_size = roundup(uni_path_len, 8);
2156 copy_path = kzalloc(copy_size, GFP_KERNEL);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11002157 if (!copy_path) {
2158 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01002159 return -ENOMEM;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11002160 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01002161 memcpy((char *)copy_path, (const char *)path,
2162 uni_path_len);
2163 uni_path_len = copy_size;
2164 path = copy_path;
2165 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002166 }
2167
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002168 iov[1].iov_len = uni_path_len;
2169 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002170
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002171 if (!server->oplocks)
2172 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2173
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002174 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002175 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2176 req->RequestedOplockLevel = *oplock;
2177 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002178 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002179 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002180 cifs_small_buf_release(req);
2181 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002182 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002183 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002184 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002185 }
2186
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002187 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2188 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002189 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002190 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002191 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002192 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002193 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002194 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002195
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002196 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002197 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002198 if (rc) {
2199 cifs_small_buf_release(req);
2200 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04002201 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002202 return rc;
2203 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002204 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002205 }
2206
Steve Frenchce558b02018-05-31 19:16:54 -05002207#ifdef CONFIG_CIFS_SMB311
2208 if (tcon->posix_extensions) {
2209 if (n_iov > 2) {
2210 struct create_context *ccontext =
2211 (struct create_context *)iov[n_iov-1].iov_base;
2212 ccontext->Next =
2213 cpu_to_le32(iov[n_iov-1].iov_len);
2214 }
2215
2216 rc = add_posix_context(iov, &n_iov, oparms->mode);
2217 if (rc) {
2218 cifs_small_buf_release(req);
2219 kfree(copy_path);
2220 kfree(lc_buf);
2221 kfree(dhc_buf);
2222 return rc;
2223 }
2224 pc_buf = iov[n_iov-1].iov_base;
2225 }
2226#endif /* SMB311 */
2227
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002228 memset(&rqst, 0, sizeof(struct smb_rqst));
2229 rqst.rq_iov = iov;
2230 rqst.rq_nvec = n_iov;
2231
2232 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002233 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002234 cifs_small_buf_release(req);
2235 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002236
2237 if (rc != 0) {
2238 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002239 if (err_iov && rsp) {
2240 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002241 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002242 resp_buftype = CIFS_NO_BUFFER;
2243 rsp = NULL;
2244 }
Steve French28d59362018-05-30 21:42:34 -05002245 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2246 oparms->create_options, oparms->desired_access, rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002247 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002248 } else
2249 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2250 ses->Suid, oparms->create_options,
2251 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002252
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002253 oparms->fid->persistent_fid = rsp->PersistentFileId;
2254 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002255
2256 if (buf) {
2257 memcpy(buf, &rsp->CreationTime, 32);
2258 buf->AllocationSize = rsp->AllocationSize;
2259 buf->EndOfFile = rsp->EndofFile;
2260 buf->Attributes = rsp->FileAttributes;
2261 buf->NumberOfLinks = cpu_to_le32(1);
2262 buf->DeletePending = 0;
2263 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002264
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002265 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002266 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch,
2267 oparms->fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002268 else
2269 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002270creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002271 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04002272 kfree(lc_buf);
2273 kfree(dhc_buf);
Steve Frenchce558b02018-05-31 19:16:54 -05002274 kfree(pc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002275 free_rsp_buf(resp_buftype, rsp);
2276 return rc;
2277}
2278
Steve French4a72daf2013-06-25 00:20:49 -05002279/*
2280 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2281 */
2282int
2283SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002284 u64 volatile_fid, u32 opcode, bool is_fsctl,
Aurelien Aptel51146622017-02-28 15:08:41 +01002285 char *in_data, u32 indatalen,
2286 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05002287{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002288 struct smb_rqst rqst;
Steve French4a72daf2013-06-25 00:20:49 -05002289 struct smb2_ioctl_req *req;
2290 struct smb2_ioctl_rsp *rsp;
Steve French8e353102015-03-26 19:47:02 -05002291 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05002292 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002293 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002294 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002295 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002296 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002297 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002298 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05002299
2300 cifs_dbg(FYI, "SMB2 IOCTL\n");
2301
Steve French3d1a3742014-08-11 21:05:25 -05002302 if (out_data != NULL)
2303 *out_data = NULL;
2304
Steve French4a72daf2013-06-25 00:20:49 -05002305 /* zero out returned data len, in case of error */
2306 if (plen)
2307 *plen = 0;
2308
Steve French8e353102015-03-26 19:47:02 -05002309 if (tcon)
2310 ses = tcon->ses;
2311 else
2312 return -EIO;
2313
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002314 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05002315 return -EIO;
2316
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002317 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002318 if (rc)
2319 return rc;
2320
Steve French5a77e752018-05-09 17:43:08 -05002321 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002322 flags |= CIFS_TRANSFORM_REQ;
2323
Steve French4a72daf2013-06-25 00:20:49 -05002324 req->CtlCode = cpu_to_le32(opcode);
2325 req->PersistentFileId = persistent_fid;
2326 req->VolatileFileId = volatile_fid;
2327
2328 if (indatalen) {
2329 req->InputCount = cpu_to_le32(indatalen);
2330 /* do not set InputOffset if no input data */
2331 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002332 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05002333 iov[1].iov_base = in_data;
2334 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002335 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05002336 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002337 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05002338
2339 req->OutputOffset = 0;
2340 req->OutputCount = 0; /* MBZ */
2341
2342 /*
2343 * Could increase MaxOutputResponse, but that would require more
2344 * than one credit. Windows typically sets this smaller, but for some
2345 * ioctls it may be useful to allow server to send more. No point
2346 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05002347 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2348 * (by default, note that it can be overridden to make max larger)
2349 * in responses (except for read responses which can be bigger.
2350 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05002351 */
Steve French7db0a6e2017-05-03 21:12:20 -05002352 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05002353
2354 if (is_fsctl)
2355 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2356 else
2357 req->Flags = 0;
2358
2359 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05002360
Steve French7ff8d452013-10-14 00:44:19 -05002361 /*
2362 * If no input data, the size of ioctl struct in
2363 * protocol spec still includes a 1 byte data buffer,
2364 * but if input data passed to ioctl, we do not
2365 * want to double count this, so we do not send
2366 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002367 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05002368 */
2369
2370 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002371 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05002372 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002373 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05002374
Steve French4587eee2017-10-25 15:58:31 -05002375 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2376 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002377 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002378
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002379 memset(&rqst, 0, sizeof(struct smb_rqst));
2380 rqst.rq_iov = iov;
2381 rqst.rq_nvec = n_iov;
2382
2383 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002384 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002385 cifs_small_buf_release(req);
2386 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002387
Steve Frencheccb4422018-05-17 21:16:55 -05002388 if (rc != 0)
2389 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2390 ses->Suid, 0, opcode, rc);
2391
Steve French9bf0c9c2013-11-16 18:05:28 -06002392 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002393 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002394 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002395 } else if (rc == -EINVAL) {
2396 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2397 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002398 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002399 goto ioctl_exit;
2400 }
Steve French4a72daf2013-06-25 00:20:49 -05002401 }
2402
2403 /* check if caller wants to look at return data or just return rc */
2404 if ((plen == NULL) || (out_data == NULL))
2405 goto ioctl_exit;
2406
2407 *plen = le32_to_cpu(rsp->OutputCount);
2408
2409 /* We check for obvious errors in the output buffer length and offset */
2410 if (*plen == 0)
2411 goto ioctl_exit; /* server returned no data */
2412 else if (*plen > 0xFF00) {
2413 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2414 *plen = 0;
2415 rc = -EIO;
2416 goto ioctl_exit;
2417 }
2418
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002419 if (rsp_iov.iov_len < le32_to_cpu(rsp->OutputOffset) + *plen) {
Steve French4a72daf2013-06-25 00:20:49 -05002420 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2421 le32_to_cpu(rsp->OutputOffset));
2422 *plen = 0;
2423 rc = -EIO;
2424 goto ioctl_exit;
2425 }
2426
2427 *out_data = kmalloc(*plen, GFP_KERNEL);
2428 if (*out_data == NULL) {
2429 rc = -ENOMEM;
2430 goto ioctl_exit;
2431 }
2432
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002433 memcpy(*out_data, (char *)rsp + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002434ioctl_exit:
2435 free_rsp_buf(resp_buftype, rsp);
2436 return rc;
2437}
2438
Steve French64a5cfa2013-10-14 15:31:32 -05002439/*
2440 * Individual callers to ioctl worker function follow
2441 */
2442
2443int
2444SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2445 u64 persistent_fid, u64 volatile_fid)
2446{
2447 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002448 struct compress_ioctl fsctl_input;
2449 char *ret_data = NULL;
2450
2451 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002452 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002453
2454 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2455 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2456 (char *)&fsctl_input /* data input */,
2457 2 /* in data len */, &ret_data /* out data */, NULL);
2458
2459 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002460
2461 return rc;
2462}
2463
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002464int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002465SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2466 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002467{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002468 struct smb_rqst rqst;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002469 struct smb2_close_req *req;
2470 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002471 struct cifs_ses *ses = tcon->ses;
2472 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002473 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002474 int resp_buftype;
2475 int rc = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002476 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002477
Joe Perchesf96637b2013-05-04 22:12:25 -05002478 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002479
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002480 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002481 return -EIO;
2482
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002483 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002484 if (rc)
2485 return rc;
2486
Steve French5a77e752018-05-09 17:43:08 -05002487 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002488 flags |= CIFS_TRANSFORM_REQ;
2489
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002490 req->PersistentFileId = persistent_fid;
2491 req->VolatileFileId = volatile_fid;
2492
2493 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002494 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002495
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002496 memset(&rqst, 0, sizeof(struct smb_rqst));
2497 rqst.rq_iov = iov;
2498 rqst.rq_nvec = 1;
2499
2500 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002501 cifs_small_buf_release(req);
2502 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002503
2504 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002505 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002506 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2507 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002508 goto close_exit;
2509 }
2510
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002511 /* BB FIXME - decode close response, update inode for caching */
2512
2513close_exit:
2514 free_rsp_buf(resp_buftype, rsp);
2515 return rc;
2516}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002517
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002518int
2519SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2520 u64 persistent_fid, u64 volatile_fid)
2521{
2522 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2523}
2524
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002525static int
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002526validate_iov(unsigned int offset, unsigned int buffer_length,
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002527 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002528{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002529 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002530 char *end_of_smb = smb_len + (char *)iov->iov_base;
2531 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002532 char *end_of_buf = begin_of_buf + buffer_length;
2533
2534
2535 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002536 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2537 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002538 return -EINVAL;
2539 }
2540
2541 /* check if beyond RFC1001 maximum length */
2542 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002543 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2544 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002545 return -EINVAL;
2546 }
2547
2548 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002549 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002550 return -EINVAL;
2551 }
2552
2553 return 0;
2554}
2555
2556/*
2557 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2558 * Caller must free buffer.
2559 */
2560static int
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002561validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002562 struct kvec *iov, unsigned int minbufsize,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002563 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002564{
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002565 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002566 int rc;
2567
2568 if (!data)
2569 return -EINVAL;
2570
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002571 rc = validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002572 if (rc)
2573 return rc;
2574
2575 memcpy(data, begin_of_buf, buffer_length);
2576
2577 return 0;
2578}
2579
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002580static int
2581query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002582 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2583 u32 additional_info, size_t output_len, size_t min_len, void **data,
2584 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002585{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002586 struct smb_rqst rqst;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002587 struct smb2_query_info_req *req;
2588 struct smb2_query_info_rsp *rsp = NULL;
2589 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002590 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002591 int rc = 0;
2592 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002593 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002594 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002595 unsigned int total_len;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002596
Joe Perchesf96637b2013-05-04 22:12:25 -05002597 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002598
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002599 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002600 return -EIO;
2601
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002602 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2603 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002604 if (rc)
2605 return rc;
2606
Steve French5a77e752018-05-09 17:43:08 -05002607 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002608 flags |= CIFS_TRANSFORM_REQ;
2609
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002610 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002611 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002612 req->PersistentFileId = persistent_fid;
2613 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002614 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002615
2616 /*
2617 * We do not use the input buffer (do not send extra byte)
2618 */
2619 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002620
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002621 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002622
2623 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002624 /* 1 for Buffer */
2625 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002626
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002627 memset(&rqst, 0, sizeof(struct smb_rqst));
2628 rqst.rq_iov = iov;
2629 rqst.rq_nvec = 1;
2630
2631 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002632 cifs_small_buf_release(req);
2633 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002634
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002635 if (rc) {
2636 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002637 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2638 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002639 goto qinf_exit;
2640 }
2641
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002642 if (dlen) {
2643 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2644 if (!*data) {
2645 *data = kmalloc(*dlen, GFP_KERNEL);
2646 if (!*data) {
2647 cifs_dbg(VFS,
2648 "Error %d allocating memory for acl\n",
2649 rc);
2650 *dlen = 0;
2651 goto qinf_exit;
2652 }
2653 }
2654 }
2655
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002656 rc = validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002657 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002658 &rsp_iov, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002659
2660qinf_exit:
2661 free_rsp_buf(resp_buftype, rsp);
2662 return rc;
2663}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002664
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002665int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002666 u64 persistent_fid, u64 volatile_fid,
2667 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002668{
2669 return query_info(xid, tcon, persistent_fid, volatile_fid,
2670 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002671 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002672 sizeof(struct smb2_file_full_ea_info),
2673 (void **)&data,
2674 NULL);
2675}
2676
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002677int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2678 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002679{
2680 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002681 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002682 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002683 sizeof(struct smb2_file_all_info), (void **)&data,
2684 NULL);
2685}
2686
2687int
2688SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2689 u64 persistent_fid, u64 volatile_fid,
2690 void **data, u32 *plen)
2691{
2692 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2693 *plen = 0;
2694
2695 return query_info(xid, tcon, persistent_fid, volatile_fid,
2696 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05002697 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002698}
2699
2700int
2701SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2702 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2703{
2704 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002705 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002706 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002707 sizeof(struct smb2_file_internal_info),
2708 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002709}
2710
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002711/*
2712 * This is a no-op for now. We're not really interested in the reply, but
2713 * rather in the fact that the server sent one and that server->lstrp
2714 * gets updated.
2715 *
2716 * FIXME: maybe we should consider checking that the reply matches request?
2717 */
2718static void
2719smb2_echo_callback(struct mid_q_entry *mid)
2720{
2721 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002722 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002723 unsigned int credits_received = 1;
2724
2725 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002726 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002727
2728 DeleteMidQEntry(mid);
2729 add_credits(server, credits_received, CIFS_ECHO_OP);
2730}
2731
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002732void smb2_reconnect_server(struct work_struct *work)
2733{
2734 struct TCP_Server_Info *server = container_of(work,
2735 struct TCP_Server_Info, reconnect.work);
2736 struct cifs_ses *ses;
2737 struct cifs_tcon *tcon, *tcon2;
2738 struct list_head tmp_list;
2739 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002740 int rc;
2741 int resched = false;
2742
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002743
2744 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2745 mutex_lock(&server->reconnect_mutex);
2746
2747 INIT_LIST_HEAD(&tmp_list);
2748 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2749
2750 spin_lock(&cifs_tcp_ses_lock);
2751 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2752 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002753 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002754 tcon->tc_count++;
2755 list_add_tail(&tcon->rlist, &tmp_list);
2756 tcon_exist = true;
2757 }
2758 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002759 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2760 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2761 tcon_exist = true;
2762 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002763 }
2764 /*
2765 * Get the reference to server struct to be sure that the last call of
2766 * cifs_put_tcon() in the loop below won't release the server pointer.
2767 */
2768 if (tcon_exist)
2769 server->srv_count++;
2770
2771 spin_unlock(&cifs_tcp_ses_lock);
2772
2773 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002774 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2775 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002776 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002777 else
2778 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002779 list_del_init(&tcon->rlist);
2780 cifs_put_tcon(tcon);
2781 }
2782
2783 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002784 if (resched)
2785 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002786 mutex_unlock(&server->reconnect_mutex);
2787
2788 /* now we can safely release srv struct */
2789 if (tcon_exist)
2790 cifs_put_tcp_session(server, 1);
2791}
2792
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002793int
2794SMB2_echo(struct TCP_Server_Info *server)
2795{
2796 struct smb2_echo_req *req;
2797 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002798 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002799 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002800 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002801 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002802
Joe Perchesf96637b2013-05-04 22:12:25 -05002803 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002804
Steve French4fcd1812016-06-22 20:12:05 -05002805 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002806 /* No need to send echo on newly established connections */
2807 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2808 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002809 }
2810
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002811 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002812 if (rc)
2813 return rc;
2814
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002815 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002816
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002817 iov[0].iov_len = total_len;
2818 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002819
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002820 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2821 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002822 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002823 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002824
2825 cifs_small_buf_release(req);
2826 return rc;
2827}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002828
2829int
2830SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2831 u64 volatile_fid)
2832{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002833 struct smb_rqst rqst;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002834 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002835 struct cifs_ses *ses = tcon->ses;
2836 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002837 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002838 int resp_buftype;
2839 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002840 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002841 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002842
Joe Perchesf96637b2013-05-04 22:12:25 -05002843 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002844
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002845 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002846 return -EIO;
2847
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002848 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002849 if (rc)
2850 return rc;
2851
Steve French5a77e752018-05-09 17:43:08 -05002852 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002853 flags |= CIFS_TRANSFORM_REQ;
2854
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002855 req->PersistentFileId = persistent_fid;
2856 req->VolatileFileId = volatile_fid;
2857
2858 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002859 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002860
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002861 memset(&rqst, 0, sizeof(struct smb_rqst));
2862 rqst.rq_iov = iov;
2863 rqst.rq_nvec = 1;
2864
2865 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002866 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002867
Steve Frencheccb4422018-05-17 21:16:55 -05002868 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002869 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002870 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
2871 rc);
2872 }
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002873
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002874 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002875 return rc;
2876}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002877
2878/*
2879 * To form a chain of read requests, any read requests after the first should
2880 * have the end_of_chain boolean set to true.
2881 */
2882static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002883smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002884 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2885 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002886{
2887 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002888 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002889 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002890 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002891
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002892 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2893 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002894 if (rc)
2895 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002896
2897 server = io_parms->tcon->ses->server;
2898 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002899 return -ECONNABORTED;
2900
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002901 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002902 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002903
2904 req->PersistentFileId = io_parms->persistent_fid;
2905 req->VolatileFileId = io_parms->volatile_fid;
2906 req->ReadChannelInfoOffset = 0; /* reserved */
2907 req->ReadChannelInfoLength = 0; /* reserved */
2908 req->Channel = 0; /* reserved */
2909 req->MinimumCount = 0;
2910 req->Length = cpu_to_le32(io_parms->length);
2911 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07002912#ifdef CONFIG_CIFS_SMB_DIRECT
2913 /*
2914 * If we want to do a RDMA write, fill in and append
2915 * smbd_buffer_descriptor_v1 to the end of read request
2916 */
Long Libb4c0412018-04-17 12:17:08 -07002917 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07002918 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002919
Long Libd3dcc62017-11-22 17:38:47 -07002920 struct smbd_buffer_descriptor_v1 *v1;
2921 bool need_invalidate =
2922 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2923
2924 rdata->mr = smbd_register_mr(
2925 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07002926 rdata->nr_pages, rdata->page_offset,
2927 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07002928 if (!rdata->mr)
2929 return -ENOBUFS;
2930
2931 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2932 if (need_invalidate)
2933 req->Channel = SMB2_CHANNEL_RDMA_V1;
2934 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002935 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07002936 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002937 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07002938 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002939 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2940 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2941 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07002942
2943 *total_len += sizeof(*v1) - 1;
2944 }
2945#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002946 if (request_type & CHAINED_REQUEST) {
2947 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002948 /* next 8-byte aligned request */
2949 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2950 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002951 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002952 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002953 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002954 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002955 /*
2956 * Related requests use info from previous read request
2957 * in chain.
2958 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002959 shdr->SessionId = 0xFFFFFFFF;
2960 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002961 req->PersistentFileId = 0xFFFFFFFF;
2962 req->VolatileFileId = 0xFFFFFFFF;
2963 }
2964 }
2965 if (remaining_bytes > io_parms->length)
2966 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2967 else
2968 req->RemainingBytes = 0;
2969
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002970 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002971 return rc;
2972}
2973
2974static void
2975smb2_readv_callback(struct mid_q_entry *mid)
2976{
2977 struct cifs_readdata *rdata = mid->callback_data;
2978 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2979 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002980 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002981 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002982 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002983 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2984 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002985 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07002986 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07002987 .rq_npages = rdata->nr_pages,
2988 .rq_pagesz = rdata->pagesz,
2989 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002990
Joe Perchesf96637b2013-05-04 22:12:25 -05002991 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2992 __func__, mid->mid, mid->mid_state, rdata->result,
2993 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002994
2995 switch (mid->mid_state) {
2996 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002997 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002998 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002999 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003000 int rc;
3001
Jeff Layton0b688cf2012-09-18 16:20:34 -07003002 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003003 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003004 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
3005 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003006 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003007 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003008 task_io_account_read(rdata->got_bytes);
3009 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003010 break;
3011 case MID_REQUEST_SUBMITTED:
3012 case MID_RETRY_NEEDED:
3013 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003014 if (server->sign && rdata->got_bytes)
3015 /* reset bytes number since we can not check a sign */
3016 rdata->got_bytes = 0;
3017 /* FIXME: should this be counted toward the initiating task? */
3018 task_io_account_read(rdata->got_bytes);
3019 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003020 break;
3021 default:
3022 if (rdata->result != -ENODATA)
3023 rdata->result = -EIO;
3024 }
Long Libd3dcc62017-11-22 17:38:47 -07003025#ifdef CONFIG_CIFS_SMB_DIRECT
3026 /*
3027 * If this rdata has a memmory registered, the MR can be freed
3028 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3029 * because they have limited number and are used for future I/Os
3030 */
3031 if (rdata->mr) {
3032 smbd_deregister_mr(rdata->mr);
3033 rdata->mr = NULL;
3034 }
3035#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003036 if (rdata->result)
3037 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3038
3039 queue_work(cifsiod_wq, &rdata->work);
3040 DeleteMidQEntry(mid);
3041 add_credits(server, credits_received, 0);
3042}
3043
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003044/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003045int
3046smb2_async_readv(struct cifs_readdata *rdata)
3047{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003048 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003049 char *buf;
3050 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003051 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003052 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003053 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003054 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003055 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003056
Joe Perchesf96637b2013-05-04 22:12:25 -05003057 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3058 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003059
3060 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3061 io_parms.offset = rdata->offset;
3062 io_parms.length = rdata->bytes;
3063 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3064 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3065 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003066
3067 server = io_parms.tcon->ses->server;
3068
Long Li2dabfd52017-11-07 01:54:53 -07003069 rc = smb2_new_read_req(
3070 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003071 if (rc) {
3072 if (rc == -EAGAIN && rdata->credits) {
3073 /* credits was reset by reconnect */
3074 rdata->credits = 0;
3075 /* reduce in_flight value since we won't send the req */
3076 spin_lock(&server->req_lock);
3077 server->in_flight--;
3078 spin_unlock(&server->req_lock);
3079 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003080 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003081 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003082
Steve French5a77e752018-05-09 17:43:08 -05003083 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003084 flags |= CIFS_TRANSFORM_REQ;
3085
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003086 rdata->iov[0].iov_base = buf;
3087 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003088
3089 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003090
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003091 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003092 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003093 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003094 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003095 spin_lock(&server->req_lock);
3096 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003097 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003098 spin_unlock(&server->req_lock);
3099 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003100 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003101 }
3102
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003103 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07003104 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003105 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003106 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003107 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003108 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003109 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003110 trace_smb3_read_err(rc, 0 /* xid */, io_parms.persistent_fid,
3111 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
3112 io_parms.offset, io_parms.length);
3113 } else
3114 trace_smb3_read_done(0 /* xid */, io_parms.persistent_fid,
3115 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
3116 io_parms.offset, io_parms.length);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003117
3118 cifs_small_buf_release(buf);
3119 return rc;
3120}
Pavel Shilovsky33319142012-09-18 16:20:29 -07003121
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003122int
3123SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3124 unsigned int *nbytes, char **buf, int *buf_type)
3125{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003126 struct smb_rqst rqst;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003127 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003128 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003129 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003130 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003131 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003132 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003133 int flags = CIFS_LOG_ERROR;
3134 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003135
3136 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07003137 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003138 if (rc)
3139 return rc;
3140
Steve French5a77e752018-05-09 17:43:08 -05003141 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003142 flags |= CIFS_TRANSFORM_REQ;
3143
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003144 iov[0].iov_base = (char *)req;
3145 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003146
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003147 memset(&rqst, 0, sizeof(struct smb_rqst));
3148 rqst.rq_iov = iov;
3149 rqst.rq_nvec = 1;
3150
3151 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003152 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003153
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003154 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003155
3156 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003157 if (rc != -ENODATA) {
3158 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3159 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003160 }
Steve Frencheccb4422018-05-17 21:16:55 -05003161 trace_smb3_read_err(rc, xid, req->PersistentFileId,
3162 io_parms->tcon->tid, ses->Suid,
3163 io_parms->offset, io_parms->length);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003164 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3165 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05003166 } else
3167 trace_smb3_read_done(xid, req->PersistentFileId,
3168 io_parms->tcon->tid, ses->Suid,
3169 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003170
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003171 *nbytes = le32_to_cpu(rsp->DataLength);
3172 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3173 (*nbytes > io_parms->length)) {
3174 cifs_dbg(FYI, "bad length %d for count %d\n",
3175 *nbytes, io_parms->length);
3176 rc = -EIO;
3177 *nbytes = 0;
3178 }
3179
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003180 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003181 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003182 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003183 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003184 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003185 if (resp_buftype == CIFS_SMALL_BUFFER)
3186 *buf_type = CIFS_SMALL_BUFFER;
3187 else if (resp_buftype == CIFS_LARGE_BUFFER)
3188 *buf_type = CIFS_LARGE_BUFFER;
3189 }
3190 return rc;
3191}
3192
Pavel Shilovsky33319142012-09-18 16:20:29 -07003193/*
3194 * Check the mid_state and signature on received buffer (if any), and queue the
3195 * workqueue completion task.
3196 */
3197static void
3198smb2_writev_callback(struct mid_q_entry *mid)
3199{
3200 struct cifs_writedata *wdata = mid->callback_data;
3201 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3202 unsigned int written;
3203 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3204 unsigned int credits_received = 1;
3205
3206 switch (mid->mid_state) {
3207 case MID_RESPONSE_RECEIVED:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003208 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003209 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
3210 if (wdata->result != 0)
3211 break;
3212
3213 written = le32_to_cpu(rsp->DataLength);
3214 /*
3215 * Mask off high 16 bits when bytes written as returned
3216 * by the server is greater than bytes requested by the
3217 * client. OS/2 servers are known to set incorrect
3218 * CountHigh values.
3219 */
3220 if (written > wdata->bytes)
3221 written &= 0xFFFF;
3222
3223 if (written < wdata->bytes)
3224 wdata->result = -ENOSPC;
3225 else
3226 wdata->bytes = written;
3227 break;
3228 case MID_REQUEST_SUBMITTED:
3229 case MID_RETRY_NEEDED:
3230 wdata->result = -EAGAIN;
3231 break;
3232 default:
3233 wdata->result = -EIO;
3234 break;
3235 }
Long Lidb223a52017-11-22 17:38:45 -07003236#ifdef CONFIG_CIFS_SMB_DIRECT
3237 /*
3238 * If this wdata has a memory registered, the MR can be freed
3239 * The number of MRs available is limited, it's important to recover
3240 * used MR as soon as I/O is finished. Hold MR longer in the later
3241 * I/O process can possibly result in I/O deadlock due to lack of MR
3242 * to send request on I/O retry
3243 */
3244 if (wdata->mr) {
3245 smbd_deregister_mr(wdata->mr);
3246 wdata->mr = NULL;
3247 }
3248#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003249 if (wdata->result)
3250 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3251
3252 queue_work(cifsiod_wq, &wdata->work);
3253 DeleteMidQEntry(mid);
3254 add_credits(tcon->ses->server, credits_received, 0);
3255}
3256
3257/* smb2_async_writev - send an async write, and set up mid to handle result */
3258int
Steve French4a5c80d2014-02-07 20:45:12 -06003259smb2_async_writev(struct cifs_writedata *wdata,
3260 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07003261{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003262 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003263 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003264 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003265 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003266 struct TCP_Server_Info *server = tcon->ses->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003267 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003268 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003269 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003270
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003271 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003272 if (rc) {
3273 if (rc == -EAGAIN && wdata->credits) {
3274 /* credits was reset by reconnect */
3275 wdata->credits = 0;
3276 /* reduce in_flight value since we won't send the req */
3277 spin_lock(&server->req_lock);
3278 server->in_flight--;
3279 spin_unlock(&server->req_lock);
3280 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003281 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003282 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003283
Steve French5a77e752018-05-09 17:43:08 -05003284 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003285 flags |= CIFS_TRANSFORM_REQ;
3286
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003287 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003288 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003289
3290 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3291 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3292 req->WriteChannelInfoOffset = 0;
3293 req->WriteChannelInfoLength = 0;
3294 req->Channel = 0;
3295 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003296 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003297 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003298 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07003299#ifdef CONFIG_CIFS_SMB_DIRECT
3300 /*
3301 * If we want to do a server RDMA read, fill in and append
3302 * smbd_buffer_descriptor_v1 to the end of write request
3303 */
Long Libb4c0412018-04-17 12:17:08 -07003304 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003305 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003306
Long Lidb223a52017-11-22 17:38:45 -07003307 struct smbd_buffer_descriptor_v1 *v1;
3308 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3309
3310 wdata->mr = smbd_register_mr(
3311 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003312 wdata->nr_pages, wdata->page_offset,
3313 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07003314 if (!wdata->mr) {
3315 rc = -ENOBUFS;
3316 goto async_writev_out;
3317 }
3318 req->Length = 0;
3319 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07003320 if (wdata->nr_pages > 1)
3321 req->RemainingBytes =
3322 cpu_to_le32(
3323 (wdata->nr_pages - 1) * wdata->pagesz -
3324 wdata->page_offset + wdata->tailsz
3325 );
3326 else
3327 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003328 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3329 if (need_invalidate)
3330 req->Channel = SMB2_CHANNEL_RDMA_V1;
3331 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003332 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003333 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003334 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003335 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003336 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3337 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3338 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003339 }
3340#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003341 iov[0].iov_len = total_len - 1;
3342 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003343
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003344 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003345 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003346 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07003347 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003348 rqst.rq_npages = wdata->nr_pages;
3349 rqst.rq_pagesz = wdata->pagesz;
3350 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003351#ifdef CONFIG_CIFS_SMB_DIRECT
3352 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003353 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07003354 rqst.rq_npages = 0;
3355 }
3356#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003357 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3358 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003359
Long Lidb223a52017-11-22 17:38:45 -07003360#ifdef CONFIG_CIFS_SMB_DIRECT
3361 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3362 if (!wdata->mr)
3363 req->Length = cpu_to_le32(wdata->bytes);
3364#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003365 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003366#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003367
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003368 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003369 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003370 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003371 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003372 spin_lock(&server->req_lock);
3373 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003374 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003375 spin_unlock(&server->req_lock);
3376 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003377 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003378 }
3379
Pavel Shilovsky33319142012-09-18 16:20:29 -07003380 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003381 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3382 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003383
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003384 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003385 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3386 tcon->tid, tcon->ses->Suid, wdata->offset,
3387 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06003388 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003389 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003390 } else
3391 trace_smb3_write_done(0 /* no xid */, req->PersistentFileId,
3392 tcon->tid, tcon->ses->Suid, wdata->offset,
3393 wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003394
Pavel Shilovsky33319142012-09-18 16:20:29 -07003395async_writev_out:
3396 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003397 return rc;
3398}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003399
3400/*
3401 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3402 * The length field from io_parms must be at least 1 and indicates a number of
3403 * elements with data to write that begins with position 1 in iov array. All
3404 * data length is specified by count.
3405 */
3406int
3407SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3408 unsigned int *nbytes, struct kvec *iov, int n_vec)
3409{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003410 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003411 int rc = 0;
3412 struct smb2_write_req *req = NULL;
3413 struct smb2_write_rsp *rsp = NULL;
3414 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003415 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003416 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003417 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003418
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003419 *nbytes = 0;
3420
3421 if (n_vec < 1)
3422 return rc;
3423
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003424 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3425 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003426 if (rc)
3427 return rc;
3428
3429 if (io_parms->tcon->ses->server == NULL)
3430 return -ECONNABORTED;
3431
Steve French5a77e752018-05-09 17:43:08 -05003432 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003433 flags |= CIFS_TRANSFORM_REQ;
3434
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003435 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003436
3437 req->PersistentFileId = io_parms->persistent_fid;
3438 req->VolatileFileId = io_parms->volatile_fid;
3439 req->WriteChannelInfoOffset = 0;
3440 req->WriteChannelInfoLength = 0;
3441 req->Channel = 0;
3442 req->Length = cpu_to_le32(io_parms->length);
3443 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003444 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003445 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003446 req->RemainingBytes = 0;
3447
3448 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003449 /* 1 for Buffer */
3450 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003451
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003452 memset(&rqst, 0, sizeof(struct smb_rqst));
3453 rqst.rq_iov = iov;
3454 rqst.rq_nvec = n_vec + 1;
3455
3456 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003457 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003458 cifs_small_buf_release(req);
3459 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003460
3461 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003462 trace_smb3_write_err(xid, req->PersistentFileId,
3463 io_parms->tcon->tid,
3464 io_parms->tcon->ses->Suid,
3465 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003466 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003467 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05003468 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003469 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05003470 trace_smb3_write_done(xid, req->PersistentFileId,
3471 io_parms->tcon->tid,
3472 io_parms->tcon->ses->Suid,
3473 io_parms->offset, *nbytes);
3474 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003475
3476 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003477 return rc;
3478}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003479
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003480static unsigned int
3481num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3482{
3483 int len;
3484 unsigned int entrycount = 0;
3485 unsigned int next_offset = 0;
3486 FILE_DIRECTORY_INFO *entryptr;
3487
3488 if (bufstart == NULL)
3489 return 0;
3490
3491 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3492
3493 while (1) {
3494 entryptr = (FILE_DIRECTORY_INFO *)
3495 ((char *)entryptr + next_offset);
3496
3497 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003498 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003499 break;
3500 }
3501
3502 len = le32_to_cpu(entryptr->FileNameLength);
3503 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003504 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3505 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003506 break;
3507 }
3508
3509 *lastentry = (char *)entryptr;
3510 entrycount++;
3511
3512 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3513 if (!next_offset)
3514 break;
3515 }
3516
3517 return entrycount;
3518}
3519
3520/*
3521 * Readdir/FindFirst
3522 */
3523int
3524SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3525 u64 persistent_fid, u64 volatile_fid, int index,
3526 struct cifs_search_info *srch_inf)
3527{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003528 struct smb_rqst rqst;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003529 struct smb2_query_directory_req *req;
3530 struct smb2_query_directory_rsp *rsp = NULL;
3531 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003532 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003533 int rc = 0;
3534 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003535 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003536 unsigned char *bufptr;
3537 struct TCP_Server_Info *server;
3538 struct cifs_ses *ses = tcon->ses;
3539 __le16 asteriks = cpu_to_le16('*');
3540 char *end_of_smb;
3541 unsigned int output_size = CIFSMaxBufSize;
3542 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003543 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003544 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003545
3546 if (ses && (ses->server))
3547 server = ses->server;
3548 else
3549 return -EIO;
3550
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003551 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3552 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003553 if (rc)
3554 return rc;
3555
Steve French5a77e752018-05-09 17:43:08 -05003556 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003557 flags |= CIFS_TRANSFORM_REQ;
3558
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003559 switch (srch_inf->info_level) {
3560 case SMB_FIND_FILE_DIRECTORY_INFO:
3561 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3562 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3563 break;
3564 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3565 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3566 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3567 break;
3568 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003569 cifs_dbg(VFS, "info level %u isn't supported\n",
3570 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003571 rc = -EINVAL;
3572 goto qdir_exit;
3573 }
3574
3575 req->FileIndex = cpu_to_le32(index);
3576 req->PersistentFileId = persistent_fid;
3577 req->VolatileFileId = volatile_fid;
3578
3579 len = 0x2;
3580 bufptr = req->Buffer;
3581 memcpy(bufptr, &asteriks, len);
3582
3583 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003584 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003585 req->FileNameLength = cpu_to_le16(len);
3586 /*
3587 * BB could be 30 bytes or so longer if we used SMB2 specific
3588 * buffer lengths, but this is safe and close enough.
3589 */
3590 output_size = min_t(unsigned int, output_size, server->maxBuf);
3591 output_size = min_t(unsigned int, output_size, 2 << 15);
3592 req->OutputBufferLength = cpu_to_le32(output_size);
3593
3594 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003595 /* 1 for Buffer */
3596 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003597
3598 iov[1].iov_base = (char *)(req->Buffer);
3599 iov[1].iov_len = len;
3600
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003601 memset(&rqst, 0, sizeof(struct smb_rqst));
3602 rqst.rq_iov = iov;
3603 rqst.rq_nvec = 2;
3604
3605 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003606 cifs_small_buf_release(req);
3607 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003608
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003609 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003610 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003611 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003612 srch_inf->endOfSearch = true;
3613 rc = 0;
3614 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003615 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3616 goto qdir_exit;
3617 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003618
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10003619 rc = validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003620 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003621 info_buf_size);
3622 if (rc)
3623 goto qdir_exit;
3624
3625 srch_inf->unicode = true;
3626
3627 if (srch_inf->ntwrk_buf_start) {
3628 if (srch_inf->smallBuf)
3629 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3630 else
3631 cifs_buf_release(srch_inf->ntwrk_buf_start);
3632 }
3633 srch_inf->ntwrk_buf_start = (char *)rsp;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003634 srch_inf->srch_entries_start = srch_inf->last_entry =
3635 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
3636 end_of_smb = rsp_iov.iov_len + (char *)rsp;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003637 srch_inf->entries_in_buffer =
3638 num_entries(srch_inf->srch_entries_start, end_of_smb,
3639 &srch_inf->last_entry, info_buf_size);
3640 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003641 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3642 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3643 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003644 if (resp_buftype == CIFS_LARGE_BUFFER)
3645 srch_inf->smallBuf = false;
3646 else if (resp_buftype == CIFS_SMALL_BUFFER)
3647 srch_inf->smallBuf = true;
3648 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003649 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003650
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003651 return rc;
3652
3653qdir_exit:
3654 free_rsp_buf(resp_buftype, rsp);
3655 return rc;
3656}
3657
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003658static int
3659send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003660 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3661 u8 info_type, u32 additional_info, unsigned int num,
3662 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003663{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003664 struct smb_rqst rqst;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003665 struct smb2_set_info_req *req;
3666 struct smb2_set_info_rsp *rsp = NULL;
3667 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003668 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003669 int rc = 0;
3670 int resp_buftype;
3671 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003672 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003673 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003674 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003675
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003676 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003677 return -EIO;
3678
3679 if (!num)
3680 return -EINVAL;
3681
Kees Cook6da2ec52018-06-12 13:55:00 -07003682 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003683 if (!iov)
3684 return -ENOMEM;
3685
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003686 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003687 if (rc) {
3688 kfree(iov);
3689 return rc;
3690 }
3691
Steve French5a77e752018-05-09 17:43:08 -05003692 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003693 flags |= CIFS_TRANSFORM_REQ;
3694
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003695 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003696
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003697 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003698 req->FileInfoClass = info_class;
3699 req->PersistentFileId = persistent_fid;
3700 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003701 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003702
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003703 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003704 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003705 req->BufferLength = cpu_to_le32(*size);
3706
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003707 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003708 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003709
3710 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003711 /* 1 for Buffer */
3712 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003713
3714 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003715 le32_add_cpu(&req->BufferLength, size[i]);
3716 iov[i].iov_base = (char *)data[i];
3717 iov[i].iov_len = size[i];
3718 }
3719
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003720 memset(&rqst, 0, sizeof(struct smb_rqst));
3721 rqst.rq_iov = iov;
3722 rqst.rq_nvec = num;
3723
3724 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003725 &rsp_iov);
Stefano Briviof46ecbd2018-07-05 11:46:42 +02003726 cifs_buf_release(req);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003727 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003728
Steve Frencheccb4422018-05-17 21:16:55 -05003729 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003730 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003731 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
3732 ses->Suid, info_class, (__u32)info_type, rc);
3733 }
Steve French7d3fb242013-11-18 09:56:28 -06003734
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003735 free_rsp_buf(resp_buftype, rsp);
3736 kfree(iov);
3737 return rc;
3738}
3739
3740int
3741SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3742 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3743{
3744 struct smb2_file_rename_info info;
3745 void **data;
3746 unsigned int size[2];
3747 int rc;
3748 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3749
Kees Cook6da2ec52018-06-12 13:55:00 -07003750 data = kmalloc_array(2, sizeof(void *), GFP_KERNEL);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003751 if (!data)
3752 return -ENOMEM;
3753
3754 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3755 /* 0 = fail if target already exists */
3756 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3757 info.FileNameLength = cpu_to_le32(len);
3758
3759 data[0] = &info;
3760 size[0] = sizeof(struct smb2_file_rename_info);
3761
3762 data[1] = target_file;
3763 size[1] = len + 2 /* null */;
3764
3765 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003766 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3767 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003768 kfree(data);
3769 return rc;
3770}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003771
3772int
Steve French897fba12016-05-12 21:20:36 -05003773SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3774 u64 persistent_fid, u64 volatile_fid)
3775{
3776 __u8 delete_pending = 1;
3777 void *data;
3778 unsigned int size;
3779
3780 data = &delete_pending;
3781 size = 1; /* sizeof __u8 */
3782
3783 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003784 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3785 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003786}
3787
3788int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003789SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3790 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3791{
3792 struct smb2_file_link_info info;
3793 void **data;
3794 unsigned int size[2];
3795 int rc;
3796 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3797
Kees Cook6da2ec52018-06-12 13:55:00 -07003798 data = kmalloc_array(2, sizeof(void *), GFP_KERNEL);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003799 if (!data)
3800 return -ENOMEM;
3801
3802 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3803 /* 0 = fail if link already exists */
3804 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3805 info.FileNameLength = cpu_to_le32(len);
3806
3807 data[0] = &info;
3808 size[0] = sizeof(struct smb2_file_link_info);
3809
3810 data[1] = target_file;
3811 size[1] = len + 2 /* null */;
3812
3813 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003814 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3815 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003816 kfree(data);
3817 return rc;
3818}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003819
3820int
3821SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003822 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003823{
3824 struct smb2_file_eof_info info;
3825 void *data;
3826 unsigned int size;
3827
3828 info.EndOfFile = *eof;
3829
3830 data = &info;
3831 size = sizeof(struct smb2_file_eof_info);
3832
Steve Frenchf29ebb42014-07-19 21:44:58 -05003833 if (is_falloc)
3834 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003835 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3836 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003837 else
3838 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003839 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3840 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003841}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003842
3843int
3844SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3845 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3846{
3847 unsigned int size;
3848 size = sizeof(FILE_BASIC_INFO);
3849 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003850 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3851 0, 1, (void **)&buf, &size);
3852}
3853
3854int
3855SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3856 u64 persistent_fid, u64 volatile_fid,
3857 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3858{
3859 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3860 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3861 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003862}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003863
3864int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003865SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3866 u64 persistent_fid, u64 volatile_fid,
3867 struct smb2_file_full_ea_info *buf, int len)
3868{
3869 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3870 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3871 0, 1, (void **)&buf, &len);
3872}
3873
3874int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003875SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3876 const u64 persistent_fid, const u64 volatile_fid,
3877 __u8 oplock_level)
3878{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003879 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003880 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10003881 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003882 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003883 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003884 unsigned int total_len;
3885 struct kvec iov[1];
3886 struct kvec rsp_iov;
3887 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003888
Joe Perchesf96637b2013-05-04 22:12:25 -05003889 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003890 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3891 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003892 if (rc)
3893 return rc;
3894
Steve French5a77e752018-05-09 17:43:08 -05003895 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003896 flags |= CIFS_TRANSFORM_REQ;
3897
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003898 req->VolatileFid = volatile_fid;
3899 req->PersistentFid = persistent_fid;
3900 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003901 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003902
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003903 flags |= CIFS_NO_RESP;
3904
3905 iov[0].iov_base = (char *)req;
3906 iov[0].iov_len = total_len;
3907
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003908 memset(&rqst, 0, sizeof(struct smb_rqst));
3909 rqst.rq_iov = iov;
3910 rqst.rq_nvec = 1;
3911
3912 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003913 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003914
3915 if (rc) {
3916 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003917 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003918 }
3919
3920 return rc;
3921}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003922
3923static void
3924copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3925 struct kstatfs *kst)
3926{
3927 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3928 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3929 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303930 kst->f_bfree = kst->f_bavail =
3931 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003932 return;
3933}
3934
3935static int
3936build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3937 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3938{
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003939 struct TCP_Server_Info *server;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003940 int rc;
3941 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003942 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003943
Joe Perchesf96637b2013-05-04 22:12:25 -05003944 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003945
3946 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3947 return -EIO;
3948
Gustavo A. R. Silvac0953f22018-04-03 16:00:40 -05003949 server = tcon->ses->server;
3950
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003951 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3952 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003953 if (rc)
3954 return rc;
3955
3956 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3957 req->FileInfoClass = level;
3958 req->PersistentFileId = persistent_fid;
3959 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003960 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003961 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003962 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003963 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10003964 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003965
3966 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003967 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003968 return 0;
3969}
3970
3971int
3972SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3973 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3974{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003975 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003976 struct smb2_query_info_rsp *rsp = NULL;
3977 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003978 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003979 int rc = 0;
3980 int resp_buftype;
3981 struct cifs_ses *ses = tcon->ses;
3982 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003983 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003984
3985 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3986 sizeof(struct smb2_fs_full_size_info),
3987 persistent_fid, volatile_fid);
3988 if (rc)
3989 return rc;
3990
Steve French5a77e752018-05-09 17:43:08 -05003991 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003992 flags |= CIFS_TRANSFORM_REQ;
3993
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003994 memset(&rqst, 0, sizeof(struct smb_rqst));
3995 rqst.rq_iov = &iov;
3996 rqst.rq_nvec = 1;
3997
3998 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003999 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004000 if (rc) {
4001 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05004002 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004003 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004004 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004005
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10004006 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004007 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10004008 rc = validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10004009 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004010 sizeof(struct smb2_fs_full_size_info));
4011 if (!rc)
4012 copy_fs_info_to_kstatfs(info, fsdata);
4013
Steve French34f62642013-10-09 02:07:00 -05004014qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004015 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004016 return rc;
4017}
4018
4019int
4020SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05004021 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05004022{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004023 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05004024 struct smb2_query_info_rsp *rsp = NULL;
4025 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004026 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05004027 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05004028 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05004029 struct cifs_ses *ses = tcon->ses;
4030 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004031 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05004032
Steven French21671142013-10-09 13:36:35 -05004033 if (level == FS_DEVICE_INFORMATION) {
4034 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4035 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4036 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4037 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4038 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004039 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4040 max_len = sizeof(struct smb3_fs_ss_info);
4041 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05004042 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004043 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05004044 return -EINVAL;
4045 }
4046
4047 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05004048 persistent_fid, volatile_fid);
4049 if (rc)
4050 return rc;
4051
Steve French5a77e752018-05-09 17:43:08 -05004052 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004053 flags |= CIFS_TRANSFORM_REQ;
4054
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004055 memset(&rqst, 0, sizeof(struct smb_rqst));
4056 rqst.rq_iov = &iov;
4057 rqst.rq_nvec = 1;
4058
4059 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004060 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004061 if (rc) {
4062 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4063 goto qfsattr_exit;
4064 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004065 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05004066
4067 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4068 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10004069 rc = validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05004070 if (rc)
4071 goto qfsattr_exit;
4072
4073 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10004074 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004075 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05004076 rsp_len, max_len));
4077 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10004078 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004079 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004080 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4081 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10004082 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004083 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4084 tcon->perf_sector_size =
4085 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4086 }
Steve French34f62642013-10-09 02:07:00 -05004087
4088qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004089 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004090 return rc;
4091}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004092
4093int
4094smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4095 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4096 const __u32 num_lock, struct smb2_lock_element *buf)
4097{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004098 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004099 int rc = 0;
4100 struct smb2_lock_req *req = NULL;
4101 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004102 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004103 int resp_buf_type;
4104 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004105 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004106 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004107
Joe Perchesf96637b2013-05-04 22:12:25 -05004108 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004109
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004110 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004111 if (rc)
4112 return rc;
4113
Steve French5a77e752018-05-09 17:43:08 -05004114 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004115 flags |= CIFS_TRANSFORM_REQ;
4116
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004117 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004118 req->LockCount = cpu_to_le16(num_lock);
4119
4120 req->PersistentFileId = persist_fid;
4121 req->VolatileFileId = volatile_fid;
4122
4123 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004124
4125 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004126 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004127 iov[1].iov_base = (char *)buf;
4128 iov[1].iov_len = count;
4129
4130 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004131
4132 memset(&rqst, 0, sizeof(struct smb_rqst));
4133 rqst.rq_iov = iov;
4134 rqst.rq_nvec = 2;
4135
4136 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004137 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004138 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004139 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004140 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004141 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004142 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4143 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004144 }
4145
4146 return rc;
4147}
4148
4149int
4150SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4151 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4152 const __u64 length, const __u64 offset, const __u32 lock_flags,
4153 const bool wait)
4154{
4155 struct smb2_lock_element lock;
4156
4157 lock.Offset = cpu_to_le64(offset);
4158 lock.Length = cpu_to_le64(length);
4159 lock.Flags = cpu_to_le32(lock_flags);
4160 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4161 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4162
4163 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4164}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004165
4166int
4167SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4168 __u8 *lease_key, const __le32 lease_state)
4169{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004170 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004171 int rc;
4172 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004173 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004174 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004175 unsigned int total_len;
4176 struct kvec iov[1];
4177 struct kvec rsp_iov;
4178 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004179
Joe Perchesf96637b2013-05-04 22:12:25 -05004180 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004181 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4182 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004183 if (rc)
4184 return rc;
4185
Steve French5a77e752018-05-09 17:43:08 -05004186 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004187 flags |= CIFS_TRANSFORM_REQ;
4188
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004189 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004190 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004191 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004192
4193 memcpy(req->LeaseKey, lease_key, 16);
4194 req->LeaseState = lease_state;
4195
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004196 flags |= CIFS_NO_RESP;
4197
4198 iov[0].iov_base = (char *)req;
4199 iov[0].iov_len = total_len;
4200
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004201 memset(&rqst, 0, sizeof(struct smb_rqst));
4202 rqst.rq_iov = iov;
4203 rqst.rq_nvec = 1;
4204
4205 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004206 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004207
4208 if (rc) {
4209 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004210 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004211 }
4212
4213 return rc;
4214}