blob: 710ceb875161a5a70e67f9af0e95f5b53b7b5284 [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"
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -020053#ifdef CONFIG_CIFS_DFS_UPCALL
54#include "dfs_cache.h"
55#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040056
57/*
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
60 *
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
63 */
64static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
67 /* SMB2_LOGOFF */ 4,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
70 /* SMB2_CREATE */ 57,
71 /* SMB2_CLOSE */ 24,
72 /* SMB2_FLUSH */ 24,
73 /* SMB2_READ */ 49,
74 /* SMB2_WRITE */ 49,
75 /* SMB2_LOCK */ 48,
76 /* SMB2_IOCTL */ 57,
77 /* SMB2_CANCEL */ 4,
78 /* SMB2_ECHO */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
84};
85
Ronnie Sahlberg730928c2018-08-08 15:07:49 +100086int smb3_encryption_required(const struct cifs_tcon *tcon)
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070087{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080088 if (!tcon)
89 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070090 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
91 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
92 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080093 if (tcon->seal &&
94 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
95 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070096 return 0;
97}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040098
99static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700100smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400101 const struct cifs_tcon *tcon)
102{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700103 shdr->ProtocolId = SMB2_PROTO_NUMBER;
104 shdr->StructureSize = cpu_to_le16(64);
105 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
108
109 spin_lock(&server->req_lock);
Steve French69dc4b12019-03-05 21:04:56 -0600110 /* Request up to 10 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500111 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700112 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100113 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700114 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500115 min_t(int, server->max_credits -
Steve French69dc4b12019-03-05 21:04:56 -0600116 server->credits, 10));
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100117 spin_unlock(&server->req_lock);
118 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700119 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100120 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400122
123 if (!tcon)
124 goto out;
125
Steve French2b80d042013-06-23 18:43:37 -0500126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500128 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700130 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500131 /* else CreditCharge MBZ */
132
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700133 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400134 /* Uid is not converted */
135 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700136 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500137
138 /*
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
147 */
148/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500150
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
Steve French5a77e752018-05-09 17:43:08 -0500152 !smb3_encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700153 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400154out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400155 return;
156}
157
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200158#ifdef CONFIG_CIFS_DFS_UPCALL
159static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
161{
162 int rc;
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100165 char *tree;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200166 const char *tcp_host;
167 size_t tcp_host_len;
168 const char *dfs_host;
169 size_t dfs_host_len;
170
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
172 if (!tree)
173 return -ENOMEM;
174
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200175 if (tcon->ipc) {
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000176 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
179 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200180 }
181
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
184 goto out;
185 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200186
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
188 if (rc)
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100189 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200190
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
192 &tcp_host_len);
193
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
196 const char *tgt = dfs_cache_get_tgt_name(it);
197
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
199
200 if (dfs_host_len != tcp_host_len
201 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
202 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
203 __func__,
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
206 continue;
207 }
208
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000209 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200210
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
212 if (!rc)
213 break;
214 if (rc == -EREMOTE)
215 break;
216 }
217
218 if (!rc) {
219 if (it)
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
221 it);
222 else
223 rc = -ENOENT;
224 }
225 dfs_cache_free_tgts(&tl);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100226out:
227 kfree(tree);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200228 return rc;
229}
230#else
231static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
233{
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
235}
236#endif
237
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400238static int
239smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
240{
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300241 int rc;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200245 int retries;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400246
247 /*
248 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
249 * check for tcp and smb session status done differently
250 * for those three - in the calling routine.
251 */
252 if (tcon == NULL)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300253 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400254
255 if (smb2_command == SMB2_TREE_CONNECT)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300256 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400257
258 if (tcon->tidStatus == CifsExiting) {
259 /*
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
263 */
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
268 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400269 return -ENODEV;
270 }
271 }
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
274 return -EIO;
275
276 ses = tcon->ses;
277 server = ses->server;
278
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200279 retries = server->nr_targets;
280
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400281 /*
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200282 * Give demultiplex thread up to 10 seconds to each target available for
283 * reconnect -- should be greater than cifs socket timeout which is 7
284 * seconds.
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400285 */
286 while (server->tcpStatus == CifsNeedReconnect) {
287 /*
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
290 */
291 switch (smb2_command) {
292 /*
293 * BB Should we keep oplock break and add flush to exceptions?
294 */
295 case SMB2_TREE_DISCONNECT:
296 case SMB2_CANCEL:
297 case SMB2_CLOSE:
298 case SMB2_OPLOCK_BREAK:
299 return -EAGAIN;
300 }
301
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
304 10 * HZ);
305 if (rc < 0) {
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
308 return -ERESTARTSYS;
309 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400310
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
313 break;
314
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200315 if (--retries)
316 continue;
317
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400318 /*
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
321 * back on-line
322 */
323 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400325 return -EHOSTDOWN;
326 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200327 retries = server->nr_targets;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400328 }
329
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300331 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400332
333 nls_codepage = load_nls_default();
334
335 /*
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
338 */
339 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200340
341 /*
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
345 */
346 if (server->tcpStatus == CifsNeedReconnect) {
347 rc = -EHOSTDOWN;
348 mutex_unlock(&tcon->ses->session_mutex);
349 goto out;
350 }
351
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400352 rc = cifs_negotiate_protocol(0, tcon->ses);
353 if (!rc && tcon->ses->need_reconnect)
354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
355
356 if (rc || !tcon->need_reconnect) {
357 mutex_unlock(&tcon->ses->session_mutex);
358 goto out;
359 }
360
361 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800362 if (tcon->use_persistent)
363 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500364
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200365 rc = __smb2_reconnect(nls_codepage, tcon);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400366 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500367
Joe Perchesf96637b2013-05-04 22:12:25 -0500368 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500369 if (rc) {
370 /* If sess reconnected but tcon didn't, something strange ... */
371 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400372 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500373 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800374
375 if (smb2_command != SMB2_INTERNAL_CMD)
376 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
377
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400378 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400379out:
380 /*
381 * Check if handle based operation so we know whether we can continue
382 * or not without returning to caller to reset file handle.
383 */
384 /*
385 * BB Is flush done by server on drop of tcp session? Should we special
386 * case it and skip above?
387 */
388 switch (smb2_command) {
389 case SMB2_FLUSH:
390 case SMB2_READ:
391 case SMB2_WRITE:
392 case SMB2_LOCK:
393 case SMB2_IOCTL:
394 case SMB2_QUERY_DIRECTORY:
395 case SMB2_CHANGE_NOTIFY:
396 case SMB2_QUERY_INFO:
397 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800398 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400399 }
400 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400401 return rc;
402}
403
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700404static void
405fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
406 unsigned int *total_len)
407{
408 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
409 /* lookup word count ie StructureSize from table */
410 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
411
412 /*
413 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
414 * largest operations (Create)
415 */
416 memset(buf, 0, 256);
417
418 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
419 spdu->StructureSize2 = cpu_to_le16(parmsize);
420
421 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
422}
423
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100424/*
425 * Allocate and return pointer to an SMB request hdr, and set basic
426 * SMB information in the SMB header. If the return code is zero, this
427 * function must have filled in request_buf pointer.
428 */
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800429static int
430smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
431 void **request_buf, unsigned int *total_len)
432{
433 int rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800434
435 rc = smb2_reconnect(smb2_command, tcon);
436 if (rc)
437 return rc;
438
439 /* BB eventually switch this to SMB2 specific small buf size */
Stefano Briviof46ecbd2018-07-05 11:46:42 +0200440 if (smb2_command == SMB2_SET_INFO)
441 *request_buf = cifs_buf_get();
442 else
443 *request_buf = cifs_small_buf_get();
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800444 if (*request_buf == NULL) {
445 /* BB should we add a retry in here if not a writepage? */
446 return -ENOMEM;
447 }
448
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100449 fill_small_buf(smb2_command, tcon,
450 (struct smb2_sync_hdr *)(*request_buf),
451 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400452
453 if (tcon != NULL) {
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400454 uint16_t com_code = le16_to_cpu(smb2_command);
455 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400456 cifs_stats_inc(&tcon->num_smbs_sent);
457 }
458
459 return rc;
460}
461
Steve Frenchd7bef4c2019-04-18 11:03:58 -0500462/* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500463
464static void
465build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
466{
467 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
468 pneg_ctxt->DataLength = cpu_to_le16(38);
469 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
470 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
471 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
472 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
473}
474
475static void
Steve French26ea8882019-04-26 20:36:08 -0700476build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
477{
478 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
479 pneg_ctxt->DataLength =
480 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
481 - sizeof(struct smb2_neg_context));
482 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
483 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
484 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
485 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
486}
487
488static void
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500489build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
490{
491 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French23657ad2018-04-22 15:14:58 -0500492 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
493 pneg_ctxt->CipherCount = cpu_to_le16(1);
494/* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
495 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500496}
497
498static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500499build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
500{
501 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
502 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
Steve French0d481322019-02-24 17:56:33 -0600503 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
504 pneg_ctxt->Name[0] = 0x93;
505 pneg_ctxt->Name[1] = 0xAD;
506 pneg_ctxt->Name[2] = 0x25;
507 pneg_ctxt->Name[3] = 0x50;
508 pneg_ctxt->Name[4] = 0x9C;
509 pneg_ctxt->Name[5] = 0xB4;
510 pneg_ctxt->Name[6] = 0x11;
511 pneg_ctxt->Name[7] = 0xE7;
512 pneg_ctxt->Name[8] = 0xB4;
513 pneg_ctxt->Name[9] = 0x23;
514 pneg_ctxt->Name[10] = 0x83;
515 pneg_ctxt->Name[11] = 0xDE;
516 pneg_ctxt->Name[12] = 0x96;
517 pneg_ctxt->Name[13] = 0x8B;
518 pneg_ctxt->Name[14] = 0xCD;
519 pneg_ctxt->Name[15] = 0x7C;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500520}
521
522static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100523assemble_neg_contexts(struct smb2_negotiate_req *req,
524 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500525{
Steve Frenchd5c70762019-01-03 02:37:21 -0600526 char *pneg_ctxt = (char *)req;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500527 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500528
Steve Frenchd5c70762019-01-03 02:37:21 -0600529 if (*total_len > 200) {
530 /* In case length corrupted don't want to overrun smb buffer */
531 cifs_dbg(VFS, "Bad frame length assembling neg contexts\n");
532 return;
533 }
534
535 /*
536 * round up total_len of fixed part of SMB3 negotiate request to 8
537 * byte boundary before adding negotiate contexts
538 */
539 *total_len = roundup(*total_len, 8);
540
541 pneg_ctxt = (*total_len) + (char *)req;
542 req->NegotiateContextOffset = cpu_to_le32(*total_len);
543
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500544 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500545 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
546 *total_len += ctxt_len;
547 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100548
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500549 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500550 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
551 *total_len += ctxt_len;
552 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100553
Steve French26ea8882019-04-26 20:36:08 -0700554 build_compression_ctxt((struct smb2_compression_capabilities_context *)
555 pneg_ctxt);
556 ctxt_len = DIV_ROUND_UP(
557 sizeof(struct smb2_compression_capabilities_context), 8) * 8;
558 *total_len += ctxt_len;
559 pneg_ctxt += ctxt_len;
560
Steve Frenchfcef0db2018-05-19 20:45:27 -0500561 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
562 *total_len += sizeof(struct smb2_posix_neg_context);
563
Steve French26ea8882019-04-26 20:36:08 -0700564 req->NegotiateContextCount = cpu_to_le16(4);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500565}
Steve French5100d8a2018-04-09 10:47:14 -0500566
567static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
568{
569 unsigned int len = le16_to_cpu(ctxt->DataLength);
570
571 /* If invalid preauth context warn but use what we requested, SHA-512 */
572 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
573 printk_once(KERN_WARNING "server sent bad preauth context\n");
574 return;
575 }
576 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
577 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
578 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
579 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
580}
581
Steve French26ea8882019-04-26 20:36:08 -0700582static void decode_compress_ctx(struct TCP_Server_Info *server,
583 struct smb2_compression_capabilities_context *ctxt)
584{
585 unsigned int len = le16_to_cpu(ctxt->DataLength);
586
587 /* sizeof compress context is a one element compression capbility struct */
588 if (len < 10) {
589 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
590 return;
591 }
592 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
593 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
594 return;
595 }
596 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
597 printk_once(KERN_WARNING "unknown compression algorithm\n");
598 return;
599 }
600 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
601}
602
Steve French5100d8a2018-04-09 10:47:14 -0500603static int decode_encrypt_ctx(struct TCP_Server_Info *server,
604 struct smb2_encryption_neg_context *ctxt)
605{
606 unsigned int len = le16_to_cpu(ctxt->DataLength);
607
608 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
609 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
610 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
611 return -EINVAL;
612 }
613
614 if (le16_to_cpu(ctxt->CipherCount) != 1) {
615 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
616 return -EINVAL;
617 }
618 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
619 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
620 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
621 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
622 return -EINVAL;
623 }
624 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500625 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500626 return 0;
627}
628
629static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000630 struct TCP_Server_Info *server,
631 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500632{
633 struct smb2_neg_context *pctx;
634 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
635 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500636 unsigned int len_of_ctxts, i;
637 int rc = 0;
638
639 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
640 if (len_of_smb <= offset) {
641 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
642 return -EINVAL;
643 }
644
645 len_of_ctxts = len_of_smb - offset;
646
647 for (i = 0; i < ctxt_cnt; i++) {
648 int clen;
649 /* check that offset is not beyond end of SMB */
650 if (len_of_ctxts == 0)
651 break;
652
653 if (len_of_ctxts < sizeof(struct smb2_neg_context))
654 break;
655
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000656 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500657 clen = le16_to_cpu(pctx->DataLength);
658 if (clen > len_of_ctxts)
659 break;
660
661 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
662 decode_preauth_context(
663 (struct smb2_preauth_neg_context *)pctx);
664 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
665 rc = decode_encrypt_ctx(server,
666 (struct smb2_encryption_neg_context *)pctx);
Steve French26ea8882019-04-26 20:36:08 -0700667 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
668 decode_compress_ctx(server,
669 (struct smb2_compression_capabilities_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500670 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
671 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500672 else
673 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
674 le16_to_cpu(pctx->ContextType));
675
676 if (rc)
677 break;
678 /* offsets must be 8 byte aligned */
679 clen = (clen + 7) & ~0x7;
680 offset += clen + sizeof(struct smb2_neg_context);
681 len_of_ctxts -= clen;
682 }
683 return rc;
684}
685
Steve Frenchce558b02018-05-31 19:16:54 -0500686static struct create_posix *
687create_posix_buf(umode_t mode)
688{
689 struct create_posix *buf;
690
691 buf = kzalloc(sizeof(struct create_posix),
692 GFP_KERNEL);
693 if (!buf)
694 return NULL;
695
696 buf->ccontext.DataOffset =
697 cpu_to_le16(offsetof(struct create_posix, Mode));
698 buf->ccontext.DataLength = cpu_to_le32(4);
699 buf->ccontext.NameOffset =
700 cpu_to_le16(offsetof(struct create_posix, Name));
701 buf->ccontext.NameLength = cpu_to_le16(16);
702
703 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
704 buf->Name[0] = 0x93;
705 buf->Name[1] = 0xAD;
706 buf->Name[2] = 0x25;
707 buf->Name[3] = 0x50;
708 buf->Name[4] = 0x9C;
709 buf->Name[5] = 0xB4;
710 buf->Name[6] = 0x11;
711 buf->Name[7] = 0xE7;
712 buf->Name[8] = 0xB4;
713 buf->Name[9] = 0x23;
714 buf->Name[10] = 0x83;
715 buf->Name[11] = 0xDE;
716 buf->Name[12] = 0x96;
717 buf->Name[13] = 0x8B;
718 buf->Name[14] = 0xCD;
719 buf->Name[15] = 0x7C;
720 buf->Mode = cpu_to_le32(mode);
721 cifs_dbg(FYI, "mode on posix create 0%o", mode);
722 return buf;
723}
724
725static int
726add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
727{
728 struct smb2_create_req *req = iov[0].iov_base;
729 unsigned int num = *num_iovec;
730
731 iov[num].iov_base = create_posix_buf(mode);
732 if (iov[num].iov_base == NULL)
733 return -ENOMEM;
734 iov[num].iov_len = sizeof(struct create_posix);
735 if (!req->CreateContextsOffset)
736 req->CreateContextsOffset = cpu_to_le32(
737 sizeof(struct smb2_create_req) +
738 iov[num - 1].iov_len);
739 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
740 *num_iovec = num + 1;
741 return 0;
742}
743
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500744
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400745/*
746 *
747 * SMB2 Worker functions follow:
748 *
749 * The general structure of the worker functions is:
750 * 1) Call smb2_init (assembles SMB2 header)
751 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
752 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
753 * 4) Decode SMB2 command specific fields in the fixed length area
754 * 5) Decode variable length data area (if any for this SMB2 command type)
755 * 6) Call free smb buffer
756 * 7) return
757 *
758 */
759
760int
761SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
762{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000763 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400764 struct smb2_negotiate_req *req;
765 struct smb2_negotiate_rsp *rsp;
766 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700767 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400768 int rc = 0;
769 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400770 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400771 int blob_offset, blob_length;
772 char *security_blob;
773 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100774 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400775
Joe Perchesf96637b2013-05-04 22:12:25 -0500776 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400777
Jeff Layton3534b852013-05-24 07:41:01 -0400778 if (!server) {
779 WARN(1, "%s: server is NULL!\n", __func__);
780 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400781 }
782
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100783 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400784 if (rc)
785 return rc;
786
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100787 req->sync_hdr.SessionId = 0;
Steve French0fdfef92018-06-28 19:30:23 -0500788
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100789 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
790 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400791
Steve French9764c022017-09-17 10:41:35 -0500792 if (strcmp(ses->server->vals->version_string,
793 SMB3ANY_VERSION_STRING) == 0) {
794 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
795 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
796 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100797 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500798 } else if (strcmp(ses->server->vals->version_string,
799 SMBDEFAULT_VERSION_STRING) == 0) {
800 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
801 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
802 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600803 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
804 req->DialectCount = cpu_to_le16(4);
805 total_len += 8;
Steve French9764c022017-09-17 10:41:35 -0500806 } else {
807 /* otherwise send specific dialect */
808 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
809 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100810 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500811 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400812
813 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400814 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500815 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400816 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500817 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400818 else
819 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400820
Steve Frenche4aa25e2012-10-01 12:26:22 -0500821 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400822
Steve French3c5f9be12014-05-13 13:37:45 -0700823 /* ClientGUID must be zero for SMB2.02 dialect */
824 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
825 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500826 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700827 memcpy(req->ClientGUID, server->client_guid,
828 SMB2_CLIENT_GUID_SIZE);
Steve Frenchd5c70762019-01-03 02:37:21 -0600829 if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
830 (strcmp(ses->server->vals->version_string,
831 SMBDEFAULT_VERSION_STRING) == 0))
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100832 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500833 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400834 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100835 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400836
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000837 memset(&rqst, 0, sizeof(struct smb_rqst));
838 rqst.rq_iov = iov;
839 rqst.rq_nvec = 1;
840
841 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700842 cifs_small_buf_release(req);
843 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400844 /*
845 * No tcon so can't do
846 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
847 */
Steve French7e682f72017-08-31 21:34:24 -0500848 if (rc == -EOPNOTSUPP) {
849 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500850 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500851 " older servers\n");
852 goto neg_exit;
853 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400854 goto neg_exit;
855
Steve French9764c022017-09-17 10:41:35 -0500856 if (strcmp(ses->server->vals->version_string,
857 SMB3ANY_VERSION_STRING) == 0) {
858 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
859 cifs_dbg(VFS,
860 "SMB2 dialect returned but not requested\n");
861 return -EIO;
862 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
863 cifs_dbg(VFS,
864 "SMB2.1 dialect returned but not requested\n");
865 return -EIO;
866 }
867 } else if (strcmp(ses->server->vals->version_string,
868 SMBDEFAULT_VERSION_STRING) == 0) {
869 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
870 cifs_dbg(VFS,
871 "SMB2 dialect returned but not requested\n");
872 return -EIO;
873 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
874 /* ops set to 3.0 by default for default so update */
875 ses->server->ops = &smb21_operations;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800876 ses->server->vals = &smb21_values;
877 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
Steve Frenchd5c70762019-01-03 02:37:21 -0600878 ses->server->ops = &smb311_operations;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800879 ses->server->vals = &smb311_values;
880 }
Steve French590d08d2017-09-19 11:43:47 -0500881 } else if (le16_to_cpu(rsp->DialectRevision) !=
882 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500883 /* if requested single dialect ensure returned dialect matched */
884 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500885 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500886 return -EIO;
887 }
888
Joe Perchesf96637b2013-05-04 22:12:25 -0500889 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400890
Steve Frenche4aa25e2012-10-01 12:26:22 -0500891 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500892 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500893 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500894 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500895 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500896 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500897 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
898 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600899 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
900 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400901 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500902 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500903 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400904 rc = -EIO;
905 goto neg_exit;
906 }
907 server->dialect = le16_to_cpu(rsp->DialectRevision);
908
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100909 /*
910 * Keep a copy of the hash after negprot. This hash will be
911 * the starting hash value for all sessions made from this
912 * server.
913 */
914 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
915 SMB2_PREAUTH_HASH_SIZE);
Steve French0fdfef92018-06-28 19:30:23 -0500916
Jeff Laytone598d1d82013-05-26 07:00:59 -0400917 /* SMB2 only has an extended negflavor */
918 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400919 /* set it to the maximum buffer size value we can send with 1 credit */
920 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
921 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400922 server->max_read = le32_to_cpu(rsp->MaxReadSize);
923 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400924 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500925 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
926 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
927 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400928 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400929 /* Internal types */
930 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400931
932 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000933 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500934 /*
935 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
936 * for us will be
937 * ses->sectype = RawNTLMSSP;
938 * but for time being this is our only auth choice so doesn't matter.
939 * We just found a server which sets blob length to zero expecting raw.
940 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700941 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500942 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700943 server->sec_ntlmssp = true;
944 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700945
Jeff Layton38d77c52013-05-26 07:01:00 -0400946 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400947 if (rc)
948 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500949 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500950 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500951 if (rc == 1)
952 rc = 0;
953 else if (rc == 0)
954 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400955 }
Steve French5100d8a2018-04-09 10:47:14 -0500956
Steve French5100d8a2018-04-09 10:47:14 -0500957 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
958 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000959 rc = smb311_decode_neg_context(rsp, server,
960 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -0500961 else
962 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
963 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400964neg_exit:
965 free_rsp_buf(resp_buftype, rsp);
966 return rc;
967}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400968
Steve Frenchff1c0382013-11-19 23:44:46 -0600969int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
970{
Long Li2796d302018-04-25 11:30:04 -0700971 int rc;
972 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200973 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600974 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500975 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600976
977 cifs_dbg(FYI, "validate negotiate\n");
978
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100979 /* In SMB3.11 preauth integrity supersedes validate negotiate */
980 if (tcon->ses->server->dialect == SMB311_PROT_ID)
981 return 0;
982
Steve Frenchff1c0382013-11-19 23:44:46 -0600983 /*
984 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500985 * can not sign it (ie are not known user). Even if signing is not
986 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600987 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500988 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600989 */
Steve French0603c962017-09-20 19:57:18 -0500990 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600991 return 0; /* validation requires signing */
992
Steve French0603c962017-09-20 19:57:18 -0500993 if (tcon->ses->user_name == NULL) {
994 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
995 return 0; /* validation requires signing */
996 }
997
998 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
999 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1000
Long Li2796d302018-04-25 11:30:04 -07001001 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1002 if (!pneg_inbuf)
1003 return -ENOMEM;
1004
1005 pneg_inbuf->Capabilities =
Steve Frenchff1c0382013-11-19 23:44:46 -06001006 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Long Li2796d302018-04-25 11:30:04 -07001007 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +01001008 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -06001009
1010 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -07001011 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001012 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1013 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -07001014 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001015 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1016 else
Long Li2796d302018-04-25 11:30:04 -07001017 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001018
Steve French9764c022017-09-17 10:41:35 -05001019
1020 if (strcmp(tcon->ses->server->vals->version_string,
1021 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001022 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1023 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1024 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -05001025 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -07001026 inbuflen = sizeof(*pneg_inbuf) -
Steve Frenchd5c70762019-01-03 02:37:21 -06001027 (2 * sizeof(pneg_inbuf->Dialects[0]));
Steve French9764c022017-09-17 10:41:35 -05001028 } else if (strcmp(tcon->ses->server->vals->version_string,
1029 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001030 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1031 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1032 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -06001033 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1034 pneg_inbuf->DialectCount = cpu_to_le16(4);
Steve French9764c022017-09-17 10:41:35 -05001035 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -07001036 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -05001037 } else {
1038 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -07001039 pneg_inbuf->Dialects[0] =
Steve French9764c022017-09-17 10:41:35 -05001040 cpu_to_le16(tcon->ses->server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -07001041 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -05001042 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -07001043 inbuflen = sizeof(*pneg_inbuf) -
1044 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -05001045 }
Steve Frenchff1c0382013-11-19 23:44:46 -06001046
1047 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1048 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001049 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1050 (char **)&pneg_rsp, &rsplen);
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001051 if (rc == -EOPNOTSUPP) {
1052 /*
1053 * Old Windows versions or Netapp SMB server can return
1054 * not supported error. Client should accept it.
1055 */
1056 cifs_dbg(VFS, "Server does not support validate negotiate\n");
1057 return 0;
1058 } else if (rc != 0) {
Steve Frenchff1c0382013-11-19 23:44:46 -06001059 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -07001060 rc = -EIO;
1061 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -06001062 }
1063
Long Li2796d302018-04-25 11:30:04 -07001064 rc = -EIO;
1065 if (rsplen != sizeof(*pneg_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -05001066 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1067 rsplen);
1068
1069 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -07001070 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1071 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001072 }
1073
1074 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +01001075 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -06001076 goto vneg_out;
1077
1078 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
1079 goto vneg_out;
1080
1081 /* do not validate server guid because not saved at negprot time yet */
1082
1083 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1084 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
1085 goto vneg_out;
1086
1087 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -07001088 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001089 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -07001090 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001091
1092vneg_out:
1093 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -07001094out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001095 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -07001096out_free_inbuf:
1097 kfree(pneg_inbuf);
1098 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -06001099}
1100
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301101enum securityEnum
1102smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1103{
1104 switch (requested) {
1105 case Kerberos:
1106 case RawNTLMSSP:
1107 return requested;
1108 case NTLMv2:
1109 return RawNTLMSSP;
1110 case Unspecified:
1111 if (server->sec_ntlmssp &&
1112 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1113 return RawNTLMSSP;
1114 if ((server->sec_kerberos || server->sec_mskerberos) &&
1115 (global_secflags & CIFSSEC_MAY_KRB5))
1116 return Kerberos;
1117 /* Fallthrough */
1118 default:
1119 return Unspecified;
1120 }
1121}
1122
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001123struct SMB2_sess_data {
1124 unsigned int xid;
1125 struct cifs_ses *ses;
1126 struct nls_table *nls_cp;
1127 void (*func)(struct SMB2_sess_data *);
1128 int result;
1129 u64 previous_session;
1130
1131 /* we will send the SMB in three pieces:
1132 * a fixed length beginning part, an optional
1133 * SPNEGO blob (which can be zero length), and a
1134 * last part which will include the strings
1135 * and rest of bcc area. This allows us to avoid
1136 * a large buffer 17K allocation
1137 */
1138 int buf0_type;
1139 struct kvec iov[2];
1140};
1141
1142static int
1143SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1144{
1145 int rc;
1146 struct cifs_ses *ses = sess_data->ses;
1147 struct smb2_sess_setup_req *req;
1148 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001149 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001150
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001151 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1152 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001153 if (rc)
1154 return rc;
1155
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001156 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001157 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001158
1159 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1160 req->PreviousSessionId = sess_data->previous_session;
1161
1162 req->Flags = 0; /* MBZ */
Steve Frenchd4090142018-06-13 17:05:58 -05001163
1164 /* enough to enable echos and oplocks and one max size write */
1165 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001166
1167 /* only one of SMB2 signing flags may be set in SMB2 request */
1168 if (server->sign)
1169 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1170 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1171 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1172 else
1173 req->SecurityMode = 0;
1174
1175 req->Capabilities = 0;
1176 req->Channel = 0; /* MBZ */
1177
1178 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001179 /* 1 for pad */
1180 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001181 /*
1182 * This variable will be used to clear the buffer
1183 * allocated above in case of any error in the calling function.
1184 */
1185 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1186
1187 return 0;
1188}
1189
1190static void
1191SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1192{
1193 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1194 sess_data->buf0_type = CIFS_NO_BUFFER;
1195}
1196
1197static int
1198SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1199{
1200 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001201 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001202 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001203 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001204
1205 /* Testing shows that buffer offset must be at location of Buffer[0] */
1206 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001207 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001208 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1209
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001210 memset(&rqst, 0, sizeof(struct smb_rqst));
1211 rqst.rq_iov = sess_data->iov;
1212 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001213
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001214 /* BB add code to build os and lm fields */
1215 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1216 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001217 &sess_data->buf0_type,
1218 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001219 cifs_small_buf_release(sess_data->iov[0].iov_base);
1220 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001221
1222 return rc;
1223}
1224
1225static int
1226SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1227{
1228 int rc = 0;
1229 struct cifs_ses *ses = sess_data->ses;
1230
1231 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001232 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001233 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001234 if (rc) {
1235 cifs_dbg(FYI,
1236 "SMB3 session key generation failed\n");
1237 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001238 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001239 }
1240 }
1241 if (!ses->server->session_estab) {
1242 ses->server->sequence_number = 0x2;
1243 ses->server->session_estab = true;
1244 }
1245 mutex_unlock(&ses->server->srv_mutex);
1246
1247 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1248 spin_lock(&GlobalMid_Lock);
1249 ses->status = CifsGood;
1250 ses->need_reconnect = false;
1251 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001252 return rc;
1253}
1254
1255#ifdef CONFIG_CIFS_UPCALL
1256static void
1257SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1258{
1259 int rc;
1260 struct cifs_ses *ses = sess_data->ses;
1261 struct cifs_spnego_msg *msg;
1262 struct key *spnego_key = NULL;
1263 struct smb2_sess_setup_rsp *rsp = NULL;
1264
1265 rc = SMB2_sess_alloc_buffer(sess_data);
1266 if (rc)
1267 goto out;
1268
1269 spnego_key = cifs_get_spnego_key(ses);
1270 if (IS_ERR(spnego_key)) {
1271 rc = PTR_ERR(spnego_key);
1272 spnego_key = NULL;
1273 goto out;
1274 }
1275
1276 msg = spnego_key->payload.data[0];
1277 /*
1278 * check version field to make sure that cifs.upcall is
1279 * sending us a response in an expected form
1280 */
1281 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1282 cifs_dbg(VFS,
1283 "bad cifs.upcall version. Expected %d got %d",
1284 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1285 rc = -EKEYREJECTED;
1286 goto out_put_spnego_key;
1287 }
1288
1289 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1290 GFP_KERNEL);
1291 if (!ses->auth_key.response) {
1292 cifs_dbg(VFS,
1293 "Kerberos can't allocate (%u bytes) memory",
1294 msg->sesskey_len);
1295 rc = -ENOMEM;
1296 goto out_put_spnego_key;
1297 }
1298 ses->auth_key.len = msg->sesskey_len;
1299
1300 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1301 sess_data->iov[1].iov_len = msg->secblob_len;
1302
1303 rc = SMB2_sess_sendreceive(sess_data);
1304 if (rc)
1305 goto out_put_spnego_key;
1306
1307 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001308 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001309
1310 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001311
1312 rc = SMB2_sess_establish_session(sess_data);
1313out_put_spnego_key:
1314 key_invalidate(spnego_key);
1315 key_put(spnego_key);
1316out:
1317 sess_data->result = rc;
1318 sess_data->func = NULL;
1319 SMB2_sess_free_buffer(sess_data);
1320}
1321#else
1322static void
1323SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1324{
1325 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1326 sess_data->result = -EOPNOTSUPP;
1327 sess_data->func = NULL;
1328}
1329#endif
1330
Sachin Prabhu166cea42016-10-07 19:11:22 +01001331static void
1332SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1333
1334static void
1335SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1336{
1337 int rc;
1338 struct cifs_ses *ses = sess_data->ses;
1339 struct smb2_sess_setup_rsp *rsp = NULL;
1340 char *ntlmssp_blob = NULL;
1341 bool use_spnego = false; /* else use raw ntlmssp */
1342 u16 blob_length = 0;
1343
1344 /*
1345 * If memory allocation is successful, caller of this function
1346 * frees it.
1347 */
1348 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1349 if (!ses->ntlmssp) {
1350 rc = -ENOMEM;
1351 goto out_err;
1352 }
1353 ses->ntlmssp->sesskey_per_smbsess = true;
1354
1355 rc = SMB2_sess_alloc_buffer(sess_data);
1356 if (rc)
1357 goto out_err;
1358
1359 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1360 GFP_KERNEL);
1361 if (ntlmssp_blob == NULL) {
1362 rc = -ENOMEM;
1363 goto out;
1364 }
1365
1366 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1367 if (use_spnego) {
1368 /* BB eventually need to add this */
1369 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1370 rc = -EOPNOTSUPP;
1371 goto out;
1372 } else {
1373 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1374 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1375 }
1376 sess_data->iov[1].iov_base = ntlmssp_blob;
1377 sess_data->iov[1].iov_len = blob_length;
1378
1379 rc = SMB2_sess_sendreceive(sess_data);
1380 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1381
1382 /* If true, rc here is expected and not an error */
1383 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001384 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001385 rc = 0;
1386
1387 if (rc)
1388 goto out;
1389
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001390 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001391 le16_to_cpu(rsp->SecurityBufferOffset)) {
1392 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1393 le16_to_cpu(rsp->SecurityBufferOffset));
1394 rc = -EIO;
1395 goto out;
1396 }
1397 rc = decode_ntlmssp_challenge(rsp->Buffer,
1398 le16_to_cpu(rsp->SecurityBufferLength), ses);
1399 if (rc)
1400 goto out;
1401
1402 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1403
1404
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001405 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001406 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001407
1408out:
1409 kfree(ntlmssp_blob);
1410 SMB2_sess_free_buffer(sess_data);
1411 if (!rc) {
1412 sess_data->result = 0;
1413 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1414 return;
1415 }
1416out_err:
1417 kfree(ses->ntlmssp);
1418 ses->ntlmssp = NULL;
1419 sess_data->result = rc;
1420 sess_data->func = NULL;
1421}
1422
1423static void
1424SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1425{
1426 int rc;
1427 struct cifs_ses *ses = sess_data->ses;
1428 struct smb2_sess_setup_req *req;
1429 struct smb2_sess_setup_rsp *rsp = NULL;
1430 unsigned char *ntlmssp_blob = NULL;
1431 bool use_spnego = false; /* else use raw ntlmssp */
1432 u16 blob_length = 0;
1433
1434 rc = SMB2_sess_alloc_buffer(sess_data);
1435 if (rc)
1436 goto out;
1437
1438 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001439 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001440
1441 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1442 sess_data->nls_cp);
1443 if (rc) {
1444 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1445 goto out;
1446 }
1447
1448 if (use_spnego) {
1449 /* BB eventually need to add this */
1450 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1451 rc = -EOPNOTSUPP;
1452 goto out;
1453 }
1454 sess_data->iov[1].iov_base = ntlmssp_blob;
1455 sess_data->iov[1].iov_len = blob_length;
1456
1457 rc = SMB2_sess_sendreceive(sess_data);
1458 if (rc)
1459 goto out;
1460
1461 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1462
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001463 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001464 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001465
1466 rc = SMB2_sess_establish_session(sess_data);
1467out:
1468 kfree(ntlmssp_blob);
1469 SMB2_sess_free_buffer(sess_data);
1470 kfree(ses->ntlmssp);
1471 ses->ntlmssp = NULL;
1472 sess_data->result = rc;
1473 sess_data->func = NULL;
1474}
1475
1476static int
1477SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1478{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301479 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001480
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301481 type = smb2_select_sectype(ses->server, ses->sectype);
1482 cifs_dbg(FYI, "sess setup type %d\n", type);
1483 if (type == Unspecified) {
1484 cifs_dbg(VFS,
1485 "Unable to select appropriate authentication method!");
1486 return -EINVAL;
1487 }
1488
1489 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001490 case Kerberos:
1491 sess_data->func = SMB2_auth_kerberos;
1492 break;
1493 case RawNTLMSSP:
1494 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1495 break;
1496 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301497 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001498 return -EOPNOTSUPP;
1499 }
1500
1501 return 0;
1502}
1503
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001504int
1505SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1506 const struct nls_table *nls_cp)
1507{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001508 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001509 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001510 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001511
Joe Perchesf96637b2013-05-04 22:12:25 -05001512 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001513
Jeff Layton3534b852013-05-24 07:41:01 -04001514 if (!server) {
1515 WARN(1, "%s: server is NULL!\n", __func__);
1516 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001517 }
1518
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001519 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1520 if (!sess_data)
1521 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001522
1523 rc = SMB2_select_sec(ses, sess_data);
1524 if (rc)
1525 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001526 sess_data->xid = xid;
1527 sess_data->ses = ses;
1528 sess_data->buf0_type = CIFS_NO_BUFFER;
1529 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001530 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001531
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001532 /*
1533 * Initialize the session hash with the server one.
1534 */
1535 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1536 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001537
Sachin Prabhu166cea42016-10-07 19:11:22 +01001538 while (sess_data->func)
1539 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001540
Steve Frenchc721c382017-09-19 18:40:03 -05001541 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1542 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001543 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001544out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001545 kfree(sess_data);
1546 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001547}
1548
1549int
1550SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1551{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001552 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001553 struct smb2_logoff_req *req; /* response is also trivial struct */
1554 int rc = 0;
1555 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001556 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001557 unsigned int total_len;
1558 struct kvec iov[1];
1559 struct kvec rsp_iov;
1560 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001561
Joe Perchesf96637b2013-05-04 22:12:25 -05001562 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001563
1564 if (ses && (ses->server))
1565 server = ses->server;
1566 else
1567 return -EIO;
1568
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001569 /* no need to send SMB logoff if uid already closed due to reconnect */
1570 if (ses->need_reconnect)
1571 goto smb2_session_already_dead;
1572
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001573 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001574 if (rc)
1575 return rc;
1576
1577 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001578 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001579
1580 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1581 flags |= CIFS_TRANSFORM_REQ;
1582 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001583 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001584
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001585 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001586
1587 iov[0].iov_base = (char *)req;
1588 iov[0].iov_len = total_len;
1589
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001590 memset(&rqst, 0, sizeof(struct smb_rqst));
1591 rqst.rq_iov = iov;
1592 rqst.rq_nvec = 1;
1593
1594 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001595 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001596 /*
1597 * No tcon so can't do
1598 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1599 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001600
1601smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001602 return rc;
1603}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001604
1605static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1606{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001607 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001608}
1609
1610#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1611
Steve Frenchde9f68df2013-11-15 11:26:24 -06001612/* These are similar values to what Windows uses */
1613static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1614{
1615 tcon->max_chunks = 256;
1616 tcon->max_bytes_chunk = 1048576;
1617 tcon->max_bytes_copy = 16777216;
1618}
1619
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001620int
1621SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1622 struct cifs_tcon *tcon, const struct nls_table *cp)
1623{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001624 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001625 struct smb2_tree_connect_req *req;
1626 struct smb2_tree_connect_rsp *rsp = NULL;
1627 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001628 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001629 int rc = 0;
1630 int resp_buftype;
1631 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001632 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001633 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001634 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001635
Joe Perchesf96637b2013-05-04 22:12:25 -05001636 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001637
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001638 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001639 return -EIO;
1640
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001641 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1642 if (unc_path == NULL)
1643 return -ENOMEM;
1644
1645 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1646 unc_path_len *= 2;
1647 if (unc_path_len < 2) {
1648 kfree(unc_path);
1649 return -EINVAL;
1650 }
1651
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001652 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001653 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001654 atomic_set(&tcon->num_remote_opens, 0);
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001655 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1656 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001657 if (rc) {
1658 kfree(unc_path);
1659 return rc;
1660 }
1661
Steve French5a77e752018-05-09 17:43:08 -05001662 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001663 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001664
1665 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001666 /* 1 for pad */
1667 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001668
1669 /* Testing shows that buffer offset must be at location of Buffer[0] */
1670 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001671 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001672 req->PathLength = cpu_to_le16(unc_path_len - 2);
1673 iov[1].iov_base = unc_path;
1674 iov[1].iov_len = unc_path_len;
1675
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001676 /*
1677 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1678 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
Steve French8c11a602019-03-22 22:31:17 -05001679 * (Samba servers don't always set the flag so also check if null user)
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001680 */
Steve French6188f282018-03-13 02:29:36 -05001681 if ((ses->server->dialect == SMB311_PROT_ID) &&
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001682 !smb3_encryption_required(tcon) &&
Steve French8c11a602019-03-22 22:31:17 -05001683 !(ses->session_flags &
1684 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1685 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
Steve French6188f282018-03-13 02:29:36 -05001686 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1687
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001688 memset(&rqst, 0, sizeof(struct smb_rqst));
1689 rqst.rq_iov = iov;
1690 rqst.rq_nvec = 2;
1691
Steve French4fe75c42019-02-14 01:19:02 -06001692 /* Need 64 for max size write so ask for more in case not there yet */
1693 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1694
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001695 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001696 cifs_small_buf_release(req);
1697 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001698 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001699 if (rc != 0) {
1700 if (tcon) {
1701 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1702 tcon->need_reconnect = true;
1703 }
1704 goto tcon_error_exit;
1705 }
1706
Christophe JAILLETcd123002017-05-12 17:59:32 +02001707 switch (rsp->ShareType) {
1708 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001709 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001710 break;
1711 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001712 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001713 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001714 break;
1715 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001716 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001717 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001718 break;
1719 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001720 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001721 rc = -EOPNOTSUPP;
1722 goto tcon_error_exit;
1723 }
1724
1725 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001726 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001727 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1728 tcon->tidStatus = CifsGood;
1729 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001730 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001731 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001732
1733 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1734 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001735 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001736
1737 if (tcon->seal &&
1738 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1739 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1740
Steve Frenchde9f68df2013-11-15 11:26:24 -06001741 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001742 if (tcon->ses->server->ops->validate_negotiate)
1743 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001744tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001745
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001746 free_rsp_buf(resp_buftype, rsp);
1747 kfree(unc_path);
1748 return rc;
1749
1750tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001751 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001752 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001753 }
1754 goto tcon_exit;
1755}
1756
1757int
1758SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1759{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001760 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001761 struct smb2_tree_disconnect_req *req; /* response is trivial */
1762 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001763 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001764 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001765 unsigned int total_len;
1766 struct kvec iov[1];
1767 struct kvec rsp_iov;
1768 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001769
Joe Perchesf96637b2013-05-04 22:12:25 -05001770 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001771
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001772 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001773 return -EIO;
1774
1775 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1776 return 0;
1777
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001778 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1779 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001780 if (rc)
1781 return rc;
1782
Steve French5a77e752018-05-09 17:43:08 -05001783 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001784 flags |= CIFS_TRANSFORM_REQ;
1785
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001786 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001787
1788 iov[0].iov_base = (char *)req;
1789 iov[0].iov_len = total_len;
1790
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001791 memset(&rqst, 0, sizeof(struct smb_rqst));
1792 rqst.rq_iov = iov;
1793 rqst.rq_nvec = 1;
1794
1795 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001796 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001797 if (rc)
1798 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1799
1800 return rc;
1801}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001802
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001803
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001804static struct create_durable *
1805create_durable_buf(void)
1806{
1807 struct create_durable *buf;
1808
1809 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1810 if (!buf)
1811 return NULL;
1812
1813 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001814 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001815 buf->ccontext.DataLength = cpu_to_le32(16);
1816 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1817 (struct create_durable, Name));
1818 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001819 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001820 buf->Name[0] = 'D';
1821 buf->Name[1] = 'H';
1822 buf->Name[2] = 'n';
1823 buf->Name[3] = 'Q';
1824 return buf;
1825}
1826
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001827static struct create_durable *
1828create_reconnect_durable_buf(struct cifs_fid *fid)
1829{
1830 struct create_durable *buf;
1831
1832 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1833 if (!buf)
1834 return NULL;
1835
1836 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1837 (struct create_durable, Data));
1838 buf->ccontext.DataLength = cpu_to_le32(16);
1839 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1840 (struct create_durable, Name));
1841 buf->ccontext.NameLength = cpu_to_le16(4);
1842 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1843 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001844 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001845 buf->Name[0] = 'D';
1846 buf->Name[1] = 'H';
1847 buf->Name[2] = 'n';
1848 buf->Name[3] = 'C';
1849 return buf;
1850}
1851
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +10001852__u8
1853smb2_parse_lease_state(struct TCP_Server_Info *server,
1854 struct smb2_create_rsp *rsp,
1855 unsigned int *epoch, char *lease_key)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001856{
1857 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001858 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001859 unsigned int next;
1860 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001861 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001862
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001863 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001864 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001865 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001866 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001867 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001868 if (le16_to_cpu(cc->NameLength) == 4 &&
1869 strncmp(name, "RqLs", 4) == 0)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001870 return server->ops->parse_lease_buf(cc, epoch,
1871 lease_key);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001872
1873 next = le32_to_cpu(cc->Next);
1874 if (!next)
1875 break;
1876 remaining -= next;
1877 cc = (struct create_context *)((char *)cc + next);
1878 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001879
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001880 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001881}
1882
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001883static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001884add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02001885 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001886{
1887 struct smb2_create_req *req = iov[0].iov_base;
1888 unsigned int num = *num_iovec;
1889
Stefano Brivio729c0c92018-07-05 15:10:02 +02001890 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001891 if (iov[num].iov_base == NULL)
1892 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001893 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001894 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1895 if (!req->CreateContextsOffset)
1896 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001897 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001898 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001899 le32_add_cpu(&req->CreateContextsLength,
1900 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001901 *num_iovec = num + 1;
1902 return 0;
1903}
1904
Steve Frenchb56eae42015-11-03 09:26:27 -06001905static struct create_durable_v2 *
Steve Frenchca567eb2019-03-29 16:31:07 -05001906create_durable_v2_buf(struct cifs_open_parms *oparms)
Steve Frenchb56eae42015-11-03 09:26:27 -06001907{
Steve Frenchca567eb2019-03-29 16:31:07 -05001908 struct cifs_fid *pfid = oparms->fid;
Steve Frenchb56eae42015-11-03 09:26:27 -06001909 struct create_durable_v2 *buf;
1910
1911 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1912 if (!buf)
1913 return NULL;
1914
1915 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1916 (struct create_durable_v2, dcontext));
1917 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1918 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1919 (struct create_durable_v2, Name));
1920 buf->ccontext.NameLength = cpu_to_le16(4);
1921
Steve Frenchca567eb2019-03-29 16:31:07 -05001922 /*
1923 * NB: Handle timeout defaults to 0, which allows server to choose
1924 * (most servers default to 120 seconds) and most clients default to 0.
1925 * This can be overridden at mount ("handletimeout=") if the user wants
1926 * a different persistent (or resilient) handle timeout for all opens
1927 * opens on a particular SMB3 mount.
1928 */
1929 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
Steve Frenchb56eae42015-11-03 09:26:27 -06001930 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001931 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001932 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1933
1934 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1935 buf->Name[0] = 'D';
1936 buf->Name[1] = 'H';
1937 buf->Name[2] = '2';
1938 buf->Name[3] = 'Q';
1939 return buf;
1940}
1941
1942static struct create_durable_handle_reconnect_v2 *
1943create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1944{
1945 struct create_durable_handle_reconnect_v2 *buf;
1946
1947 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1948 GFP_KERNEL);
1949 if (!buf)
1950 return NULL;
1951
1952 buf->ccontext.DataOffset =
1953 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1954 dcontext));
1955 buf->ccontext.DataLength =
1956 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1957 buf->ccontext.NameOffset =
1958 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1959 Name));
1960 buf->ccontext.NameLength = cpu_to_le16(4);
1961
1962 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1963 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1964 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1965 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1966
1967 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1968 buf->Name[0] = 'D';
1969 buf->Name[1] = 'H';
1970 buf->Name[2] = '2';
1971 buf->Name[3] = 'C';
1972 return buf;
1973}
1974
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001975static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001976add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001977 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001978{
1979 struct smb2_create_req *req = iov[0].iov_base;
1980 unsigned int num = *num_iovec;
1981
Steve Frenchca567eb2019-03-29 16:31:07 -05001982 iov[num].iov_base = create_durable_v2_buf(oparms);
Steve Frenchb56eae42015-11-03 09:26:27 -06001983 if (iov[num].iov_base == NULL)
1984 return -ENOMEM;
1985 iov[num].iov_len = sizeof(struct create_durable_v2);
1986 if (!req->CreateContextsOffset)
1987 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001988 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001989 iov[1].iov_len);
1990 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001991 *num_iovec = num + 1;
1992 return 0;
1993}
1994
1995static int
1996add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1997 struct cifs_open_parms *oparms)
1998{
1999 struct smb2_create_req *req = iov[0].iov_base;
2000 unsigned int num = *num_iovec;
2001
2002 /* indicate that we don't need to relock the file */
2003 oparms->reconnect = false;
2004
2005 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2006 if (iov[num].iov_base == NULL)
2007 return -ENOMEM;
2008 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2009 if (!req->CreateContextsOffset)
2010 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002011 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002012 iov[1].iov_len);
2013 le32_add_cpu(&req->CreateContextsLength,
2014 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002015 *num_iovec = num + 1;
2016 return 0;
2017}
2018
2019static int
2020add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2021 struct cifs_open_parms *oparms, bool use_persistent)
2022{
2023 struct smb2_create_req *req = iov[0].iov_base;
2024 unsigned int num = *num_iovec;
2025
2026 if (use_persistent) {
2027 if (oparms->reconnect)
2028 return add_durable_reconnect_v2_context(iov, num_iovec,
2029 oparms);
2030 else
2031 return add_durable_v2_context(iov, num_iovec, oparms);
2032 }
2033
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002034 if (oparms->reconnect) {
2035 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2036 /* indicate that we don't need to relock the file */
2037 oparms->reconnect = false;
2038 } else
2039 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002040 if (iov[num].iov_base == NULL)
2041 return -ENOMEM;
2042 iov[num].iov_len = sizeof(struct create_durable);
2043 if (!req->CreateContextsOffset)
2044 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002045 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002046 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08002047 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002048 *num_iovec = num + 1;
2049 return 0;
2050}
2051
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002052/* See MS-SMB2 2.2.13.2.7 */
2053static struct crt_twarp_ctxt *
2054create_twarp_buf(__u64 timewarp)
2055{
2056 struct crt_twarp_ctxt *buf;
2057
2058 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2059 if (!buf)
2060 return NULL;
2061
2062 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2063 (struct crt_twarp_ctxt, Timestamp));
2064 buf->ccontext.DataLength = cpu_to_le32(8);
2065 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2066 (struct crt_twarp_ctxt, Name));
2067 buf->ccontext.NameLength = cpu_to_le16(4);
2068 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2069 buf->Name[0] = 'T';
2070 buf->Name[1] = 'W';
2071 buf->Name[2] = 'r';
2072 buf->Name[3] = 'p';
2073 buf->Timestamp = cpu_to_le64(timewarp);
2074 return buf;
2075}
2076
2077/* See MS-SMB2 2.2.13.2.7 */
2078static int
2079add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2080{
2081 struct smb2_create_req *req = iov[0].iov_base;
2082 unsigned int num = *num_iovec;
2083
2084 iov[num].iov_base = create_twarp_buf(timewarp);
2085 if (iov[num].iov_base == NULL)
2086 return -ENOMEM;
2087 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2088 if (!req->CreateContextsOffset)
2089 req->CreateContextsOffset = cpu_to_le32(
2090 sizeof(struct smb2_create_req) +
2091 iov[num - 1].iov_len);
2092 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2093 *num_iovec = num + 1;
2094 return 0;
2095}
2096
Aurelien Aptelf0712922017-02-22 14:47:17 +01002097static int
2098alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2099 const char *treename, const __le16 *path)
2100{
2101 int treename_len, path_len;
2102 struct nls_table *cp;
2103 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2104
2105 /*
2106 * skip leading "\\"
2107 */
2108 treename_len = strlen(treename);
2109 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2110 return -EINVAL;
2111
2112 treename += 2;
2113 treename_len -= 2;
2114
2115 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2116
2117 /*
2118 * make room for one path separator between the treename and
2119 * path
2120 */
2121 *out_len = treename_len + 1 + path_len;
2122
2123 /*
2124 * final path needs to be null-terminated UTF16 with a
2125 * size aligned to 8
2126 */
2127
2128 *out_size = roundup((*out_len+1)*2, 8);
2129 *out_path = kzalloc(*out_size, GFP_KERNEL);
2130 if (!*out_path)
2131 return -ENOMEM;
2132
2133 cp = load_nls_default();
2134 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2135 UniStrcat(*out_path, sep);
2136 UniStrcat(*out_path, path);
2137 unload_nls(cp);
2138
2139 return 0;
2140}
2141
Steve Frenchbea851b2018-06-14 21:56:32 -05002142int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2143 umode_t mode, struct cifs_tcon *tcon,
2144 const char *full_path,
2145 struct cifs_sb_info *cifs_sb)
2146{
2147 struct smb_rqst rqst;
2148 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002149 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002150 struct cifs_ses *ses = tcon->ses;
2151 struct kvec iov[3]; /* make sure at least one for each open context */
2152 struct kvec rsp_iov = {NULL, 0};
2153 int resp_buftype;
2154 int uni_path_len;
2155 __le16 *copy_path = NULL;
2156 int copy_size;
2157 int rc = 0;
2158 unsigned int n_iov = 2;
2159 __u32 file_attributes = 0;
2160 char *pc_buf = NULL;
2161 int flags = 0;
2162 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002163 __le16 *utf16_path = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002164
2165 cifs_dbg(FYI, "mkdir\n");
2166
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002167 /* resource #1: path allocation */
2168 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2169 if (!utf16_path)
2170 return -ENOMEM;
2171
YueHaibing29cbfa12018-12-18 02:51:51 +00002172 if (!ses || !(ses->server)) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002173 rc = -EIO;
2174 goto err_free_path;
2175 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002176
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002177 /* resource #2: request */
Steve Frenchbea851b2018-06-14 21:56:32 -05002178 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002179 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002180 goto err_free_path;
2181
Steve Frenchbea851b2018-06-14 21:56:32 -05002182
2183 if (smb3_encryption_required(tcon))
2184 flags |= CIFS_TRANSFORM_REQ;
2185
Steve Frenchbea851b2018-06-14 21:56:32 -05002186 req->ImpersonationLevel = IL_IMPERSONATION;
2187 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2188 /* File attributes ignored on open (used in create though) */
2189 req->FileAttributes = cpu_to_le32(file_attributes);
2190 req->ShareAccess = FILE_SHARE_ALL_LE;
2191 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2192 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2193
2194 iov[0].iov_base = (char *)req;
2195 /* -1 since last byte is buf[0] which is sent below (path) */
2196 iov[0].iov_len = total_len - 1;
2197
2198 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2199
2200 /* [MS-SMB2] 2.2.13 NameOffset:
2201 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2202 * the SMB2 header, the file name includes a prefix that will
2203 * be processed during DFS name normalization as specified in
2204 * section 3.3.5.9. Otherwise, the file name is relative to
2205 * the share that is identified by the TreeId in the SMB2
2206 * header.
2207 */
2208 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2209 int name_len;
2210
2211 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2212 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2213 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002214 tcon->treeName, utf16_path);
2215 if (rc)
2216 goto err_free_req;
2217
Steve Frenchbea851b2018-06-14 21:56:32 -05002218 req->NameLength = cpu_to_le16(name_len * 2);
2219 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002220 /* free before overwriting resource */
2221 kfree(utf16_path);
2222 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002223 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002224 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002225 /* MUST set path len (NameLength) to 0 opening root of share */
2226 req->NameLength = cpu_to_le16(uni_path_len - 2);
2227 if (uni_path_len % 8 != 0) {
2228 copy_size = roundup(uni_path_len, 8);
2229 copy_path = kzalloc(copy_size, GFP_KERNEL);
2230 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002231 rc = -ENOMEM;
2232 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002233 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002234 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002235 uni_path_len);
2236 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002237 /* free before overwriting resource */
2238 kfree(utf16_path);
2239 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002240 }
2241 }
2242
2243 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002244 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002245 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2246
2247 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002248 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002249 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002250 if (rc)
2251 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002252 pc_buf = iov[n_iov-1].iov_base;
2253 }
2254
2255
2256 memset(&rqst, 0, sizeof(struct smb_rqst));
2257 rqst.rq_iov = iov;
2258 rqst.rq_nvec = n_iov;
2259
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002260 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2261 FILE_WRITE_ATTRIBUTES);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002262 /* resource #4: response buffer */
2263 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2264 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002265 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2266 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002267 CREATE_NOT_FILE,
2268 FILE_WRITE_ATTRIBUTES, rc);
2269 goto err_free_rsp_buf;
2270 }
2271
2272 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2273 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2274 ses->Suid, CREATE_NOT_FILE,
2275 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002276
2277 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2278
2279 /* Eventually save off posix specific response info and timestaps */
2280
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002281err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002282 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002283 kfree(pc_buf);
2284err_free_req:
2285 cifs_small_buf_release(req);
2286err_free_path:
2287 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002288 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002289}
Steve Frenchbea851b2018-06-14 21:56:32 -05002290
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002291int
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002292SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2293 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002294{
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002295 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002296 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002297 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002298 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002299 int copy_size;
2300 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002301 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002302 struct kvec *iov = rqst->rq_iov;
2303 __le16 *copy_path;
2304 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002305
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002306 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002307 if (rc)
2308 return rc;
2309
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002310 iov[0].iov_base = (char *)req;
2311 /* -1 since last byte is buf[0] which is sent below (path) */
2312 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002313
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002314 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002315 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002316 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2317 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002318
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002319 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002320 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002321 /* File attributes ignored on open (used in create though) */
2322 req->FileAttributes = cpu_to_le32(file_attributes);
2323 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002324 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2325 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002326 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002327
2328 /* [MS-SMB2] 2.2.13 NameOffset:
2329 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2330 * the SMB2 header, the file name includes a prefix that will
2331 * be processed during DFS name normalization as specified in
2332 * section 3.3.5.9. Otherwise, the file name is relative to
2333 * the share that is identified by the TreeId in the SMB2
2334 * header.
2335 */
2336 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2337 int name_len;
2338
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002339 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002340 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2341 &name_len,
2342 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002343 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002344 return rc;
2345 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002346 uni_path_len = copy_size;
2347 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002348 } else {
2349 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2350 /* MUST set path len (NameLength) to 0 opening root of share */
2351 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002352 copy_size = uni_path_len;
2353 if (copy_size % 8 != 0)
2354 copy_size = roundup(copy_size, 8);
2355 copy_path = kzalloc(copy_size, GFP_KERNEL);
2356 if (!copy_path)
2357 return -ENOMEM;
2358 memcpy((char *)copy_path, (const char *)path,
2359 uni_path_len);
2360 uni_path_len = copy_size;
2361 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002362 }
2363
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002364 iov[1].iov_len = uni_path_len;
2365 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002366
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002367 if (!server->oplocks)
2368 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2369
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002370 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002371 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2372 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002373 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2374 (oparms->create_options & CREATE_NOT_FILE))
2375 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002376 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002377 rc = add_lease_context(server, iov, &n_iov,
2378 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002379 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002380 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002381 }
2382
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002383 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2384 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002385 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002386 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002387 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002388 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002389 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002390 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002391
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002392 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002393 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002394 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002395 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002396 }
2397
Steve Frenchce558b02018-05-31 19:16:54 -05002398 if (tcon->posix_extensions) {
2399 if (n_iov > 2) {
2400 struct create_context *ccontext =
2401 (struct create_context *)iov[n_iov-1].iov_base;
2402 ccontext->Next =
2403 cpu_to_le32(iov[n_iov-1].iov_len);
2404 }
2405
2406 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002407 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002408 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002409 }
Steve Frenchce558b02018-05-31 19:16:54 -05002410
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002411 if (tcon->snapshot_time) {
2412 cifs_dbg(FYI, "adding snapshot context\n");
2413 if (n_iov > 2) {
2414 struct create_context *ccontext =
2415 (struct create_context *)iov[n_iov-1].iov_base;
2416 ccontext->Next =
2417 cpu_to_le32(iov[n_iov-1].iov_len);
2418 }
2419
2420 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2421 if (rc)
2422 return rc;
2423 }
2424
2425
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002426 rqst->rq_nvec = n_iov;
2427 return 0;
2428}
2429
2430/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2431 * All other vectors are freed by kfree().
2432 */
2433void
2434SMB2_open_free(struct smb_rqst *rqst)
2435{
2436 int i;
2437
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002438 if (rqst && rqst->rq_iov) {
2439 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2440 for (i = 1; i < rqst->rq_nvec; i++)
2441 if (rqst->rq_iov[i].iov_base != smb2_padding)
2442 kfree(rqst->rq_iov[i].iov_base);
2443 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002444}
2445
2446int
2447SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2448 __u8 *oplock, struct smb2_file_all_info *buf,
2449 struct kvec *err_iov, int *buftype)
2450{
2451 struct smb_rqst rqst;
2452 struct smb2_create_rsp *rsp = NULL;
2453 struct TCP_Server_Info *server;
2454 struct cifs_tcon *tcon = oparms->tcon;
2455 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002456 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002457 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002458 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002459 int rc = 0;
2460 int flags = 0;
2461
2462 cifs_dbg(FYI, "create/open\n");
2463 if (ses && (ses->server))
2464 server = ses->server;
2465 else
2466 return -EIO;
2467
2468 if (smb3_encryption_required(tcon))
2469 flags |= CIFS_TRANSFORM_REQ;
2470
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002471 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002472 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002473 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002474 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002475
2476 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2477 if (rc)
2478 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002479
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002480 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2481 oparms->create_options, oparms->desired_access);
2482
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002483 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002484 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002485 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002486
2487 if (rc != 0) {
2488 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002489 if (err_iov && rsp) {
2490 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002491 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002492 resp_buftype = CIFS_NO_BUFFER;
2493 rsp = NULL;
2494 }
Steve French28d59362018-05-30 21:42:34 -05002495 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2496 oparms->create_options, oparms->desired_access, rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002497 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002498 } else
2499 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2500 ses->Suid, oparms->create_options,
2501 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002502
Steve Frenchfae80442018-10-19 17:14:32 -05002503 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002504 oparms->fid->persistent_fid = rsp->PersistentFileId;
2505 oparms->fid->volatile_fid = rsp->VolatileFileId;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002506#ifdef CONFIG_CIFS_DEBUG2
2507 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2508#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002509
2510 if (buf) {
2511 memcpy(buf, &rsp->CreationTime, 32);
2512 buf->AllocationSize = rsp->AllocationSize;
2513 buf->EndOfFile = rsp->EndofFile;
2514 buf->Attributes = rsp->FileAttributes;
2515 buf->NumberOfLinks = cpu_to_le32(1);
2516 buf->DeletePending = 0;
2517 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002518
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002519 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +10002520 *oplock = smb2_parse_lease_state(server, rsp,
2521 &oparms->fid->epoch,
2522 oparms->fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002523 else
2524 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002525creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002526 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002527 free_rsp_buf(resp_buftype, rsp);
2528 return rc;
2529}
2530
Steve French4a72daf2013-06-25 00:20:49 -05002531int
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002532SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2533 u64 persistent_fid, u64 volatile_fid, u32 opcode,
Steve French153322f2019-03-28 22:32:49 -05002534 bool is_fsctl, char *in_data, u32 indatalen,
2535 __u32 max_response_size)
Steve French4a72daf2013-06-25 00:20:49 -05002536{
2537 struct smb2_ioctl_req *req;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002538 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002539 unsigned int total_len;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002540 int rc;
Long Li2c87d6a2019-05-15 14:09:05 -07002541 char *in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002542
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002543 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002544 if (rc)
2545 return rc;
2546
Long Li2c87d6a2019-05-15 14:09:05 -07002547 if (indatalen) {
2548 /*
2549 * indatalen is usually small at a couple of bytes max, so
2550 * just allocate through generic pool
2551 */
2552 in_data_buf = kmalloc(indatalen, GFP_NOFS);
2553 if (!in_data_buf) {
2554 cifs_small_buf_release(req);
2555 return -ENOMEM;
2556 }
2557 memcpy(in_data_buf, in_data, indatalen);
2558 }
2559
Steve French4a72daf2013-06-25 00:20:49 -05002560 req->CtlCode = cpu_to_le32(opcode);
2561 req->PersistentFileId = persistent_fid;
2562 req->VolatileFileId = volatile_fid;
2563
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002564 iov[0].iov_base = (char *)req;
2565 /*
2566 * If no input data, the size of ioctl struct in
2567 * protocol spec still includes a 1 byte data buffer,
2568 * but if input data passed to ioctl, we do not
2569 * want to double count this, so we do not send
2570 * the dummy one byte of data in iovec[0] if sending
2571 * input data (in iovec[1]).
2572 */
Steve French4a72daf2013-06-25 00:20:49 -05002573 if (indatalen) {
2574 req->InputCount = cpu_to_le32(indatalen);
2575 /* do not set InputOffset if no input data */
2576 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002577 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002578 rqst->rq_nvec = 2;
2579 iov[0].iov_len = total_len - 1;
Long Li2c87d6a2019-05-15 14:09:05 -07002580 iov[1].iov_base = in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002581 iov[1].iov_len = indatalen;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002582 } else {
2583 rqst->rq_nvec = 1;
2584 iov[0].iov_len = total_len;
2585 }
Steve French4a72daf2013-06-25 00:20:49 -05002586
2587 req->OutputOffset = 0;
2588 req->OutputCount = 0; /* MBZ */
2589
2590 /*
Steve French153322f2019-03-28 22:32:49 -05002591 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2592 * We Could increase default MaxOutputResponse, but that could require
2593 * more credits. Windows typically sets this smaller, but for some
Steve French4a72daf2013-06-25 00:20:49 -05002594 * ioctls it may be useful to allow server to send more. No point
2595 * limiting what the server can send as long as fits in one credit
Steve French153322f2019-03-28 22:32:49 -05002596 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2597 * to increase this limit up in the future.
2598 * Note that for snapshot queries that servers like Azure expect that
2599 * the first query be minimal size (and just used to get the number/size
2600 * of previous versions) so response size must be specified as EXACTLY
2601 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2602 * of eight bytes. Currently that is the only case where we set max
2603 * response size smaller.
Steve French4a72daf2013-06-25 00:20:49 -05002604 */
Steve French153322f2019-03-28 22:32:49 -05002605 req->MaxOutputResponse = cpu_to_le32(max_response_size);
Steve French4a72daf2013-06-25 00:20:49 -05002606
2607 if (is_fsctl)
2608 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2609 else
2610 req->Flags = 0;
2611
Steve French4587eee2017-10-25 15:58:31 -05002612 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2613 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002614 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002615
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002616 return 0;
2617}
2618
2619void
2620SMB2_ioctl_free(struct smb_rqst *rqst)
2621{
Long Li2c87d6a2019-05-15 14:09:05 -07002622 if (rqst && rqst->rq_iov) {
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002623 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Long Li2c87d6a2019-05-15 14:09:05 -07002624 if (rqst->rq_iov[1].iov_len)
2625 kfree(rqst->rq_iov[1].iov_base);
2626 }
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002627}
2628
Steve French153322f2019-03-28 22:32:49 -05002629
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002630/*
2631 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2632 */
2633int
2634SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2635 u64 volatile_fid, u32 opcode, bool is_fsctl,
Steve French153322f2019-03-28 22:32:49 -05002636 char *in_data, u32 indatalen, u32 max_out_data_len,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002637 char **out_data, u32 *plen /* returned data len */)
2638{
2639 struct smb_rqst rqst;
2640 struct smb2_ioctl_rsp *rsp = NULL;
2641 struct cifs_ses *ses;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002642 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002643 struct kvec rsp_iov = {NULL, 0};
2644 int resp_buftype = CIFS_NO_BUFFER;
2645 int rc = 0;
2646 int flags = 0;
2647
2648 cifs_dbg(FYI, "SMB2 IOCTL\n");
2649
2650 if (out_data != NULL)
2651 *out_data = NULL;
2652
2653 /* zero out returned data len, in case of error */
2654 if (plen)
2655 *plen = 0;
2656
2657 if (tcon)
2658 ses = tcon->ses;
2659 else
2660 return -EIO;
2661
2662 if (!ses || !(ses->server))
2663 return -EIO;
2664
2665 if (smb3_encryption_required(tcon))
2666 flags |= CIFS_TRANSFORM_REQ;
2667
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002668 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002669 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002670 rqst.rq_iov = iov;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002671 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002672
Steve French153322f2019-03-28 22:32:49 -05002673 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2674 is_fsctl, in_data, indatalen, max_out_data_len);
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002675 if (rc)
2676 goto ioctl_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002677
2678 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002679 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002680 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002681
Steve Frencheccb4422018-05-17 21:16:55 -05002682 if (rc != 0)
2683 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2684 ses->Suid, 0, opcode, rc);
2685
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002686 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
Steve French8e353102015-03-26 19:47:02 -05002687 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002688 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002689 } else if (rc == -EINVAL) {
2690 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2691 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002692 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002693 goto ioctl_exit;
2694 }
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002695 } else if (rc == -E2BIG) {
2696 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2697 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2698 goto ioctl_exit;
2699 }
Steve French4a72daf2013-06-25 00:20:49 -05002700 }
2701
2702 /* check if caller wants to look at return data or just return rc */
2703 if ((plen == NULL) || (out_data == NULL))
2704 goto ioctl_exit;
2705
2706 *plen = le32_to_cpu(rsp->OutputCount);
2707
2708 /* We check for obvious errors in the output buffer length and offset */
2709 if (*plen == 0)
2710 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002711 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Steve French4a72daf2013-06-25 00:20:49 -05002712 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2713 *plen = 0;
2714 rc = -EIO;
2715 goto ioctl_exit;
2716 }
2717
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002718 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Steve French4a72daf2013-06-25 00:20:49 -05002719 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2720 le32_to_cpu(rsp->OutputOffset));
2721 *plen = 0;
2722 rc = -EIO;
2723 goto ioctl_exit;
2724 }
2725
YueHaibingd034fee2018-09-10 01:33:06 +00002726 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2727 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05002728 if (*out_data == NULL) {
2729 rc = -ENOMEM;
2730 goto ioctl_exit;
2731 }
2732
Steve French4a72daf2013-06-25 00:20:49 -05002733ioctl_exit:
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002734 SMB2_ioctl_free(&rqst);
Steve French4a72daf2013-06-25 00:20:49 -05002735 free_rsp_buf(resp_buftype, rsp);
2736 return rc;
2737}
2738
Steve French64a5cfa2013-10-14 15:31:32 -05002739/*
2740 * Individual callers to ioctl worker function follow
2741 */
2742
2743int
2744SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2745 u64 persistent_fid, u64 volatile_fid)
2746{
2747 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002748 struct compress_ioctl fsctl_input;
2749 char *ret_data = NULL;
2750
2751 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002752 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002753
2754 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2755 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2756 (char *)&fsctl_input /* data input */,
Steve French153322f2019-03-28 22:32:49 -05002757 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2758 &ret_data /* out data */, NULL);
Steve French64a5cfa2013-10-14 15:31:32 -05002759
2760 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002761
2762 return rc;
2763}
2764
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002765int
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002766SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2767 u64 persistent_fid, u64 volatile_fid)
2768{
2769 struct smb2_close_req *req;
2770 struct kvec *iov = rqst->rq_iov;
2771 unsigned int total_len;
2772 int rc;
2773
2774 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2775 if (rc)
2776 return rc;
2777
2778 req->PersistentFileId = persistent_fid;
2779 req->VolatileFileId = volatile_fid;
2780 iov[0].iov_base = (char *)req;
2781 iov[0].iov_len = total_len;
2782
2783 return 0;
2784}
2785
2786void
2787SMB2_close_free(struct smb_rqst *rqst)
2788{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002789 if (rqst && rqst->rq_iov)
2790 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002791}
2792
2793int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002794SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2795 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002796{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002797 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002798 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002799 struct cifs_ses *ses = tcon->ses;
2800 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002801 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01002802 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002803 int rc = 0;
2804
Joe Perchesf96637b2013-05-04 22:12:25 -05002805 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002806
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002807 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002808 return -EIO;
2809
Steve French5a77e752018-05-09 17:43:08 -05002810 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002811 flags |= CIFS_TRANSFORM_REQ;
2812
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002813 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002814 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002815 rqst.rq_iov = iov;
2816 rqst.rq_nvec = 1;
2817
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002818 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2819 if (rc)
2820 goto close_exit;
2821
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002822 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002823 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002824
2825 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002826 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002827 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2828 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002829 goto close_exit;
2830 }
2831
Steve Frenchfae80442018-10-19 17:14:32 -05002832 atomic_dec(&tcon->num_remote_opens);
2833
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002834 /* BB FIXME - decode close response, update inode for caching */
2835
2836close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002837 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002838 free_rsp_buf(resp_buftype, rsp);
2839 return rc;
2840}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002841
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002842int
2843SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2844 u64 persistent_fid, u64 volatile_fid)
2845{
2846 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2847}
2848
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002849int
2850smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2851 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002852{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002853 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002854 char *end_of_smb = smb_len + (char *)iov->iov_base;
2855 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002856 char *end_of_buf = begin_of_buf + buffer_length;
2857
2858
2859 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002860 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2861 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002862 return -EINVAL;
2863 }
2864
2865 /* check if beyond RFC1001 maximum length */
2866 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002867 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2868 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002869 return -EINVAL;
2870 }
2871
2872 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002873 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002874 return -EINVAL;
2875 }
2876
2877 return 0;
2878}
2879
2880/*
2881 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2882 * Caller must free buffer.
2883 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002884int
2885smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2886 struct kvec *iov, unsigned int minbufsize,
2887 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002888{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002889 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002890 int rc;
2891
2892 if (!data)
2893 return -EINVAL;
2894
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002895 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002896 if (rc)
2897 return rc;
2898
2899 memcpy(data, begin_of_buf, buffer_length);
2900
2901 return 0;
2902}
2903
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002904int
2905SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2906 u64 persistent_fid, u64 volatile_fid,
2907 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002908 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002909{
2910 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002911 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002912 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002913 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002914
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002915 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2916 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002917 if (rc)
2918 return rc;
2919
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002920 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002921 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002922 req->PersistentFileId = persistent_fid;
2923 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002924 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002925
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002926 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002927 if (input_len) {
2928 req->InputBufferLength = cpu_to_le32(input_len);
2929 /* total_len for smb query request never close to le16 max */
2930 req->InputBufferOffset = cpu_to_le16(total_len - 1);
2931 memcpy(req->Buffer, input, input_len);
2932 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002933
2934 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002935 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002936 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002937 return 0;
2938}
2939
2940void
2941SMB2_query_info_free(struct smb_rqst *rqst)
2942{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002943 if (rqst && rqst->rq_iov)
2944 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002945}
2946
2947static int
2948query_info(const unsigned int xid, struct cifs_tcon *tcon,
2949 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2950 u32 additional_info, size_t output_len, size_t min_len, void **data,
2951 u32 *dlen)
2952{
2953 struct smb_rqst rqst;
2954 struct smb2_query_info_rsp *rsp = NULL;
2955 struct kvec iov[1];
2956 struct kvec rsp_iov;
2957 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01002958 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002959 struct cifs_ses *ses = tcon->ses;
2960 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00002961 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002962
2963 cifs_dbg(FYI, "Query Info\n");
2964
2965 if (!ses || !(ses->server))
2966 return -EIO;
2967
2968 if (smb3_encryption_required(tcon))
2969 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002970
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002971 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002972 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002973 rqst.rq_iov = iov;
2974 rqst.rq_nvec = 1;
2975
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002976 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
2977 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002978 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002979 if (rc)
2980 goto qinf_exit;
2981
Steve Frenchd42043a2019-02-26 21:58:30 -06002982 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
2983 ses->Suid, info_class, (__u32)info_type);
2984
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002985 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002986 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002987
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002988 if (rc) {
2989 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002990 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2991 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002992 goto qinf_exit;
2993 }
2994
Steve Frenchd42043a2019-02-26 21:58:30 -06002995 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
2996 ses->Suid, info_class, (__u32)info_type);
2997
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002998 if (dlen) {
2999 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3000 if (!*data) {
3001 *data = kmalloc(*dlen, GFP_KERNEL);
3002 if (!*data) {
3003 cifs_dbg(VFS,
3004 "Error %d allocating memory for acl\n",
3005 rc);
3006 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003007 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003008 goto qinf_exit;
3009 }
Colin Ian King73aaf922019-01-16 16:28:59 +00003010 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003011 }
3012 }
3013
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003014 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3015 le32_to_cpu(rsp->OutputBufferLength),
3016 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00003017 if (rc && allocated) {
3018 kfree(*data);
3019 *data = NULL;
3020 *dlen = 0;
3021 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003022
3023qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003024 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003025 free_rsp_buf(resp_buftype, rsp);
3026 return rc;
3027}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003028
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003029int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3030 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003031{
3032 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003033 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04003034 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003035 sizeof(struct smb2_file_all_info), (void **)&data,
3036 NULL);
3037}
3038
3039int
3040SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3041 u64 persistent_fid, u64 volatile_fid,
3042 void **data, u32 *plen)
3043{
3044 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3045 *plen = 0;
3046
3047 return query_info(xid, tcon, persistent_fid, volatile_fid,
3048 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05003049 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003050}
3051
3052int
3053SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3054 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3055{
3056 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003057 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003058 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003059 sizeof(struct smb2_file_internal_info),
3060 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003061}
3062
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003063/*
3064 * This is a no-op for now. We're not really interested in the reply, but
3065 * rather in the fact that the server sent one and that server->lstrp
3066 * gets updated.
3067 *
3068 * FIXME: maybe we should consider checking that the reply matches request?
3069 */
3070static void
3071smb2_echo_callback(struct mid_q_entry *mid)
3072{
3073 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003074 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003075 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003076
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003077 if (mid->mid_state == MID_RESPONSE_RECEIVED
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003078 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3079 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3080 credits.instance = server->reconnect_instance;
3081 }
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003082
3083 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003084 add_credits(server, &credits, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003085}
3086
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003087void smb2_reconnect_server(struct work_struct *work)
3088{
3089 struct TCP_Server_Info *server = container_of(work,
3090 struct TCP_Server_Info, reconnect.work);
3091 struct cifs_ses *ses;
3092 struct cifs_tcon *tcon, *tcon2;
3093 struct list_head tmp_list;
3094 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01003095 int rc;
3096 int resched = false;
3097
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003098
3099 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3100 mutex_lock(&server->reconnect_mutex);
3101
3102 INIT_LIST_HEAD(&tmp_list);
3103 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3104
3105 spin_lock(&cifs_tcp_ses_lock);
3106 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3107 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003108 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003109 tcon->tc_count++;
3110 list_add_tail(&tcon->rlist, &tmp_list);
3111 tcon_exist = true;
3112 }
3113 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01003114 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3115 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3116 tcon_exist = true;
3117 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003118 }
3119 /*
3120 * Get the reference to server struct to be sure that the last call of
3121 * cifs_put_tcon() in the loop below won't release the server pointer.
3122 */
3123 if (tcon_exist)
3124 server->srv_count++;
3125
3126 spin_unlock(&cifs_tcp_ses_lock);
3127
3128 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01003129 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3130 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003131 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01003132 else
3133 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003134 list_del_init(&tcon->rlist);
3135 cifs_put_tcon(tcon);
3136 }
3137
3138 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01003139 if (resched)
3140 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003141 mutex_unlock(&server->reconnect_mutex);
3142
3143 /* now we can safely release srv struct */
3144 if (tcon_exist)
3145 cifs_put_tcp_session(server, 1);
3146}
3147
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003148int
3149SMB2_echo(struct TCP_Server_Info *server)
3150{
3151 struct smb2_echo_req *req;
3152 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003153 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003154 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003155 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003156 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003157
Joe Perchesf96637b2013-05-04 22:12:25 -05003158 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003159
Steve French4fcd1812016-06-22 20:12:05 -05003160 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003161 /* No need to send echo on newly established connections */
3162 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3163 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003164 }
3165
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003166 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003167 if (rc)
3168 return rc;
3169
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003170 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003171
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003172 iov[0].iov_len = total_len;
3173 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003174
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003175 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003176 server, CIFS_ECHO_OP, NULL);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003177 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003178 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003179
3180 cifs_small_buf_release(req);
3181 return rc;
3182}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003183
3184int
3185SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3186 u64 volatile_fid)
3187{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003188 struct smb_rqst rqst;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003189 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003190 struct cifs_ses *ses = tcon->ses;
3191 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003192 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003193 int resp_buftype;
3194 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003195 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003196 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003197
Joe Perchesf96637b2013-05-04 22:12:25 -05003198 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003199
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003200 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003201 return -EIO;
3202
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003203 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003204 if (rc)
3205 return rc;
3206
Steve French5a77e752018-05-09 17:43:08 -05003207 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003208 flags |= CIFS_TRANSFORM_REQ;
3209
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003210 req->PersistentFileId = persistent_fid;
3211 req->VolatileFileId = volatile_fid;
3212
3213 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003214 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003215
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003216 memset(&rqst, 0, sizeof(struct smb_rqst));
3217 rqst.rq_iov = iov;
3218 rqst.rq_nvec = 1;
3219
3220 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003221 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003222
Steve Frencheccb4422018-05-17 21:16:55 -05003223 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003224 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003225 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3226 rc);
3227 }
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003228
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003229 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003230 return rc;
3231}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003232
3233/*
3234 * To form a chain of read requests, any read requests after the first should
3235 * have the end_of_chain boolean set to true.
3236 */
3237static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003238smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003239 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3240 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003241{
3242 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003243 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003244 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07003245 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003246
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003247 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3248 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003249 if (rc)
3250 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003251
3252 server = io_parms->tcon->ses->server;
3253 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003254 return -ECONNABORTED;
3255
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003256 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003257 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003258
3259 req->PersistentFileId = io_parms->persistent_fid;
3260 req->VolatileFileId = io_parms->volatile_fid;
3261 req->ReadChannelInfoOffset = 0; /* reserved */
3262 req->ReadChannelInfoLength = 0; /* reserved */
3263 req->Channel = 0; /* reserved */
3264 req->MinimumCount = 0;
3265 req->Length = cpu_to_le32(io_parms->length);
3266 req->Offset = cpu_to_le64(io_parms->offset);
Steve Frenchd323c2462019-02-25 00:52:43 -06003267
3268 trace_smb3_read_enter(0 /* xid */,
3269 io_parms->persistent_fid,
3270 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3271 io_parms->offset, io_parms->length);
Long Libd3dcc62017-11-22 17:38:47 -07003272#ifdef CONFIG_CIFS_SMB_DIRECT
3273 /*
3274 * If we want to do a RDMA write, fill in and append
3275 * smbd_buffer_descriptor_v1 to the end of read request
3276 */
Long Libb4c0412018-04-17 12:17:08 -07003277 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003278 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003279
Long Libd3dcc62017-11-22 17:38:47 -07003280 struct smbd_buffer_descriptor_v1 *v1;
3281 bool need_invalidate =
3282 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3283
3284 rdata->mr = smbd_register_mr(
3285 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003286 rdata->nr_pages, rdata->page_offset,
3287 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003288 if (!rdata->mr)
Long Lib7972092019-04-05 21:36:34 +00003289 return -EAGAIN;
Long Libd3dcc62017-11-22 17:38:47 -07003290
3291 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3292 if (need_invalidate)
3293 req->Channel = SMB2_CHANNEL_RDMA_V1;
3294 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003295 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003296 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003297 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003298 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003299 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3300 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3301 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003302
3303 *total_len += sizeof(*v1) - 1;
3304 }
3305#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003306 if (request_type & CHAINED_REQUEST) {
3307 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003308 /* next 8-byte aligned request */
3309 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3310 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003311 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003312 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003313 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003314 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003315 /*
3316 * Related requests use info from previous read request
3317 * in chain.
3318 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003319 shdr->SessionId = 0xFFFFFFFF;
3320 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003321 req->PersistentFileId = 0xFFFFFFFF;
3322 req->VolatileFileId = 0xFFFFFFFF;
3323 }
3324 }
3325 if (remaining_bytes > io_parms->length)
3326 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3327 else
3328 req->RemainingBytes = 0;
3329
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003330 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003331 return rc;
3332}
3333
3334static void
3335smb2_readv_callback(struct mid_q_entry *mid)
3336{
3337 struct cifs_readdata *rdata = mid->callback_data;
3338 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3339 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003340 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003341 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003342 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003343 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3344 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07003345 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003346 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003347 .rq_npages = rdata->nr_pages,
3348 .rq_pagesz = rdata->pagesz,
3349 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003350
Joe Perchesf96637b2013-05-04 22:12:25 -05003351 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3352 __func__, mid->mid, mid->mid_state, rdata->result,
3353 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003354
3355 switch (mid->mid_state) {
3356 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003357 credits.value = le16_to_cpu(shdr->CreditRequest);
3358 credits.instance = server->reconnect_instance;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003359 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003360 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003361 int rc;
3362
Jeff Layton0b688cf2012-09-18 16:20:34 -07003363 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003364 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003365 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
3366 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003367 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003368 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003369 task_io_account_read(rdata->got_bytes);
3370 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003371 break;
3372 case MID_REQUEST_SUBMITTED:
3373 case MID_RETRY_NEEDED:
3374 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003375 if (server->sign && rdata->got_bytes)
3376 /* reset bytes number since we can not check a sign */
3377 rdata->got_bytes = 0;
3378 /* FIXME: should this be counted toward the initiating task? */
3379 task_io_account_read(rdata->got_bytes);
3380 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003381 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003382 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003383 credits.value = le16_to_cpu(shdr->CreditRequest);
3384 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003385 /* fall through */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003386 default:
Pavel Shilovsky6b15eb12019-01-18 15:46:14 -08003387 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003388 }
Long Libd3dcc62017-11-22 17:38:47 -07003389#ifdef CONFIG_CIFS_SMB_DIRECT
3390 /*
3391 * If this rdata has a memmory registered, the MR can be freed
3392 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3393 * because they have limited number and are used for future I/Os
3394 */
3395 if (rdata->mr) {
3396 smbd_deregister_mr(rdata->mr);
3397 rdata->mr = NULL;
3398 }
3399#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08003400 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003401 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003402 trace_smb3_read_err(0 /* xid */,
3403 rdata->cfile->fid.persistent_fid,
3404 tcon->tid, tcon->ses->Suid, rdata->offset,
3405 rdata->bytes, rdata->result);
3406 } else
3407 trace_smb3_read_done(0 /* xid */,
3408 rdata->cfile->fid.persistent_fid,
3409 tcon->tid, tcon->ses->Suid,
3410 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003411
3412 queue_work(cifsiod_wq, &rdata->work);
3413 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003414 add_credits(server, &credits, 0);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003415}
3416
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003417/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003418int
3419smb2_async_readv(struct cifs_readdata *rdata)
3420{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003421 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003422 char *buf;
3423 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003424 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003425 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003426 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003427 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003428 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003429
Joe Perchesf96637b2013-05-04 22:12:25 -05003430 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3431 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003432
3433 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3434 io_parms.offset = rdata->offset;
3435 io_parms.length = rdata->bytes;
3436 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3437 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3438 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003439
3440 server = io_parms.tcon->ses->server;
3441
Long Li2dabfd52017-11-07 01:54:53 -07003442 rc = smb2_new_read_req(
3443 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003444 if (rc)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003445 return rc;
3446
Steve French5a77e752018-05-09 17:43:08 -05003447 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003448 flags |= CIFS_TRANSFORM_REQ;
3449
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003450 rdata->iov[0].iov_base = buf;
3451 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003452
3453 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003454
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003455 if (rdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003456 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003457 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003458 shdr->CreditRequest =
3459 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003460
3461 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3462 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003463 goto async_readv_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003464
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003465 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003466 }
3467
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003468 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07003469 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003470 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003471 smb3_handle_read_data, rdata, flags,
3472 &rdata->credits);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003473 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003474 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003475 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003476 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3477 io_parms.tcon->tid,
3478 io_parms.tcon->ses->Suid,
3479 io_parms.offset, io_parms.length, rc);
3480 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003481
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003482async_readv_out:
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003483 cifs_small_buf_release(buf);
3484 return rc;
3485}
Pavel Shilovsky33319142012-09-18 16:20:29 -07003486
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003487int
3488SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3489 unsigned int *nbytes, char **buf, int *buf_type)
3490{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003491 struct smb_rqst rqst;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003492 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003493 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003494 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003495 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003496 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003497 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003498 int flags = CIFS_LOG_ERROR;
3499 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003500
3501 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07003502 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003503 if (rc)
3504 return rc;
3505
Steve French5a77e752018-05-09 17:43:08 -05003506 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003507 flags |= CIFS_TRANSFORM_REQ;
3508
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003509 iov[0].iov_base = (char *)req;
3510 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003511
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003512 memset(&rqst, 0, sizeof(struct smb_rqst));
3513 rqst.rq_iov = iov;
3514 rqst.rq_nvec = 1;
3515
3516 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003517 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003518
3519 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003520 if (rc != -ENODATA) {
3521 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3522 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003523 trace_smb3_read_err(xid, req->PersistentFileId,
3524 io_parms->tcon->tid, ses->Suid,
3525 io_parms->offset, io_parms->length,
3526 rc);
Steve Frenchb0a42f22019-02-25 15:02:58 -06003527 } else
3528 trace_smb3_read_done(xid, req->PersistentFileId,
3529 io_parms->tcon->tid, ses->Suid,
3530 io_parms->offset, 0);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003531 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Ronnie Sahlberg05fd5c22019-04-23 16:39:45 +10003532 cifs_small_buf_release(req);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003533 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05003534 } else
3535 trace_smb3_read_done(xid, req->PersistentFileId,
3536 io_parms->tcon->tid, ses->Suid,
3537 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003538
ZhangXiaoxu088aaf12019-04-06 15:47:39 +08003539 cifs_small_buf_release(req);
3540
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003541 *nbytes = le32_to_cpu(rsp->DataLength);
3542 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3543 (*nbytes > io_parms->length)) {
3544 cifs_dbg(FYI, "bad length %d for count %d\n",
3545 *nbytes, io_parms->length);
3546 rc = -EIO;
3547 *nbytes = 0;
3548 }
3549
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003550 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003551 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003552 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003553 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003554 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003555 if (resp_buftype == CIFS_SMALL_BUFFER)
3556 *buf_type = CIFS_SMALL_BUFFER;
3557 else if (resp_buftype == CIFS_LARGE_BUFFER)
3558 *buf_type = CIFS_LARGE_BUFFER;
3559 }
3560 return rc;
3561}
3562
Pavel Shilovsky33319142012-09-18 16:20:29 -07003563/*
3564 * Check the mid_state and signature on received buffer (if any), and queue the
3565 * workqueue completion task.
3566 */
3567static void
3568smb2_writev_callback(struct mid_q_entry *mid)
3569{
3570 struct cifs_writedata *wdata = mid->callback_data;
3571 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003572 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003573 unsigned int written;
3574 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003575 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky33319142012-09-18 16:20:29 -07003576
3577 switch (mid->mid_state) {
3578 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003579 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3580 credits.instance = server->reconnect_instance;
3581 wdata->result = smb2_check_receive(mid, server, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003582 if (wdata->result != 0)
3583 break;
3584
3585 written = le32_to_cpu(rsp->DataLength);
3586 /*
3587 * Mask off high 16 bits when bytes written as returned
3588 * by the server is greater than bytes requested by the
3589 * client. OS/2 servers are known to set incorrect
3590 * CountHigh values.
3591 */
3592 if (written > wdata->bytes)
3593 written &= 0xFFFF;
3594
3595 if (written < wdata->bytes)
3596 wdata->result = -ENOSPC;
3597 else
3598 wdata->bytes = written;
3599 break;
3600 case MID_REQUEST_SUBMITTED:
3601 case MID_RETRY_NEEDED:
3602 wdata->result = -EAGAIN;
3603 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003604 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003605 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3606 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003607 /* fall through */
Pavel Shilovsky33319142012-09-18 16:20:29 -07003608 default:
3609 wdata->result = -EIO;
3610 break;
3611 }
Long Lidb223a52017-11-22 17:38:45 -07003612#ifdef CONFIG_CIFS_SMB_DIRECT
3613 /*
3614 * If this wdata has a memory registered, the MR can be freed
3615 * The number of MRs available is limited, it's important to recover
3616 * used MR as soon as I/O is finished. Hold MR longer in the later
3617 * I/O process can possibly result in I/O deadlock due to lack of MR
3618 * to send request on I/O retry
3619 */
3620 if (wdata->mr) {
3621 smbd_deregister_mr(wdata->mr);
3622 wdata->mr = NULL;
3623 }
3624#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003625 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003626 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003627 trace_smb3_write_err(0 /* no xid */,
3628 wdata->cfile->fid.persistent_fid,
3629 tcon->tid, tcon->ses->Suid, wdata->offset,
3630 wdata->bytes, wdata->result);
3631 } else
3632 trace_smb3_write_done(0 /* no xid */,
3633 wdata->cfile->fid.persistent_fid,
3634 tcon->tid, tcon->ses->Suid,
3635 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003636
3637 queue_work(cifsiod_wq, &wdata->work);
3638 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003639 add_credits(server, &credits, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003640}
3641
3642/* smb2_async_writev - send an async write, and set up mid to handle result */
3643int
Steve French4a5c80d2014-02-07 20:45:12 -06003644smb2_async_writev(struct cifs_writedata *wdata,
3645 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07003646{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003647 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003648 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003649 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003650 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003651 struct TCP_Server_Info *server = tcon->ses->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003652 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003653 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003654 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003655
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003656 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003657 if (rc)
3658 return rc;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003659
Steve French5a77e752018-05-09 17:43:08 -05003660 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003661 flags |= CIFS_TRANSFORM_REQ;
3662
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003663 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003664 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003665
3666 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3667 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3668 req->WriteChannelInfoOffset = 0;
3669 req->WriteChannelInfoLength = 0;
3670 req->Channel = 0;
3671 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003672 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003673 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003674 req->RemainingBytes = 0;
Steve Frenchd323c2462019-02-25 00:52:43 -06003675
3676 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3677 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003678#ifdef CONFIG_CIFS_SMB_DIRECT
3679 /*
3680 * If we want to do a server RDMA read, fill in and append
3681 * smbd_buffer_descriptor_v1 to the end of write request
3682 */
Long Libb4c0412018-04-17 12:17:08 -07003683 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003684 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003685
Long Lidb223a52017-11-22 17:38:45 -07003686 struct smbd_buffer_descriptor_v1 *v1;
3687 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3688
3689 wdata->mr = smbd_register_mr(
3690 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003691 wdata->nr_pages, wdata->page_offset,
3692 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07003693 if (!wdata->mr) {
Long Lib7972092019-04-05 21:36:34 +00003694 rc = -EAGAIN;
Long Lidb223a52017-11-22 17:38:45 -07003695 goto async_writev_out;
3696 }
3697 req->Length = 0;
3698 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07003699 if (wdata->nr_pages > 1)
3700 req->RemainingBytes =
3701 cpu_to_le32(
3702 (wdata->nr_pages - 1) * wdata->pagesz -
3703 wdata->page_offset + wdata->tailsz
3704 );
3705 else
3706 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003707 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3708 if (need_invalidate)
3709 req->Channel = SMB2_CHANNEL_RDMA_V1;
3710 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003711 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003712 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003713 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003714 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003715 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3716 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3717 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003718 }
3719#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003720 iov[0].iov_len = total_len - 1;
3721 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003722
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003723 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003724 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003725 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07003726 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003727 rqst.rq_npages = wdata->nr_pages;
3728 rqst.rq_pagesz = wdata->pagesz;
3729 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003730#ifdef CONFIG_CIFS_SMB_DIRECT
3731 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003732 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07003733 rqst.rq_npages = 0;
3734 }
3735#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003736 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3737 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003738
Long Lidb223a52017-11-22 17:38:45 -07003739#ifdef CONFIG_CIFS_SMB_DIRECT
3740 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3741 if (!wdata->mr)
3742 req->Length = cpu_to_le32(wdata->bytes);
3743#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003744 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003745#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003746
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003747 if (wdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003748 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003749 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003750 shdr->CreditRequest =
3751 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003752
3753 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3754 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003755 goto async_writev_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003756
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003757 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003758 }
3759
Pavel Shilovsky33319142012-09-18 16:20:29 -07003760 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003761 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003762 wdata, flags, &wdata->credits);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003763
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003764 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003765 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3766 tcon->tid, tcon->ses->Suid, wdata->offset,
3767 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06003768 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003769 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003770 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003771
Pavel Shilovsky33319142012-09-18 16:20:29 -07003772async_writev_out:
3773 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003774 return rc;
3775}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003776
3777/*
3778 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3779 * The length field from io_parms must be at least 1 and indicates a number of
3780 * elements with data to write that begins with position 1 in iov array. All
3781 * data length is specified by count.
3782 */
3783int
3784SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3785 unsigned int *nbytes, struct kvec *iov, int n_vec)
3786{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003787 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003788 int rc = 0;
3789 struct smb2_write_req *req = NULL;
3790 struct smb2_write_rsp *rsp = NULL;
3791 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003792 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003793 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003794 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003795
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003796 *nbytes = 0;
3797
3798 if (n_vec < 1)
3799 return rc;
3800
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003801 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3802 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003803 if (rc)
3804 return rc;
3805
3806 if (io_parms->tcon->ses->server == NULL)
3807 return -ECONNABORTED;
3808
Steve French5a77e752018-05-09 17:43:08 -05003809 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003810 flags |= CIFS_TRANSFORM_REQ;
3811
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003812 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003813
3814 req->PersistentFileId = io_parms->persistent_fid;
3815 req->VolatileFileId = io_parms->volatile_fid;
3816 req->WriteChannelInfoOffset = 0;
3817 req->WriteChannelInfoLength = 0;
3818 req->Channel = 0;
3819 req->Length = cpu_to_le32(io_parms->length);
3820 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003821 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003822 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003823 req->RemainingBytes = 0;
3824
Steve Frenchd323c2462019-02-25 00:52:43 -06003825 trace_smb3_write_enter(xid, io_parms->persistent_fid,
3826 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3827 io_parms->offset, io_parms->length);
3828
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003829 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003830 /* 1 for Buffer */
3831 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003832
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003833 memset(&rqst, 0, sizeof(struct smb_rqst));
3834 rqst.rq_iov = iov;
3835 rqst.rq_nvec = n_vec + 1;
3836
3837 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003838 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003839 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003840
3841 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003842 trace_smb3_write_err(xid, req->PersistentFileId,
3843 io_parms->tcon->tid,
3844 io_parms->tcon->ses->Suid,
3845 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003846 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003847 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05003848 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003849 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05003850 trace_smb3_write_done(xid, req->PersistentFileId,
3851 io_parms->tcon->tid,
3852 io_parms->tcon->ses->Suid,
3853 io_parms->offset, *nbytes);
3854 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003855
ZhangXiaoxu6a3eb332019-04-06 15:47:38 +08003856 cifs_small_buf_release(req);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003857 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003858 return rc;
3859}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003860
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003861static unsigned int
3862num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3863{
3864 int len;
3865 unsigned int entrycount = 0;
3866 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03003867 char *entryptr;
3868 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003869
3870 if (bufstart == NULL)
3871 return 0;
3872
Dan Carpenter56446f22018-09-06 12:48:22 +03003873 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003874
3875 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03003876 if (entryptr + next_offset < entryptr ||
3877 entryptr + next_offset > end_of_buf ||
3878 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003879 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003880 break;
3881 }
3882
Dan Carpenter56446f22018-09-06 12:48:22 +03003883 entryptr = entryptr + next_offset;
3884 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
3885
3886 len = le32_to_cpu(dir_info->FileNameLength);
3887 if (entryptr + len < entryptr ||
3888 entryptr + len > end_of_buf ||
3889 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003890 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3891 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003892 break;
3893 }
3894
Dan Carpenter56446f22018-09-06 12:48:22 +03003895 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003896 entrycount++;
3897
Dan Carpenter56446f22018-09-06 12:48:22 +03003898 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003899 if (!next_offset)
3900 break;
3901 }
3902
3903 return entrycount;
3904}
3905
3906/*
3907 * Readdir/FindFirst
3908 */
3909int
3910SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3911 u64 persistent_fid, u64 volatile_fid, int index,
3912 struct cifs_search_info *srch_inf)
3913{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003914 struct smb_rqst rqst;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003915 struct smb2_query_directory_req *req;
3916 struct smb2_query_directory_rsp *rsp = NULL;
3917 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003918 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003919 int rc = 0;
3920 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003921 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003922 unsigned char *bufptr;
3923 struct TCP_Server_Info *server;
3924 struct cifs_ses *ses = tcon->ses;
3925 __le16 asteriks = cpu_to_le16('*');
3926 char *end_of_smb;
3927 unsigned int output_size = CIFSMaxBufSize;
3928 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003929 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003930 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003931
3932 if (ses && (ses->server))
3933 server = ses->server;
3934 else
3935 return -EIO;
3936
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003937 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3938 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003939 if (rc)
3940 return rc;
3941
Steve French5a77e752018-05-09 17:43:08 -05003942 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003943 flags |= CIFS_TRANSFORM_REQ;
3944
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003945 switch (srch_inf->info_level) {
3946 case SMB_FIND_FILE_DIRECTORY_INFO:
3947 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3948 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3949 break;
3950 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3951 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3952 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3953 break;
3954 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003955 cifs_dbg(VFS, "info level %u isn't supported\n",
3956 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003957 rc = -EINVAL;
3958 goto qdir_exit;
3959 }
3960
3961 req->FileIndex = cpu_to_le32(index);
3962 req->PersistentFileId = persistent_fid;
3963 req->VolatileFileId = volatile_fid;
3964
3965 len = 0x2;
3966 bufptr = req->Buffer;
3967 memcpy(bufptr, &asteriks, len);
3968
3969 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003970 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003971 req->FileNameLength = cpu_to_le16(len);
3972 /*
3973 * BB could be 30 bytes or so longer if we used SMB2 specific
3974 * buffer lengths, but this is safe and close enough.
3975 */
3976 output_size = min_t(unsigned int, output_size, server->maxBuf);
3977 output_size = min_t(unsigned int, output_size, 2 << 15);
3978 req->OutputBufferLength = cpu_to_le32(output_size);
3979
3980 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003981 /* 1 for Buffer */
3982 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003983
3984 iov[1].iov_base = (char *)(req->Buffer);
3985 iov[1].iov_len = len;
3986
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003987 memset(&rqst, 0, sizeof(struct smb_rqst));
3988 rqst.rq_iov = iov;
3989 rqst.rq_nvec = 2;
3990
Steve Frenchd323c2462019-02-25 00:52:43 -06003991 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
3992 tcon->ses->Suid, index, output_size);
3993
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003994 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003995 cifs_small_buf_release(req);
3996 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003997
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003998 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003999 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004000 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004001 trace_smb3_query_dir_done(xid, persistent_fid,
4002 tcon->tid, tcon->ses->Suid, index, 0);
Pavel Shilovsky52755802014-08-18 20:49:57 +04004003 srch_inf->endOfSearch = true;
4004 rc = 0;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004005 } else {
4006 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4007 tcon->ses->Suid, index, 0, rc);
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08004008 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004009 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004010 goto qdir_exit;
4011 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004012
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004013 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4014 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4015 info_buf_size);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004016 if (rc) {
4017 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4018 tcon->ses->Suid, index, 0, rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004019 goto qdir_exit;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004020 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004021
4022 srch_inf->unicode = true;
4023
4024 if (srch_inf->ntwrk_buf_start) {
4025 if (srch_inf->smallBuf)
4026 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4027 else
4028 cifs_buf_release(srch_inf->ntwrk_buf_start);
4029 }
4030 srch_inf->ntwrk_buf_start = (char *)rsp;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004031 srch_inf->srch_entries_start = srch_inf->last_entry =
4032 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4033 end_of_smb = rsp_iov.iov_len + (char *)rsp;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004034 srch_inf->entries_in_buffer =
4035 num_entries(srch_inf->srch_entries_start, end_of_smb,
4036 &srch_inf->last_entry, info_buf_size);
4037 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05004038 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4039 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4040 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004041 if (resp_buftype == CIFS_LARGE_BUFFER)
4042 srch_inf->smallBuf = false;
4043 else if (resp_buftype == CIFS_SMALL_BUFFER)
4044 srch_inf->smallBuf = true;
4045 else
Joe Perchesf96637b2013-05-04 22:12:25 -05004046 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004047
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004048 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4049 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004050 return rc;
4051
4052qdir_exit:
4053 free_rsp_buf(resp_buftype, rsp);
4054 return rc;
4055}
4056
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004057int
4058SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004059 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004060 u8 info_type, u32 additional_info,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004061 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004062{
4063 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004064 struct kvec *iov = rqst->rq_iov;
4065 unsigned int i, total_len;
4066 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004067
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004068 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004069 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004070 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004071
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004072 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004073 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004074 req->FileInfoClass = info_class;
4075 req->PersistentFileId = persistent_fid;
4076 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004077 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004078
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004079 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004080 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004081 req->BufferLength = cpu_to_le32(*size);
4082
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004083 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004084 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004085
4086 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004087 /* 1 for Buffer */
4088 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004089
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004090 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004091 le32_add_cpu(&req->BufferLength, size[i]);
4092 iov[i].iov_base = (char *)data[i];
4093 iov[i].iov_len = size[i];
4094 }
4095
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004096 return 0;
4097}
4098
4099void
4100SMB2_set_info_free(struct smb_rqst *rqst)
4101{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10004102 if (rqst && rqst->rq_iov)
4103 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004104}
4105
4106static int
4107send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4108 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4109 u8 info_type, u32 additional_info, unsigned int num,
4110 void **data, unsigned int *size)
4111{
4112 struct smb_rqst rqst;
4113 struct smb2_set_info_rsp *rsp = NULL;
4114 struct kvec *iov;
4115 struct kvec rsp_iov;
4116 int rc = 0;
4117 int resp_buftype;
4118 struct cifs_ses *ses = tcon->ses;
4119 int flags = 0;
4120
4121 if (!ses || !(ses->server))
4122 return -EIO;
4123
4124 if (!num)
4125 return -EINVAL;
4126
4127 if (smb3_encryption_required(tcon))
4128 flags |= CIFS_TRANSFORM_REQ;
4129
4130 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4131 if (!iov)
4132 return -ENOMEM;
4133
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004134 memset(&rqst, 0, sizeof(struct smb_rqst));
4135 rqst.rq_iov = iov;
4136 rqst.rq_nvec = num;
4137
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004138 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4139 info_class, info_type, additional_info,
4140 data, size);
4141 if (rc) {
4142 kfree(iov);
4143 return rc;
4144 }
4145
4146
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004147 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004148 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004149 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004150 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004151
Steve Frencheccb4422018-05-17 21:16:55 -05004152 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004153 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004154 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4155 ses->Suid, info_class, (__u32)info_type, rc);
4156 }
Steve French7d3fb242013-11-18 09:56:28 -06004157
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004158 free_rsp_buf(resp_buftype, rsp);
4159 kfree(iov);
4160 return rc;
4161}
4162
4163int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004164SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004165 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004166{
4167 struct smb2_file_eof_info info;
4168 void *data;
4169 unsigned int size;
4170
4171 info.EndOfFile = *eof;
4172
4173 data = &info;
4174 size = sizeof(struct smb2_file_eof_info);
4175
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004176 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004177 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4178 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004179}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004180
4181int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004182SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4183 u64 persistent_fid, u64 volatile_fid,
4184 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4185{
4186 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4187 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4188 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004189}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004190
4191int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004192SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4193 u64 persistent_fid, u64 volatile_fid,
4194 struct smb2_file_full_ea_info *buf, int len)
4195{
4196 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4197 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4198 0, 1, (void **)&buf, &len);
4199}
4200
4201int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004202SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4203 const u64 persistent_fid, const u64 volatile_fid,
4204 __u8 oplock_level)
4205{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004206 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004207 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10004208 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004209 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004210 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004211 unsigned int total_len;
4212 struct kvec iov[1];
4213 struct kvec rsp_iov;
4214 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004215
Joe Perchesf96637b2013-05-04 22:12:25 -05004216 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004217 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4218 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004219 if (rc)
4220 return rc;
4221
Steve French5a77e752018-05-09 17:43:08 -05004222 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004223 flags |= CIFS_TRANSFORM_REQ;
4224
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004225 req->VolatileFid = volatile_fid;
4226 req->PersistentFid = persistent_fid;
4227 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004228 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004229
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004230 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004231
4232 iov[0].iov_base = (char *)req;
4233 iov[0].iov_len = total_len;
4234
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004235 memset(&rqst, 0, sizeof(struct smb_rqst));
4236 rqst.rq_iov = iov;
4237 rqst.rq_nvec = 1;
4238
4239 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004240 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004241
4242 if (rc) {
4243 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004244 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004245 }
4246
4247 return rc;
4248}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004249
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004250void
4251smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4252 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004253{
4254 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4255 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4256 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05304257 kst->f_bfree = kst->f_bavail =
4258 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004259 return;
4260}
4261
Steve French2d304212018-06-24 23:28:12 -05004262static void
4263copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4264 struct kstatfs *kst)
4265{
4266 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4267 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4268 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4269 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4270 kst->f_bavail = kst->f_bfree;
4271 else
4272 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4273 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4274 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4275 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4276 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4277
4278 return;
4279}
Steve French2d304212018-06-24 23:28:12 -05004280
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004281static int
4282build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4283 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4284{
4285 int rc;
4286 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004287 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004288
Joe Perchesf96637b2013-05-04 22:12:25 -05004289 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004290
4291 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4292 return -EIO;
4293
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004294 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4295 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004296 if (rc)
4297 return rc;
4298
4299 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4300 req->FileInfoClass = level;
4301 req->PersistentFileId = persistent_fid;
4302 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004303 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004304 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004305 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004306 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004307 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004308
4309 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004310 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004311 return 0;
4312}
4313
Steve French2d304212018-06-24 23:28:12 -05004314int
4315SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4316 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4317{
4318 struct smb_rqst rqst;
4319 struct smb2_query_info_rsp *rsp = NULL;
4320 struct kvec iov;
4321 struct kvec rsp_iov;
4322 int rc = 0;
4323 int resp_buftype;
4324 struct cifs_ses *ses = tcon->ses;
4325 FILE_SYSTEM_POSIX_INFO *info = NULL;
4326 int flags = 0;
4327
4328 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4329 sizeof(FILE_SYSTEM_POSIX_INFO),
4330 persistent_fid, volatile_fid);
4331 if (rc)
4332 return rc;
4333
4334 if (smb3_encryption_required(tcon))
4335 flags |= CIFS_TRANSFORM_REQ;
4336
4337 memset(&rqst, 0, sizeof(struct smb_rqst));
4338 rqst.rq_iov = &iov;
4339 rqst.rq_nvec = 1;
4340
4341 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4342 cifs_small_buf_release(iov.iov_base);
4343 if (rc) {
4344 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4345 goto posix_qfsinf_exit;
4346 }
4347 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4348
4349 info = (FILE_SYSTEM_POSIX_INFO *)(
4350 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004351 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4352 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4353 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05004354 if (!rc)
4355 copy_posix_fs_info_to_kstatfs(info, fsdata);
4356
4357posix_qfsinf_exit:
4358 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4359 return rc;
4360}
Steve French2d304212018-06-24 23:28:12 -05004361
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004362int
4363SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4364 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4365{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004366 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004367 struct smb2_query_info_rsp *rsp = NULL;
4368 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004369 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004370 int rc = 0;
4371 int resp_buftype;
4372 struct cifs_ses *ses = tcon->ses;
4373 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004374 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004375
4376 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4377 sizeof(struct smb2_fs_full_size_info),
4378 persistent_fid, volatile_fid);
4379 if (rc)
4380 return rc;
4381
Steve French5a77e752018-05-09 17:43:08 -05004382 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004383 flags |= CIFS_TRANSFORM_REQ;
4384
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004385 memset(&rqst, 0, sizeof(struct smb_rqst));
4386 rqst.rq_iov = &iov;
4387 rqst.rq_nvec = 1;
4388
4389 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004390 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004391 if (rc) {
4392 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05004393 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004394 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004395 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004396
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004397 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004398 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004399 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4400 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4401 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004402 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004403 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004404
Steve French34f62642013-10-09 02:07:00 -05004405qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004406 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004407 return rc;
4408}
4409
4410int
4411SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05004412 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05004413{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004414 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05004415 struct smb2_query_info_rsp *rsp = NULL;
4416 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004417 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05004418 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05004419 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05004420 struct cifs_ses *ses = tcon->ses;
4421 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004422 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05004423
Steven French21671142013-10-09 13:36:35 -05004424 if (level == FS_DEVICE_INFORMATION) {
4425 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4426 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4427 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4428 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4429 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004430 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4431 max_len = sizeof(struct smb3_fs_ss_info);
4432 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05004433 } else if (level == FS_VOLUME_INFORMATION) {
4434 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4435 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05004436 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004437 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05004438 return -EINVAL;
4439 }
4440
4441 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05004442 persistent_fid, volatile_fid);
4443 if (rc)
4444 return rc;
4445
Steve French5a77e752018-05-09 17:43:08 -05004446 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004447 flags |= CIFS_TRANSFORM_REQ;
4448
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004449 memset(&rqst, 0, sizeof(struct smb_rqst));
4450 rqst.rq_iov = &iov;
4451 rqst.rq_nvec = 1;
4452
4453 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004454 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004455 if (rc) {
4456 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4457 goto qfsattr_exit;
4458 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004459 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05004460
4461 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4462 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004463 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05004464 if (rc)
4465 goto qfsattr_exit;
4466
4467 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004468 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004469 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05004470 rsp_len, max_len));
4471 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004472 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004473 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004474 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4475 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004476 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004477 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4478 tcon->perf_sector_size =
4479 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05004480 } else if (level == FS_VOLUME_INFORMATION) {
4481 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4482 (offset + (char *)rsp);
4483 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4484 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004485 }
Steve French34f62642013-10-09 02:07:00 -05004486
4487qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004488 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004489 return rc;
4490}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004491
4492int
4493smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4494 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4495 const __u32 num_lock, struct smb2_lock_element *buf)
4496{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004497 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004498 int rc = 0;
4499 struct smb2_lock_req *req = NULL;
4500 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004501 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004502 int resp_buf_type;
4503 unsigned int count;
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004504 int flags = CIFS_NO_RSP_BUF;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004505 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004506
Joe Perchesf96637b2013-05-04 22:12:25 -05004507 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004508
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004509 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004510 if (rc)
4511 return rc;
4512
Steve French5a77e752018-05-09 17:43:08 -05004513 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004514 flags |= CIFS_TRANSFORM_REQ;
4515
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004516 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004517 req->LockCount = cpu_to_le16(num_lock);
4518
4519 req->PersistentFileId = persist_fid;
4520 req->VolatileFileId = volatile_fid;
4521
4522 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004523
4524 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004525 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004526 iov[1].iov_base = (char *)buf;
4527 iov[1].iov_len = count;
4528
4529 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004530
4531 memset(&rqst, 0, sizeof(struct smb_rqst));
4532 rqst.rq_iov = iov;
4533 rqst.rq_nvec = 2;
4534
4535 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004536 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004537 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004538 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004539 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004540 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004541 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4542 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004543 }
4544
4545 return rc;
4546}
4547
4548int
4549SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4550 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4551 const __u64 length, const __u64 offset, const __u32 lock_flags,
4552 const bool wait)
4553{
4554 struct smb2_lock_element lock;
4555
4556 lock.Offset = cpu_to_le64(offset);
4557 lock.Length = cpu_to_le64(length);
4558 lock.Flags = cpu_to_le32(lock_flags);
4559 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4560 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4561
4562 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4563}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004564
4565int
4566SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4567 __u8 *lease_key, const __le32 lease_state)
4568{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004569 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004570 int rc;
4571 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004572 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004573 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004574 unsigned int total_len;
4575 struct kvec iov[1];
4576 struct kvec rsp_iov;
4577 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05004578 __u64 *please_key_high;
4579 __u64 *please_key_low;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004580
Joe Perchesf96637b2013-05-04 22:12:25 -05004581 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004582 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4583 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004584 if (rc)
4585 return rc;
4586
Steve French5a77e752018-05-09 17:43:08 -05004587 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004588 flags |= CIFS_TRANSFORM_REQ;
4589
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004590 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004591 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004592 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004593
4594 memcpy(req->LeaseKey, lease_key, 16);
4595 req->LeaseState = lease_state;
4596
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004597 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004598
4599 iov[0].iov_base = (char *)req;
4600 iov[0].iov_len = total_len;
4601
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004602 memset(&rqst, 0, sizeof(struct smb_rqst));
4603 rqst.rq_iov = iov;
4604 rqst.rq_nvec = 1;
4605
4606 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004607 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004608
Aurelien Apteld339adc2019-01-31 13:46:07 +01004609 please_key_low = (__u64 *)lease_key;
4610 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004611 if (rc) {
4612 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05004613 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4614 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05004615 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05004616 } else
4617 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4618 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004619
4620 return rc;
4621}