blob: 06463f386a60c0aa7fc7879dac80be654af07404 [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;
Paulo Alcantarae4af35f2020-05-19 15:38:28 -0300163 struct TCP_Server_Info *server = tcon->ses->server;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200164 struct dfs_cache_tgt_list tl;
165 struct dfs_cache_tgt_iterator *it = NULL;
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100166 char *tree;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200167 const char *tcp_host;
168 size_t tcp_host_len;
169 const char *dfs_host;
170 size_t dfs_host_len;
171
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100172 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
173 if (!tree)
174 return -ENOMEM;
175
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100176 if (!tcon->dfs_path) {
Paulo Alcantarae4af35f2020-05-19 15:38:28 -0300177 if (tcon->ipc) {
178 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
179 server->hostname);
180 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
181 } else {
182 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon,
183 nlsc);
184 }
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100185 goto out;
186 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200187
188 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
189 if (rc)
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100190 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200191
Paulo Alcantarae4af35f2020-05-19 15:38:28 -0300192 extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200193
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300196 const char *share, *prefix;
197 size_t share_len, prefix_len;
Paulo Alcantarae4af35f2020-05-19 15:38:28 -0300198 bool target_match;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200199
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300200 rc = dfs_cache_get_tgt_share(it, &share, &share_len, &prefix,
201 &prefix_len);
202 if (rc) {
203 cifs_dbg(VFS, "%s: failed to parse target share %d\n",
204 __func__, rc);
205 continue;
206 }
207
208 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200209
210 if (dfs_host_len != tcp_host_len
211 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
Steve Frenchadbb2da2020-05-30 16:45:11 -0500212 cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n",
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200213 __func__,
214 (int)dfs_host_len, dfs_host,
215 (int)tcp_host_len, tcp_host);
Paulo Alcantarae4af35f2020-05-19 15:38:28 -0300216
217 rc = match_target_ip(server, dfs_host, dfs_host_len,
218 &target_match);
219 if (rc) {
220 cifs_dbg(VFS, "%s: failed to match target ip: %d\n",
221 __func__, rc);
222 break;
223 }
224
225 if (!target_match) {
226 cifs_dbg(FYI, "%s: skipping target\n", __func__);
227 continue;
228 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200229 }
230
Paulo Alcantarae4af35f2020-05-19 15:38:28 -0300231 if (tcon->ipc) {
232 scnprintf(tree, MAX_TREE_SIZE, "\\\\%.*s\\IPC$",
233 (int)share_len, share);
234 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
235 } else {
236 scnprintf(tree, MAX_TREE_SIZE, "\\%.*s", (int)share_len,
237 share);
238 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
239 if (!rc) {
240 rc = update_super_prepath(tcon, prefix,
241 prefix_len);
242 break;
243 }
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300244 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200245 if (rc == -EREMOTE)
246 break;
247 }
248
249 if (!rc) {
250 if (it)
251 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
252 it);
253 else
254 rc = -ENOENT;
255 }
256 dfs_cache_free_tgts(&tl);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100257out:
258 kfree(tree);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200259 return rc;
260}
261#else
262static inline int __smb2_reconnect(const struct nls_table *nlsc,
263 struct cifs_tcon *tcon)
264{
265 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
266}
267#endif
268
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400269static int
270smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
271{
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300272 int rc;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400273 struct nls_table *nls_codepage;
274 struct cifs_ses *ses;
275 struct TCP_Server_Info *server;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200276 int retries;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400277
278 /*
279 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
280 * check for tcp and smb session status done differently
281 * for those three - in the calling routine.
282 */
283 if (tcon == NULL)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300284 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400285
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300286 if (smb2_command == SMB2_TREE_CONNECT)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300287 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400288
289 if (tcon->tidStatus == CifsExiting) {
290 /*
291 * only tree disconnect, open, and write,
292 * (and ulogoff which does not have tcon)
293 * are allowed as we start force umount.
294 */
295 if ((smb2_command != SMB2_WRITE) &&
296 (smb2_command != SMB2_CREATE) &&
297 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500298 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
299 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400300 return -ENODEV;
301 }
302 }
303 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
304 (!tcon->ses->server))
305 return -EIO;
306
307 ses = tcon->ses;
308 server = ses->server;
309
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200310 retries = server->nr_targets;
311
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400312 /*
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200313 * Give demultiplex thread up to 10 seconds to each target available for
314 * reconnect -- should be greater than cifs socket timeout which is 7
315 * seconds.
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400316 */
317 while (server->tcpStatus == CifsNeedReconnect) {
318 /*
319 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
320 * here since they are implicitly done when session drops.
321 */
322 switch (smb2_command) {
323 /*
324 * BB Should we keep oplock break and add flush to exceptions?
325 */
326 case SMB2_TREE_DISCONNECT:
327 case SMB2_CANCEL:
328 case SMB2_CLOSE:
329 case SMB2_OPLOCK_BREAK:
330 return -EAGAIN;
331 }
332
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300333 rc = wait_event_interruptible_timeout(server->response_q,
334 (server->tcpStatus != CifsNeedReconnect),
335 10 * HZ);
336 if (rc < 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700337 cifs_dbg(FYI, "%s: aborting reconnect due to a received signal by the process\n",
338 __func__);
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300339 return -ERESTARTSYS;
340 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400341
342 /* are we still trying to reconnect? */
343 if (server->tcpStatus != CifsNeedReconnect)
344 break;
345
Ronnie Sahlbergc54849d2020-01-31 05:52:51 +1000346 if (retries && --retries)
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200347 continue;
348
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400349 /*
350 * on "soft" mounts we wait once. Hard mounts keep
351 * retrying until process is killed or server comes
352 * back on-line
353 */
354 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500355 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400356 return -EHOSTDOWN;
357 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200358 retries = server->nr_targets;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400359 }
360
361 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300362 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400363
364 nls_codepage = load_nls_default();
365
366 /*
367 * need to prevent multiple threads trying to simultaneously reconnect
368 * the same SMB session
369 */
370 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200371
372 /*
373 * Recheck after acquire mutex. If another thread is negotiating
374 * and the server never sends an answer the socket will be closed
375 * and tcpStatus set to reconnect.
376 */
377 if (server->tcpStatus == CifsNeedReconnect) {
378 rc = -EHOSTDOWN;
379 mutex_unlock(&tcon->ses->session_mutex);
380 goto out;
381 }
382
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400383 rc = cifs_negotiate_protocol(0, tcon->ses);
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000384 if (!rc && tcon->ses->need_reconnect) {
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400385 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000386 if ((rc == -EACCES) && !tcon->retry) {
387 rc = -EHOSTDOWN;
388 mutex_unlock(&tcon->ses->session_mutex);
389 goto failed;
390 }
391 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400392 if (rc || !tcon->need_reconnect) {
393 mutex_unlock(&tcon->ses->session_mutex);
394 goto out;
395 }
396
397 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800398 if (tcon->use_persistent)
399 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500400
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200401 rc = __smb2_reconnect(nls_codepage, tcon);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400402 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500403
Joe Perchesf96637b2013-05-04 22:12:25 -0500404 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500405 if (rc) {
406 /* If sess reconnected but tcon didn't, something strange ... */
Joe Perchesa0a30362020-04-14 22:42:53 -0700407 pr_warn_once("reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400408 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500409 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800410
411 if (smb2_command != SMB2_INTERNAL_CMD)
Stefan Metzmacherb08484d2020-02-24 14:14:59 +0100412 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800413
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400414 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400415out:
416 /*
417 * Check if handle based operation so we know whether we can continue
418 * or not without returning to caller to reset file handle.
419 */
420 /*
421 * BB Is flush done by server on drop of tcp session? Should we special
422 * case it and skip above?
423 */
424 switch (smb2_command) {
425 case SMB2_FLUSH:
426 case SMB2_READ:
427 case SMB2_WRITE:
428 case SMB2_LOCK:
429 case SMB2_IOCTL:
430 case SMB2_QUERY_DIRECTORY:
431 case SMB2_CHANGE_NOTIFY:
432 case SMB2_QUERY_INFO:
433 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800434 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400435 }
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000436failed:
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400437 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400438 return rc;
439}
440
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700441static void
442fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
443 unsigned int *total_len)
444{
445 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
446 /* lookup word count ie StructureSize from table */
447 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
448
449 /*
450 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
451 * largest operations (Create)
452 */
453 memset(buf, 0, 256);
454
455 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
456 spdu->StructureSize2 = cpu_to_le16(parmsize);
457
458 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
459}
460
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100461/*
462 * Allocate and return pointer to an SMB request hdr, and set basic
463 * SMB information in the SMB header. If the return code is zero, this
464 * function must have filled in request_buf pointer.
465 */
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300466static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
467 void **request_buf, unsigned int *total_len)
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800468{
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800469 /* BB eventually switch this to SMB2 specific small buf size */
Stefano Briviof46ecbd2018-07-05 11:46:42 +0200470 if (smb2_command == SMB2_SET_INFO)
471 *request_buf = cifs_buf_get();
472 else
473 *request_buf = cifs_small_buf_get();
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800474 if (*request_buf == NULL) {
475 /* BB should we add a retry in here if not a writepage? */
476 return -ENOMEM;
477 }
478
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100479 fill_small_buf(smb2_command, tcon,
480 (struct smb2_sync_hdr *)(*request_buf),
481 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400482
483 if (tcon != NULL) {
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400484 uint16_t com_code = le16_to_cpu(smb2_command);
485 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400486 cifs_stats_inc(&tcon->num_smbs_sent);
487 }
488
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300489 return 0;
490}
491
492static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
493 void **request_buf, unsigned int *total_len)
494{
495 int rc;
496
497 rc = smb2_reconnect(smb2_command, tcon);
498 if (rc)
499 return rc;
500
501 return __smb2_plain_req_init(smb2_command, tcon, request_buf,
502 total_len);
503}
504
505static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
506 void **request_buf, unsigned int *total_len)
507{
508 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
509 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
510 return __smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf,
511 total_len);
512 }
513 return smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf, total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400514}
515
Steve Frenchd7bef4c2019-04-18 11:03:58 -0500516/* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500517
518static void
519build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
520{
521 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
522 pneg_ctxt->DataLength = cpu_to_le16(38);
523 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
524 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
525 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
526 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
527}
528
529static void
Steve French26ea8882019-04-26 20:36:08 -0700530build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
531{
532 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
533 pneg_ctxt->DataLength =
534 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
535 - sizeof(struct smb2_neg_context));
536 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
537 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
538 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
539 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
540}
541
542static void
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500543build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
544{
545 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French9ac63ec2019-06-07 08:59:40 -0500546 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
547 pneg_ctxt->CipherCount = cpu_to_le16(2);
548 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
549 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500550}
551
Steve French96d3cca2019-06-25 04:39:51 -0500552static unsigned int
553build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
554{
555 struct nls_table *cp = load_nls_default();
556
557 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
558
559 /* copy up to max of first 100 bytes of server name to NetName field */
Steve Frenchdf58fae2019-08-05 17:07:26 -0500560 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
Steve French96d3cca2019-06-25 04:39:51 -0500561 /* context size is DataLength + minimal smb2_neg_context */
562 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
563 sizeof(struct smb2_neg_context), 8) * 8;
564}
565
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500566static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500567build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
568{
569 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
570 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
Steve French0d481322019-02-24 17:56:33 -0600571 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
572 pneg_ctxt->Name[0] = 0x93;
573 pneg_ctxt->Name[1] = 0xAD;
574 pneg_ctxt->Name[2] = 0x25;
575 pneg_ctxt->Name[3] = 0x50;
576 pneg_ctxt->Name[4] = 0x9C;
577 pneg_ctxt->Name[5] = 0xB4;
578 pneg_ctxt->Name[6] = 0x11;
579 pneg_ctxt->Name[7] = 0xE7;
580 pneg_ctxt->Name[8] = 0xB4;
581 pneg_ctxt->Name[9] = 0x23;
582 pneg_ctxt->Name[10] = 0x83;
583 pneg_ctxt->Name[11] = 0xDE;
584 pneg_ctxt->Name[12] = 0x96;
585 pneg_ctxt->Name[13] = 0x8B;
586 pneg_ctxt->Name[14] = 0xCD;
587 pneg_ctxt->Name[15] = 0x7C;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500588}
589
590static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100591assemble_neg_contexts(struct smb2_negotiate_req *req,
Steve French9fe5ff12019-06-24 20:39:04 -0500592 struct TCP_Server_Info *server, unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500593{
Colin Ian Kinga9f76cf2019-12-02 18:59:42 +0000594 char *pneg_ctxt;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500595 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500596
Steve Frenchd5c70762019-01-03 02:37:21 -0600597 if (*total_len > 200) {
598 /* In case length corrupted don't want to overrun smb buffer */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000599 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
Steve Frenchd5c70762019-01-03 02:37:21 -0600600 return;
601 }
602
603 /*
604 * round up total_len of fixed part of SMB3 negotiate request to 8
605 * byte boundary before adding negotiate contexts
606 */
607 *total_len = roundup(*total_len, 8);
608
609 pneg_ctxt = (*total_len) + (char *)req;
610 req->NegotiateContextOffset = cpu_to_le32(*total_len);
611
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500612 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500613 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
614 *total_len += ctxt_len;
615 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100616
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500617 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500618 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
619 *total_len += ctxt_len;
620 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100621
Steve French9fe5ff12019-06-24 20:39:04 -0500622 if (server->compress_algorithm) {
623 build_compression_ctxt((struct smb2_compression_capabilities_context *)
Steve French26ea8882019-04-26 20:36:08 -0700624 pneg_ctxt);
Steve French9fe5ff12019-06-24 20:39:04 -0500625 ctxt_len = DIV_ROUND_UP(
626 sizeof(struct smb2_compression_capabilities_context),
627 8) * 8;
628 *total_len += ctxt_len;
629 pneg_ctxt += ctxt_len;
Steve French96d3cca2019-06-25 04:39:51 -0500630 req->NegotiateContextCount = cpu_to_le16(5);
Steve French9fe5ff12019-06-24 20:39:04 -0500631 } else
Steve French96d3cca2019-06-25 04:39:51 -0500632 req->NegotiateContextCount = cpu_to_le16(4);
633
634 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
635 server->hostname);
636 *total_len += ctxt_len;
637 pneg_ctxt += ctxt_len;
638
Steve Frenchfcef0db2018-05-19 20:45:27 -0500639 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
640 *total_len += sizeof(struct smb2_posix_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500641}
Steve French5100d8a2018-04-09 10:47:14 -0500642
643static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
644{
645 unsigned int len = le16_to_cpu(ctxt->DataLength);
646
647 /* If invalid preauth context warn but use what we requested, SHA-512 */
648 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700649 pr_warn_once("server sent bad preauth context\n");
Steve French5100d8a2018-04-09 10:47:14 -0500650 return;
651 }
652 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
Joe Perchesa0a30362020-04-14 22:42:53 -0700653 pr_warn_once("Invalid SMB3 hash algorithm count\n");
Steve French5100d8a2018-04-09 10:47:14 -0500654 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
Joe Perchesa0a30362020-04-14 22:42:53 -0700655 pr_warn_once("unknown SMB3 hash algorithm\n");
Steve French5100d8a2018-04-09 10:47:14 -0500656}
657
Steve French26ea8882019-04-26 20:36:08 -0700658static void decode_compress_ctx(struct TCP_Server_Info *server,
659 struct smb2_compression_capabilities_context *ctxt)
660{
661 unsigned int len = le16_to_cpu(ctxt->DataLength);
662
663 /* sizeof compress context is a one element compression capbility struct */
664 if (len < 10) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700665 pr_warn_once("server sent bad compression cntxt\n");
Steve French26ea8882019-04-26 20:36:08 -0700666 return;
667 }
668 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700669 pr_warn_once("Invalid SMB3 compress algorithm count\n");
Steve French26ea8882019-04-26 20:36:08 -0700670 return;
671 }
672 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700673 pr_warn_once("unknown compression algorithm\n");
Steve French26ea8882019-04-26 20:36:08 -0700674 return;
675 }
676 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
677}
678
Steve French5100d8a2018-04-09 10:47:14 -0500679static int decode_encrypt_ctx(struct TCP_Server_Info *server,
680 struct smb2_encryption_neg_context *ctxt)
681{
682 unsigned int len = le16_to_cpu(ctxt->DataLength);
683
684 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
685 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700686 pr_warn_once("server sent bad crypto ctxt len\n");
Steve French5100d8a2018-04-09 10:47:14 -0500687 return -EINVAL;
688 }
689
690 if (le16_to_cpu(ctxt->CipherCount) != 1) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700691 pr_warn_once("Invalid SMB3.11 cipher count\n");
Steve French5100d8a2018-04-09 10:47:14 -0500692 return -EINVAL;
693 }
694 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
695 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
696 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700697 pr_warn_once("Invalid SMB3.11 cipher returned\n");
Steve French5100d8a2018-04-09 10:47:14 -0500698 return -EINVAL;
699 }
700 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500701 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500702 return 0;
703}
704
705static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000706 struct TCP_Server_Info *server,
707 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500708{
709 struct smb2_neg_context *pctx;
710 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
711 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500712 unsigned int len_of_ctxts, i;
713 int rc = 0;
714
715 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
716 if (len_of_smb <= offset) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000717 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
Steve French5100d8a2018-04-09 10:47:14 -0500718 return -EINVAL;
719 }
720
721 len_of_ctxts = len_of_smb - offset;
722
723 for (i = 0; i < ctxt_cnt; i++) {
724 int clen;
725 /* check that offset is not beyond end of SMB */
726 if (len_of_ctxts == 0)
727 break;
728
729 if (len_of_ctxts < sizeof(struct smb2_neg_context))
730 break;
731
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000732 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500733 clen = le16_to_cpu(pctx->DataLength);
734 if (clen > len_of_ctxts)
735 break;
736
737 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
738 decode_preauth_context(
739 (struct smb2_preauth_neg_context *)pctx);
740 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
741 rc = decode_encrypt_ctx(server,
742 (struct smb2_encryption_neg_context *)pctx);
Steve French26ea8882019-04-26 20:36:08 -0700743 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
744 decode_compress_ctx(server,
745 (struct smb2_compression_capabilities_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500746 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
747 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500748 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000749 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
Steve French5100d8a2018-04-09 10:47:14 -0500750 le16_to_cpu(pctx->ContextType));
751
752 if (rc)
753 break;
754 /* offsets must be 8 byte aligned */
755 clen = (clen + 7) & ~0x7;
756 offset += clen + sizeof(struct smb2_neg_context);
757 len_of_ctxts -= clen;
758 }
759 return rc;
760}
761
Steve Frenchce558b02018-05-31 19:16:54 -0500762static struct create_posix *
763create_posix_buf(umode_t mode)
764{
765 struct create_posix *buf;
766
767 buf = kzalloc(sizeof(struct create_posix),
768 GFP_KERNEL);
769 if (!buf)
770 return NULL;
771
772 buf->ccontext.DataOffset =
773 cpu_to_le16(offsetof(struct create_posix, Mode));
774 buf->ccontext.DataLength = cpu_to_le32(4);
775 buf->ccontext.NameOffset =
776 cpu_to_le16(offsetof(struct create_posix, Name));
777 buf->ccontext.NameLength = cpu_to_le16(16);
778
779 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
780 buf->Name[0] = 0x93;
781 buf->Name[1] = 0xAD;
782 buf->Name[2] = 0x25;
783 buf->Name[3] = 0x50;
784 buf->Name[4] = 0x9C;
785 buf->Name[5] = 0xB4;
786 buf->Name[6] = 0x11;
787 buf->Name[7] = 0xE7;
788 buf->Name[8] = 0xB4;
789 buf->Name[9] = 0x23;
790 buf->Name[10] = 0x83;
791 buf->Name[11] = 0xDE;
792 buf->Name[12] = 0x96;
793 buf->Name[13] = 0x8B;
794 buf->Name[14] = 0xCD;
795 buf->Name[15] = 0x7C;
796 buf->Mode = cpu_to_le32(mode);
Joe Perchesa0a30362020-04-14 22:42:53 -0700797 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
Steve Frenchce558b02018-05-31 19:16:54 -0500798 return buf;
799}
800
801static int
802add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
803{
804 struct smb2_create_req *req = iov[0].iov_base;
805 unsigned int num = *num_iovec;
806
807 iov[num].iov_base = create_posix_buf(mode);
Steve Frenchd0959b02019-10-05 10:53:58 -0500808 if (mode == ACL_NO_MODE)
Joe Perchesa0a30362020-04-14 22:42:53 -0700809 cifs_dbg(FYI, "Invalid mode\n");
Steve Frenchce558b02018-05-31 19:16:54 -0500810 if (iov[num].iov_base == NULL)
811 return -ENOMEM;
812 iov[num].iov_len = sizeof(struct create_posix);
813 if (!req->CreateContextsOffset)
814 req->CreateContextsOffset = cpu_to_le32(
815 sizeof(struct smb2_create_req) +
816 iov[num - 1].iov_len);
817 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
818 *num_iovec = num + 1;
819 return 0;
820}
821
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500822
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400823/*
824 *
825 * SMB2 Worker functions follow:
826 *
827 * The general structure of the worker functions is:
828 * 1) Call smb2_init (assembles SMB2 header)
829 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
830 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
831 * 4) Decode SMB2 command specific fields in the fixed length area
832 * 5) Decode variable length data area (if any for this SMB2 command type)
833 * 6) Call free smb buffer
834 * 7) return
835 *
836 */
837
838int
839SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
840{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000841 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400842 struct smb2_negotiate_req *req;
843 struct smb2_negotiate_rsp *rsp;
844 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700845 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400846 int rc = 0;
847 int resp_buftype;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200848 struct TCP_Server_Info *server = cifs_ses_server(ses);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400849 int blob_offset, blob_length;
850 char *security_blob;
851 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100852 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400853
Joe Perchesf96637b2013-05-04 22:12:25 -0500854 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400855
Jeff Layton3534b852013-05-24 07:41:01 -0400856 if (!server) {
857 WARN(1, "%s: server is NULL!\n", __func__);
858 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400859 }
860
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100861 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400862 if (rc)
863 return rc;
864
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100865 req->sync_hdr.SessionId = 0;
Steve French0fdfef92018-06-28 19:30:23 -0500866
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100867 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
868 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400869
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200870 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500871 SMB3ANY_VERSION_STRING) == 0) {
872 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
873 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
874 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100875 total_len += 4;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000876 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500877 SMBDEFAULT_VERSION_STRING) == 0) {
878 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
879 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
880 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600881 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
882 req->DialectCount = cpu_to_le16(4);
883 total_len += 8;
Steve French9764c022017-09-17 10:41:35 -0500884 } else {
885 /* otherwise send specific dialect */
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200886 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
Steve French9764c022017-09-17 10:41:35 -0500887 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100888 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500889 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400890
891 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400892 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500893 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400894 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500895 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400896 else
897 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400898
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000899 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400900
Steve French3c5f9be12014-05-13 13:37:45 -0700901 /* ClientGUID must be zero for SMB2.02 dialect */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000902 if (server->vals->protocol_id == SMB20_PROT_ID)
Steve French3c5f9be12014-05-13 13:37:45 -0700903 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500904 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700905 memcpy(req->ClientGUID, server->client_guid,
906 SMB2_CLIENT_GUID_SIZE);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000907 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
908 (strcmp(server->vals->version_string,
Steve Frenchd5c70762019-01-03 02:37:21 -0600909 SMBDEFAULT_VERSION_STRING) == 0))
Steve French9fe5ff12019-06-24 20:39:04 -0500910 assemble_neg_contexts(req, server, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500911 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400912 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100913 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400914
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000915 memset(&rqst, 0, sizeof(struct smb_rqst));
916 rqst.rq_iov = iov;
917 rqst.rq_nvec = 1;
918
919 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700920 cifs_small_buf_release(req);
921 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400922 /*
923 * No tcon so can't do
924 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
925 */
Steve French7e682f72017-08-31 21:34:24 -0500926 if (rc == -EOPNOTSUPP) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700927 cifs_server_dbg(VFS, "Dialect not supported by server. Consider specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n");
Steve French7e682f72017-08-31 21:34:24 -0500928 goto neg_exit;
929 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400930 goto neg_exit;
931
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000932 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500933 SMB3ANY_VERSION_STRING) == 0) {
934 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000935 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500936 "SMB2 dialect returned but not requested\n");
937 return -EIO;
938 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000939 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500940 "SMB2.1 dialect returned but not requested\n");
941 return -EIO;
942 }
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000943 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500944 SMBDEFAULT_VERSION_STRING) == 0) {
945 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000946 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500947 "SMB2 dialect returned but not requested\n");
948 return -EIO;
949 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
950 /* ops set to 3.0 by default for default so update */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000951 server->ops = &smb21_operations;
952 server->vals = &smb21_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800953 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000954 server->ops = &smb311_operations;
955 server->vals = &smb311_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800956 }
Steve French590d08d2017-09-19 11:43:47 -0500957 } else if (le16_to_cpu(rsp->DialectRevision) !=
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000958 server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500959 /* if requested single dialect ensure returned dialect matched */
Joe Perchesa0a30362020-04-14 22:42:53 -0700960 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
961 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500962 return -EIO;
963 }
964
Joe Perchesf96637b2013-05-04 22:12:25 -0500965 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400966
Steve Frenche4aa25e2012-10-01 12:26:22 -0500967 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500968 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500969 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500970 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500971 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500972 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500973 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
974 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600975 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
976 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400977 else {
Joe Perchesa0a30362020-04-14 22:42:53 -0700978 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
979 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400980 rc = -EIO;
981 goto neg_exit;
982 }
983 server->dialect = le16_to_cpu(rsp->DialectRevision);
984
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100985 /*
986 * Keep a copy of the hash after negprot. This hash will be
987 * the starting hash value for all sessions made from this
988 * server.
989 */
990 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
991 SMB2_PREAUTH_HASH_SIZE);
Steve French0fdfef92018-06-28 19:30:23 -0500992
Jeff Laytone598d1d82013-05-26 07:00:59 -0400993 /* SMB2 only has an extended negflavor */
994 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400995 /* set it to the maximum buffer size value we can send with 1 credit */
996 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
997 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400998 server->max_read = le32_to_cpu(rsp->MaxReadSize);
999 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001000 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -05001001 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1002 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1003 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001004 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001005 /* Internal types */
1006 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001007
1008 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001009 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -05001010 /*
1011 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1012 * for us will be
1013 * ses->sectype = RawNTLMSSP;
1014 * but for time being this is our only auth choice so doesn't matter.
1015 * We just found a server which sets blob length to zero expecting raw.
1016 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -07001017 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -05001018 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -07001019 server->sec_ntlmssp = true;
1020 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001021
Jeff Layton38d77c52013-05-26 07:01:00 -04001022 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -04001023 if (rc)
1024 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -05001025 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -05001026 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -05001027 if (rc == 1)
1028 rc = 0;
1029 else if (rc == 0)
1030 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001031 }
Steve French5100d8a2018-04-09 10:47:14 -05001032
Steve French5100d8a2018-04-09 10:47:14 -05001033 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1034 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10001035 rc = smb311_decode_neg_context(rsp, server,
1036 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -05001037 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001038 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
Steve French5100d8a2018-04-09 10:47:14 -05001039 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001040neg_exit:
1041 free_rsp_buf(resp_buftype, rsp);
1042 return rc;
1043}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001044
Steve Frenchff1c0382013-11-19 23:44:46 -06001045int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1046{
Long Li2796d302018-04-25 11:30:04 -07001047 int rc;
1048 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001049 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -06001050 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -05001051 u32 inbuflen; /* max of 4 dialects */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001052 struct TCP_Server_Info *server = tcon->ses->server;
Steve Frenchff1c0382013-11-19 23:44:46 -06001053
1054 cifs_dbg(FYI, "validate negotiate\n");
1055
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001056 /* In SMB3.11 preauth integrity supersedes validate negotiate */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001057 if (server->dialect == SMB311_PROT_ID)
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001058 return 0;
1059
Steve Frenchff1c0382013-11-19 23:44:46 -06001060 /*
1061 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -05001062 * can not sign it (ie are not known user). Even if signing is not
1063 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -06001064 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -05001065 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -06001066 */
Steve French0603c962017-09-20 19:57:18 -05001067 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -06001068 return 0; /* validation requires signing */
1069
Steve French0603c962017-09-20 19:57:18 -05001070 if (tcon->ses->user_name == NULL) {
1071 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1072 return 0; /* validation requires signing */
1073 }
1074
1075 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001076 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
Steve French0603c962017-09-20 19:57:18 -05001077
Long Li2796d302018-04-25 11:30:04 -07001078 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1079 if (!pneg_inbuf)
1080 return -ENOMEM;
1081
1082 pneg_inbuf->Capabilities =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001083 cpu_to_le32(server->vals->req_capabilities);
1084 memcpy(pneg_inbuf->Guid, server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +01001085 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -06001086
1087 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -07001088 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001089 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1090 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -07001091 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001092 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1093 else
Long Li2796d302018-04-25 11:30:04 -07001094 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001095
Steve French9764c022017-09-17 10:41:35 -05001096
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001097 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001098 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001099 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1100 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1101 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -05001102 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -07001103 inbuflen = sizeof(*pneg_inbuf) -
Steve Frenchd5c70762019-01-03 02:37:21 -06001104 (2 * sizeof(pneg_inbuf->Dialects[0]));
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001105 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001106 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001107 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1108 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1109 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -06001110 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1111 pneg_inbuf->DialectCount = cpu_to_le16(4);
Steve French9764c022017-09-17 10:41:35 -05001112 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -07001113 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -05001114 } else {
1115 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -07001116 pneg_inbuf->Dialects[0] =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001117 cpu_to_le16(server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -07001118 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -05001119 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -07001120 inbuflen = sizeof(*pneg_inbuf) -
1121 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -05001122 }
Steve Frenchff1c0382013-11-19 23:44:46 -06001123
1124 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1125 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001126 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1127 (char **)&pneg_rsp, &rsplen);
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001128 if (rc == -EOPNOTSUPP) {
1129 /*
1130 * Old Windows versions or Netapp SMB server can return
1131 * not supported error. Client should accept it.
1132 */
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001133 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
Colin Ian King21078202019-05-17 09:12:33 +01001134 rc = 0;
1135 goto out_free_inbuf;
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001136 } else if (rc != 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001137 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1138 rc);
Long Li2796d302018-04-25 11:30:04 -07001139 rc = -EIO;
1140 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -06001141 }
1142
Long Li2796d302018-04-25 11:30:04 -07001143 rc = -EIO;
1144 if (rsplen != sizeof(*pneg_rsp)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001145 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1146 rsplen);
Steve French7db0a6e2017-05-03 21:12:20 -05001147
1148 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -07001149 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1150 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001151 }
1152
1153 /* check validate negotiate info response matches what we got earlier */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001154 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -06001155 goto vneg_out;
1156
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001157 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
Steve Frenchff1c0382013-11-19 23:44:46 -06001158 goto vneg_out;
1159
1160 /* do not validate server guid because not saved at negprot time yet */
1161
1162 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001163 SMB2_LARGE_FILES) != server->capabilities)
Steve Frenchff1c0382013-11-19 23:44:46 -06001164 goto vneg_out;
1165
1166 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -07001167 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001168 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -07001169 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001170
1171vneg_out:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001172 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -07001173out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001174 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -07001175out_free_inbuf:
1176 kfree(pneg_inbuf);
1177 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -06001178}
1179
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301180enum securityEnum
1181smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1182{
1183 switch (requested) {
1184 case Kerberos:
1185 case RawNTLMSSP:
1186 return requested;
1187 case NTLMv2:
1188 return RawNTLMSSP;
1189 case Unspecified:
1190 if (server->sec_ntlmssp &&
1191 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1192 return RawNTLMSSP;
1193 if ((server->sec_kerberos || server->sec_mskerberos) &&
1194 (global_secflags & CIFSSEC_MAY_KRB5))
1195 return Kerberos;
1196 /* Fallthrough */
1197 default:
1198 return Unspecified;
1199 }
1200}
1201
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001202struct SMB2_sess_data {
1203 unsigned int xid;
1204 struct cifs_ses *ses;
1205 struct nls_table *nls_cp;
1206 void (*func)(struct SMB2_sess_data *);
1207 int result;
1208 u64 previous_session;
1209
1210 /* we will send the SMB in three pieces:
1211 * a fixed length beginning part, an optional
1212 * SPNEGO blob (which can be zero length), and a
1213 * last part which will include the strings
1214 * and rest of bcc area. This allows us to avoid
1215 * a large buffer 17K allocation
1216 */
1217 int buf0_type;
1218 struct kvec iov[2];
1219};
1220
1221static int
1222SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1223{
1224 int rc;
1225 struct cifs_ses *ses = sess_data->ses;
1226 struct smb2_sess_setup_req *req;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001227 struct TCP_Server_Info *server = cifs_ses_server(ses);
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001228 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001229
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001230 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1231 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001232 if (rc)
1233 return rc;
1234
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001235 if (sess_data->ses->binding) {
1236 req->sync_hdr.SessionId = sess_data->ses->Suid;
1237 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1238 req->PreviousSessionId = 0;
1239 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1240 } else {
1241 /* First session, not a reauthenticate */
1242 req->sync_hdr.SessionId = 0;
1243 /*
1244 * if reconnect, we need to send previous sess id
1245 * otherwise it is 0
1246 */
1247 req->PreviousSessionId = sess_data->previous_session;
1248 req->Flags = 0; /* MBZ */
1249 }
Steve Frenchd4090142018-06-13 17:05:58 -05001250
1251 /* enough to enable echos and oplocks and one max size write */
1252 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001253
1254 /* only one of SMB2 signing flags may be set in SMB2 request */
1255 if (server->sign)
1256 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1257 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1258 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1259 else
1260 req->SecurityMode = 0;
1261
Steve French8d330962019-07-25 18:13:10 -05001262#ifdef CONFIG_CIFS_DFS_UPCALL
1263 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1264#else
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001265 req->Capabilities = 0;
Steve French8d330962019-07-25 18:13:10 -05001266#endif /* DFS_UPCALL */
1267
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001268 req->Channel = 0; /* MBZ */
1269
1270 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001271 /* 1 for pad */
1272 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001273 /*
1274 * This variable will be used to clear the buffer
1275 * allocated above in case of any error in the calling function.
1276 */
1277 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1278
1279 return 0;
1280}
1281
1282static void
1283SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1284{
1285 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1286 sess_data->buf0_type = CIFS_NO_BUFFER;
1287}
1288
1289static int
1290SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1291{
1292 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001293 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001294 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001295 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001296
1297 /* Testing shows that buffer offset must be at location of Buffer[0] */
1298 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001299 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001300 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1301
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001302 memset(&rqst, 0, sizeof(struct smb_rqst));
1303 rqst.rq_iov = sess_data->iov;
1304 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001305
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001306 /* BB add code to build os and lm fields */
1307 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1308 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001309 &sess_data->buf0_type,
1310 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001311 cifs_small_buf_release(sess_data->iov[0].iov_base);
1312 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001313
1314 return rc;
1315}
1316
1317static int
1318SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1319{
1320 int rc = 0;
1321 struct cifs_ses *ses = sess_data->ses;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001322 struct TCP_Server_Info *server = cifs_ses_server(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001323
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001324 mutex_lock(&server->srv_mutex);
1325 if (server->ops->generate_signingkey) {
1326 rc = server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001327 if (rc) {
1328 cifs_dbg(FYI,
1329 "SMB3 session key generation failed\n");
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001330 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001331 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001332 }
1333 }
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001334 if (!server->session_estab) {
1335 server->sequence_number = 0x2;
1336 server->session_estab = true;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001337 }
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001338 mutex_unlock(&server->srv_mutex);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001339
1340 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001341 /* keep existing ses state if binding */
1342 if (!ses->binding) {
1343 spin_lock(&GlobalMid_Lock);
1344 ses->status = CifsGood;
1345 ses->need_reconnect = false;
1346 spin_unlock(&GlobalMid_Lock);
1347 }
1348
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001349 return rc;
1350}
1351
1352#ifdef CONFIG_CIFS_UPCALL
1353static void
1354SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1355{
1356 int rc;
1357 struct cifs_ses *ses = sess_data->ses;
1358 struct cifs_spnego_msg *msg;
1359 struct key *spnego_key = NULL;
1360 struct smb2_sess_setup_rsp *rsp = NULL;
1361
1362 rc = SMB2_sess_alloc_buffer(sess_data);
1363 if (rc)
1364 goto out;
1365
1366 spnego_key = cifs_get_spnego_key(ses);
1367 if (IS_ERR(spnego_key)) {
1368 rc = PTR_ERR(spnego_key);
1369 spnego_key = NULL;
1370 goto out;
1371 }
1372
1373 msg = spnego_key->payload.data[0];
1374 /*
1375 * check version field to make sure that cifs.upcall is
1376 * sending us a response in an expected form
1377 */
1378 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001379 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1380 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001381 rc = -EKEYREJECTED;
1382 goto out_put_spnego_key;
1383 }
1384
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001385 /* keep session key if binding */
1386 if (!ses->binding) {
1387 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1388 GFP_KERNEL);
1389 if (!ses->auth_key.response) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001390 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001391 msg->sesskey_len);
1392 rc = -ENOMEM;
1393 goto out_put_spnego_key;
1394 }
1395 ses->auth_key.len = msg->sesskey_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001396 }
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001397
1398 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1399 sess_data->iov[1].iov_len = msg->secblob_len;
1400
1401 rc = SMB2_sess_sendreceive(sess_data);
1402 if (rc)
1403 goto out_put_spnego_key;
1404
1405 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001406 /* keep session id and flags if binding */
1407 if (!ses->binding) {
1408 ses->Suid = rsp->sync_hdr.SessionId;
1409 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1410 }
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001411
1412 rc = SMB2_sess_establish_session(sess_data);
1413out_put_spnego_key:
1414 key_invalidate(spnego_key);
1415 key_put(spnego_key);
1416out:
1417 sess_data->result = rc;
1418 sess_data->func = NULL;
1419 SMB2_sess_free_buffer(sess_data);
1420}
1421#else
1422static void
1423SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1424{
1425 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1426 sess_data->result = -EOPNOTSUPP;
1427 sess_data->func = NULL;
1428}
1429#endif
1430
Sachin Prabhu166cea42016-10-07 19:11:22 +01001431static void
1432SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1433
1434static void
1435SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1436{
1437 int rc;
1438 struct cifs_ses *ses = sess_data->ses;
1439 struct smb2_sess_setup_rsp *rsp = NULL;
1440 char *ntlmssp_blob = NULL;
1441 bool use_spnego = false; /* else use raw ntlmssp */
1442 u16 blob_length = 0;
1443
1444 /*
1445 * If memory allocation is successful, caller of this function
1446 * frees it.
1447 */
1448 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1449 if (!ses->ntlmssp) {
1450 rc = -ENOMEM;
1451 goto out_err;
1452 }
1453 ses->ntlmssp->sesskey_per_smbsess = true;
1454
1455 rc = SMB2_sess_alloc_buffer(sess_data);
1456 if (rc)
1457 goto out_err;
1458
1459 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1460 GFP_KERNEL);
1461 if (ntlmssp_blob == NULL) {
1462 rc = -ENOMEM;
1463 goto out;
1464 }
1465
1466 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1467 if (use_spnego) {
1468 /* BB eventually need to add this */
1469 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1470 rc = -EOPNOTSUPP;
1471 goto out;
1472 } else {
1473 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1474 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1475 }
1476 sess_data->iov[1].iov_base = ntlmssp_blob;
1477 sess_data->iov[1].iov_len = blob_length;
1478
1479 rc = SMB2_sess_sendreceive(sess_data);
1480 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1481
1482 /* If true, rc here is expected and not an error */
1483 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001484 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001485 rc = 0;
1486
1487 if (rc)
1488 goto out;
1489
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001490 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001491 le16_to_cpu(rsp->SecurityBufferOffset)) {
1492 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1493 le16_to_cpu(rsp->SecurityBufferOffset));
1494 rc = -EIO;
1495 goto out;
1496 }
1497 rc = decode_ntlmssp_challenge(rsp->Buffer,
1498 le16_to_cpu(rsp->SecurityBufferLength), ses);
1499 if (rc)
1500 goto out;
1501
1502 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1503
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001504 /* keep existing ses id and flags if binding */
1505 if (!ses->binding) {
1506 ses->Suid = rsp->sync_hdr.SessionId;
1507 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1508 }
Sachin Prabhu166cea42016-10-07 19:11:22 +01001509
1510out:
1511 kfree(ntlmssp_blob);
1512 SMB2_sess_free_buffer(sess_data);
1513 if (!rc) {
1514 sess_data->result = 0;
1515 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1516 return;
1517 }
1518out_err:
1519 kfree(ses->ntlmssp);
1520 ses->ntlmssp = NULL;
1521 sess_data->result = rc;
1522 sess_data->func = NULL;
1523}
1524
1525static void
1526SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1527{
1528 int rc;
1529 struct cifs_ses *ses = sess_data->ses;
1530 struct smb2_sess_setup_req *req;
1531 struct smb2_sess_setup_rsp *rsp = NULL;
1532 unsigned char *ntlmssp_blob = NULL;
1533 bool use_spnego = false; /* else use raw ntlmssp */
1534 u16 blob_length = 0;
1535
1536 rc = SMB2_sess_alloc_buffer(sess_data);
1537 if (rc)
1538 goto out;
1539
1540 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001541 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001542
1543 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1544 sess_data->nls_cp);
1545 if (rc) {
1546 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1547 goto out;
1548 }
1549
1550 if (use_spnego) {
1551 /* BB eventually need to add this */
1552 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1553 rc = -EOPNOTSUPP;
1554 goto out;
1555 }
1556 sess_data->iov[1].iov_base = ntlmssp_blob;
1557 sess_data->iov[1].iov_len = blob_length;
1558
1559 rc = SMB2_sess_sendreceive(sess_data);
1560 if (rc)
1561 goto out;
1562
1563 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1564
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001565 /* keep existing ses id and flags if binding */
1566 if (!ses->binding) {
1567 ses->Suid = rsp->sync_hdr.SessionId;
1568 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1569 }
Sachin Prabhu166cea42016-10-07 19:11:22 +01001570
1571 rc = SMB2_sess_establish_session(sess_data);
Ronnie Sahlbergf560cda2020-04-12 16:09:26 +10001572#ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1573 if (ses->server->dialect < SMB30_PROT_ID) {
1574 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1575 /*
1576 * The session id is opaque in terms of endianness, so we can't
1577 * print it as a long long. we dump it as we got it on the wire
1578 */
1579 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1580 &ses->Suid);
1581 cifs_dbg(VFS, "Session Key %*ph\n",
1582 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1583 cifs_dbg(VFS, "Signing Key %*ph\n",
1584 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1585 }
1586#endif
Sachin Prabhu166cea42016-10-07 19:11:22 +01001587out:
1588 kfree(ntlmssp_blob);
1589 SMB2_sess_free_buffer(sess_data);
1590 kfree(ses->ntlmssp);
1591 ses->ntlmssp = NULL;
1592 sess_data->result = rc;
1593 sess_data->func = NULL;
1594}
1595
1596static int
1597SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1598{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301599 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001600
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001601 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301602 cifs_dbg(FYI, "sess setup type %d\n", type);
1603 if (type == Unspecified) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001604 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301605 return -EINVAL;
1606 }
1607
1608 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001609 case Kerberos:
1610 sess_data->func = SMB2_auth_kerberos;
1611 break;
1612 case RawNTLMSSP:
1613 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1614 break;
1615 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301616 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001617 return -EOPNOTSUPP;
1618 }
1619
1620 return 0;
1621}
1622
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001623int
1624SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1625 const struct nls_table *nls_cp)
1626{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001627 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001628 struct TCP_Server_Info *server = cifs_ses_server(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001629 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001630
Joe Perchesf96637b2013-05-04 22:12:25 -05001631 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001632
Jeff Layton3534b852013-05-24 07:41:01 -04001633 if (!server) {
1634 WARN(1, "%s: server is NULL!\n", __func__);
1635 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001636 }
1637
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001638 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1639 if (!sess_data)
1640 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001641
1642 rc = SMB2_select_sec(ses, sess_data);
1643 if (rc)
1644 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001645 sess_data->xid = xid;
1646 sess_data->ses = ses;
1647 sess_data->buf0_type = CIFS_NO_BUFFER;
1648 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001649 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001650
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001651 /*
1652 * Initialize the session hash with the server one.
1653 */
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001654 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001655 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001656
Sachin Prabhu166cea42016-10-07 19:11:22 +01001657 while (sess_data->func)
1658 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001659
Steve Frenchc721c382017-09-19 18:40:03 -05001660 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001661 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001662 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001663out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001664 kfree(sess_data);
1665 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001666}
1667
1668int
1669SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1670{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001671 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001672 struct smb2_logoff_req *req; /* response is also trivial struct */
1673 int rc = 0;
1674 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001675 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001676 unsigned int total_len;
1677 struct kvec iov[1];
1678 struct kvec rsp_iov;
1679 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001680
Joe Perchesf96637b2013-05-04 22:12:25 -05001681 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001682
1683 if (ses && (ses->server))
1684 server = ses->server;
1685 else
1686 return -EIO;
1687
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001688 /* no need to send SMB logoff if uid already closed due to reconnect */
1689 if (ses->need_reconnect)
1690 goto smb2_session_already_dead;
1691
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001692 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001693 if (rc)
1694 return rc;
1695
1696 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001697 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001698
1699 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1700 flags |= CIFS_TRANSFORM_REQ;
1701 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001702 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001703
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001704 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001705
1706 iov[0].iov_base = (char *)req;
1707 iov[0].iov_len = total_len;
1708
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001709 memset(&rqst, 0, sizeof(struct smb_rqst));
1710 rqst.rq_iov = iov;
1711 rqst.rq_nvec = 1;
1712
1713 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001714 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001715 /*
1716 * No tcon so can't do
1717 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1718 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001719
1720smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001721 return rc;
1722}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001723
1724static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1725{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001726 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001727}
1728
1729#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1730
Steve Frenchde9f68df2013-11-15 11:26:24 -06001731/* These are similar values to what Windows uses */
1732static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1733{
1734 tcon->max_chunks = 256;
1735 tcon->max_bytes_chunk = 1048576;
1736 tcon->max_bytes_copy = 16777216;
1737}
1738
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001739int
1740SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1741 struct cifs_tcon *tcon, const struct nls_table *cp)
1742{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001743 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001744 struct smb2_tree_connect_req *req;
1745 struct smb2_tree_connect_rsp *rsp = NULL;
1746 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001747 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001748 int rc = 0;
1749 int resp_buftype;
1750 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001751 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001752 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001753 unsigned int total_len;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001754 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001755
Joe Perchesf96637b2013-05-04 22:12:25 -05001756 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001757
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001758 if (!server || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001759 return -EIO;
1760
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001761 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1762 if (unc_path == NULL)
1763 return -ENOMEM;
1764
1765 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1766 unc_path_len *= 2;
1767 if (unc_path_len < 2) {
1768 kfree(unc_path);
1769 return -EINVAL;
1770 }
1771
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001772 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001773 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001774 atomic_set(&tcon->num_remote_opens, 0);
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001775 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1776 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001777 if (rc) {
1778 kfree(unc_path);
1779 return rc;
1780 }
1781
Steve French5a77e752018-05-09 17:43:08 -05001782 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001783 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001784
1785 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001786 /* 1 for pad */
1787 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001788
1789 /* Testing shows that buffer offset must be at location of Buffer[0] */
1790 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001791 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001792 req->PathLength = cpu_to_le16(unc_path_len - 2);
1793 iov[1].iov_base = unc_path;
1794 iov[1].iov_len = unc_path_len;
1795
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001796 /*
1797 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1798 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
Steve French8c11a602019-03-22 22:31:17 -05001799 * (Samba servers don't always set the flag so also check if null user)
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001800 */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001801 if ((server->dialect == SMB311_PROT_ID) &&
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001802 !smb3_encryption_required(tcon) &&
Steve French8c11a602019-03-22 22:31:17 -05001803 !(ses->session_flags &
1804 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1805 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
Steve French6188f282018-03-13 02:29:36 -05001806 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1807
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001808 memset(&rqst, 0, sizeof(struct smb_rqst));
1809 rqst.rq_iov = iov;
1810 rqst.rq_nvec = 2;
1811
Steve French4fe75c42019-02-14 01:19:02 -06001812 /* Need 64 for max size write so ask for more in case not there yet */
1813 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1814
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001815 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001816 cifs_small_buf_release(req);
1817 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001818 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001819 if (rc != 0) {
1820 if (tcon) {
1821 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1822 tcon->need_reconnect = true;
1823 }
1824 goto tcon_error_exit;
1825 }
1826
Christophe JAILLETcd123002017-05-12 17:59:32 +02001827 switch (rsp->ShareType) {
1828 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001829 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001830 break;
1831 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001832 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001833 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001834 break;
1835 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001836 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001837 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001838 break;
1839 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001840 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001841 rc = -EOPNOTSUPP;
1842 goto tcon_error_exit;
1843 }
1844
1845 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001846 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001847 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1848 tcon->tidStatus = CifsGood;
1849 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001850 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001851 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001852
1853 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1854 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001855 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001856
1857 if (tcon->seal &&
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001858 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001859 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001860
Steve Frenchde9f68df2013-11-15 11:26:24 -06001861 init_copy_chunk_defaults(tcon);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001862 if (server->ops->validate_negotiate)
1863 rc = server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001864tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001865
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001866 free_rsp_buf(resp_buftype, rsp);
1867 kfree(unc_path);
1868 return rc;
1869
1870tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001871 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001872 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001873 }
1874 goto tcon_exit;
1875}
1876
1877int
1878SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1879{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001880 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001881 struct smb2_tree_disconnect_req *req; /* response is trivial */
1882 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001883 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001884 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001885 unsigned int total_len;
1886 struct kvec iov[1];
1887 struct kvec rsp_iov;
1888 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001889
Joe Perchesf96637b2013-05-04 22:12:25 -05001890 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001891
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001892 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001893 return -EIO;
1894
1895 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1896 return 0;
1897
Pavel Shilovskyd9191312019-12-10 11:44:52 -08001898 close_shroot_lease(&tcon->crfid);
Ronnie Sahlberg72e73c72019-11-07 17:00:38 +10001899
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001900 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1901 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001902 if (rc)
1903 return rc;
1904
Steve French5a77e752018-05-09 17:43:08 -05001905 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001906 flags |= CIFS_TRANSFORM_REQ;
1907
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001908 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001909
1910 iov[0].iov_base = (char *)req;
1911 iov[0].iov_len = total_len;
1912
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001913 memset(&rqst, 0, sizeof(struct smb_rqst));
1914 rqst.rq_iov = iov;
1915 rqst.rq_nvec = 1;
1916
1917 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001918 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001919 if (rc)
1920 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1921
1922 return rc;
1923}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001924
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001925
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001926static struct create_durable *
1927create_durable_buf(void)
1928{
1929 struct create_durable *buf;
1930
1931 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1932 if (!buf)
1933 return NULL;
1934
1935 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001936 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001937 buf->ccontext.DataLength = cpu_to_le32(16);
1938 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1939 (struct create_durable, Name));
1940 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001941 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001942 buf->Name[0] = 'D';
1943 buf->Name[1] = 'H';
1944 buf->Name[2] = 'n';
1945 buf->Name[3] = 'Q';
1946 return buf;
1947}
1948
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001949static struct create_durable *
1950create_reconnect_durable_buf(struct cifs_fid *fid)
1951{
1952 struct create_durable *buf;
1953
1954 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1955 if (!buf)
1956 return NULL;
1957
1958 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1959 (struct create_durable, Data));
1960 buf->ccontext.DataLength = cpu_to_le32(16);
1961 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1962 (struct create_durable, Name));
1963 buf->ccontext.NameLength = cpu_to_le16(4);
1964 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1965 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001966 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001967 buf->Name[0] = 'D';
1968 buf->Name[1] = 'H';
1969 buf->Name[2] = 'n';
1970 buf->Name[3] = 'C';
1971 return buf;
1972}
1973
Steve French89a5bfa2019-07-18 17:22:18 -05001974static void
1975parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1976{
1977 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1978
1979 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1980 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1981 buf->IndexNumber = pdisk_id->DiskFileId;
1982}
1983
Steve Frenchab3459d2020-02-06 17:31:56 -06001984static void
Aurelien Aptel69dda302020-03-02 17:53:22 +01001985parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
1986 struct create_posix_rsp *posix)
Steve Frenchab3459d2020-02-06 17:31:56 -06001987{
Aurelien Aptel69dda302020-03-02 17:53:22 +01001988 int sid_len;
1989 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
1990 u8 *end = beg + le32_to_cpu(cc->DataLength);
1991 u8 *sid;
Steve Frenchab3459d2020-02-06 17:31:56 -06001992
Aurelien Aptel69dda302020-03-02 17:53:22 +01001993 memset(posix, 0, sizeof(*posix));
Aurelien Aptel2e8af972020-02-08 15:50:56 +01001994
Aurelien Aptel69dda302020-03-02 17:53:22 +01001995 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
1996 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
1997 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
1998
1999 sid = beg + 12;
2000 sid_len = posix_info_sid_size(sid, end);
2001 if (sid_len < 0) {
2002 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2003 return;
2004 }
2005 memcpy(&posix->owner, sid, sid_len);
2006
2007 sid = sid + sid_len;
2008 sid_len = posix_info_sid_size(sid, end);
2009 if (sid_len < 0) {
2010 cifs_dbg(VFS, "bad group sid in posix create response\n");
2011 return;
2012 }
2013 memcpy(&posix->group, sid, sid_len);
2014
2015 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2016 posix->nlink, posix->mode, posix->reparse_tag);
Steve Frenchab3459d2020-02-06 17:31:56 -06002017}
2018
Steve French89a5bfa2019-07-18 17:22:18 -05002019void
2020smb2_parse_contexts(struct TCP_Server_Info *server,
Aurelien Aptel69dda302020-03-02 17:53:22 +01002021 struct smb2_create_rsp *rsp,
2022 unsigned int *epoch, char *lease_key, __u8 *oplock,
2023 struct smb2_file_all_info *buf,
2024 struct create_posix_rsp *posix)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002025{
2026 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002027 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08002028 unsigned int next;
2029 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04002030 char *name;
Steve Frenchab3459d2020-02-06 17:31:56 -06002031 const char smb3_create_tag_posix[] = {0x93, 0xAD, 0x25, 0x50, 0x9C,
2032 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2033 0xDE, 0x96, 0x8B, 0xCD, 0x7C};
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002034
Steve French89a5bfa2019-07-18 17:22:18 -05002035 *oplock = 0;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002036 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08002037 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002038 cc = (struct create_context *)data_offset;
Steve French89a5bfa2019-07-18 17:22:18 -05002039
2040 /* Initialize inode number to 0 in case no valid data in qfid context */
2041 if (buf)
2042 buf->IndexNumber = 0;
2043
Justin Maggarddeb7def2016-02-09 15:52:08 -08002044 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002045 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08002046 if (le16_to_cpu(cc->NameLength) == 4 &&
Steve French89a5bfa2019-07-18 17:22:18 -05002047 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2048 *oplock = server->ops->parse_lease_buf(cc, epoch,
2049 lease_key);
2050 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2051 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2052 parse_query_id_ctxt(cc, buf);
Steve Frenchab3459d2020-02-06 17:31:56 -06002053 else if ((le16_to_cpu(cc->NameLength) == 16)) {
Aurelien Aptel69dda302020-03-02 17:53:22 +01002054 if (posix &&
2055 memcmp(name, smb3_create_tag_posix, 16) == 0)
2056 parse_posix_ctxt(cc, buf, posix);
Steve Frenchab3459d2020-02-06 17:31:56 -06002057 }
2058 /* else {
2059 cifs_dbg(FYI, "Context not matched with len %d\n",
2060 le16_to_cpu(cc->NameLength));
2061 cifs_dump_mem("Cctxt name: ", name, 4);
2062 } */
Justin Maggarddeb7def2016-02-09 15:52:08 -08002063
2064 next = le32_to_cpu(cc->Next);
2065 if (!next)
2066 break;
2067 remaining -= next;
2068 cc = (struct create_context *)((char *)cc + next);
2069 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002070
Steve French89a5bfa2019-07-18 17:22:18 -05002071 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2072 *oplock = rsp->OplockLevel;
2073
2074 return;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002075}
2076
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002077static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002078add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02002079 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002080{
2081 struct smb2_create_req *req = iov[0].iov_base;
2082 unsigned int num = *num_iovec;
2083
Stefano Brivio729c0c92018-07-05 15:10:02 +02002084 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002085 if (iov[num].iov_base == NULL)
2086 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002087 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002088 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2089 if (!req->CreateContextsOffset)
2090 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002091 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002092 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002093 le32_add_cpu(&req->CreateContextsLength,
2094 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002095 *num_iovec = num + 1;
2096 return 0;
2097}
2098
Steve Frenchb56eae42015-11-03 09:26:27 -06002099static struct create_durable_v2 *
Steve Frenchca567eb2019-03-29 16:31:07 -05002100create_durable_v2_buf(struct cifs_open_parms *oparms)
Steve Frenchb56eae42015-11-03 09:26:27 -06002101{
Steve Frenchca567eb2019-03-29 16:31:07 -05002102 struct cifs_fid *pfid = oparms->fid;
Steve Frenchb56eae42015-11-03 09:26:27 -06002103 struct create_durable_v2 *buf;
2104
2105 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2106 if (!buf)
2107 return NULL;
2108
2109 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2110 (struct create_durable_v2, dcontext));
2111 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2112 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2113 (struct create_durable_v2, Name));
2114 buf->ccontext.NameLength = cpu_to_le16(4);
2115
Steve Frenchca567eb2019-03-29 16:31:07 -05002116 /*
2117 * NB: Handle timeout defaults to 0, which allows server to choose
2118 * (most servers default to 120 seconds) and most clients default to 0.
2119 * This can be overridden at mount ("handletimeout=") if the user wants
2120 * a different persistent (or resilient) handle timeout for all opens
2121 * opens on a particular SMB3 mount.
2122 */
2123 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
Steve Frenchb56eae42015-11-03 09:26:27 -06002124 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05002125 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06002126 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2127
2128 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2129 buf->Name[0] = 'D';
2130 buf->Name[1] = 'H';
2131 buf->Name[2] = '2';
2132 buf->Name[3] = 'Q';
2133 return buf;
2134}
2135
2136static struct create_durable_handle_reconnect_v2 *
2137create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2138{
2139 struct create_durable_handle_reconnect_v2 *buf;
2140
2141 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2142 GFP_KERNEL);
2143 if (!buf)
2144 return NULL;
2145
2146 buf->ccontext.DataOffset =
2147 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2148 dcontext));
2149 buf->ccontext.DataLength =
2150 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2151 buf->ccontext.NameOffset =
2152 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2153 Name));
2154 buf->ccontext.NameLength = cpu_to_le16(4);
2155
2156 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2157 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2158 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2159 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2160
2161 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2162 buf->Name[0] = 'D';
2163 buf->Name[1] = 'H';
2164 buf->Name[2] = '2';
2165 buf->Name[3] = 'C';
2166 return buf;
2167}
2168
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002169static int
Steve Frenchb56eae42015-11-03 09:26:27 -06002170add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002171 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002172{
2173 struct smb2_create_req *req = iov[0].iov_base;
2174 unsigned int num = *num_iovec;
2175
Steve Frenchca567eb2019-03-29 16:31:07 -05002176 iov[num].iov_base = create_durable_v2_buf(oparms);
Steve Frenchb56eae42015-11-03 09:26:27 -06002177 if (iov[num].iov_base == NULL)
2178 return -ENOMEM;
2179 iov[num].iov_len = sizeof(struct create_durable_v2);
2180 if (!req->CreateContextsOffset)
2181 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002182 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002183 iov[1].iov_len);
2184 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002185 *num_iovec = num + 1;
2186 return 0;
2187}
2188
2189static int
2190add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2191 struct cifs_open_parms *oparms)
2192{
2193 struct smb2_create_req *req = iov[0].iov_base;
2194 unsigned int num = *num_iovec;
2195
2196 /* indicate that we don't need to relock the file */
2197 oparms->reconnect = false;
2198
2199 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2200 if (iov[num].iov_base == NULL)
2201 return -ENOMEM;
2202 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2203 if (!req->CreateContextsOffset)
2204 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002205 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002206 iov[1].iov_len);
2207 le32_add_cpu(&req->CreateContextsLength,
2208 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002209 *num_iovec = num + 1;
2210 return 0;
2211}
2212
2213static int
2214add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2215 struct cifs_open_parms *oparms, bool use_persistent)
2216{
2217 struct smb2_create_req *req = iov[0].iov_base;
2218 unsigned int num = *num_iovec;
2219
2220 if (use_persistent) {
2221 if (oparms->reconnect)
2222 return add_durable_reconnect_v2_context(iov, num_iovec,
2223 oparms);
2224 else
2225 return add_durable_v2_context(iov, num_iovec, oparms);
2226 }
2227
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002228 if (oparms->reconnect) {
2229 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2230 /* indicate that we don't need to relock the file */
2231 oparms->reconnect = false;
2232 } else
2233 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002234 if (iov[num].iov_base == NULL)
2235 return -ENOMEM;
2236 iov[num].iov_len = sizeof(struct create_durable);
2237 if (!req->CreateContextsOffset)
2238 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002239 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002240 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08002241 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002242 *num_iovec = num + 1;
2243 return 0;
2244}
2245
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002246/* See MS-SMB2 2.2.13.2.7 */
2247static struct crt_twarp_ctxt *
2248create_twarp_buf(__u64 timewarp)
2249{
2250 struct crt_twarp_ctxt *buf;
2251
2252 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2253 if (!buf)
2254 return NULL;
2255
2256 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2257 (struct crt_twarp_ctxt, Timestamp));
2258 buf->ccontext.DataLength = cpu_to_le32(8);
2259 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2260 (struct crt_twarp_ctxt, Name));
2261 buf->ccontext.NameLength = cpu_to_le16(4);
2262 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2263 buf->Name[0] = 'T';
2264 buf->Name[1] = 'W';
2265 buf->Name[2] = 'r';
2266 buf->Name[3] = 'p';
2267 buf->Timestamp = cpu_to_le64(timewarp);
2268 return buf;
2269}
2270
2271/* See MS-SMB2 2.2.13.2.7 */
2272static int
2273add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2274{
2275 struct smb2_create_req *req = iov[0].iov_base;
2276 unsigned int num = *num_iovec;
2277
2278 iov[num].iov_base = create_twarp_buf(timewarp);
2279 if (iov[num].iov_base == NULL)
2280 return -ENOMEM;
2281 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2282 if (!req->CreateContextsOffset)
2283 req->CreateContextsOffset = cpu_to_le32(
2284 sizeof(struct smb2_create_req) +
2285 iov[num - 1].iov_len);
2286 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2287 *num_iovec = num + 1;
2288 return 0;
2289}
2290
Steve Frenchfdef6652019-12-06 02:02:38 -06002291/* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2292static struct crt_sd_ctxt *
2293create_sd_buf(umode_t mode, unsigned int *len)
2294{
2295 struct crt_sd_ctxt *buf;
2296 struct cifs_ace *pace;
2297 unsigned int sdlen, acelen;
2298
Steve French643fbce2020-01-16 19:55:33 -06002299 *len = roundup(sizeof(struct crt_sd_ctxt) + sizeof(struct cifs_ace) * 2,
2300 8);
Steve Frenchfdef6652019-12-06 02:02:38 -06002301 buf = kzalloc(*len, GFP_KERNEL);
2302 if (buf == NULL)
2303 return buf;
2304
2305 sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) +
Steve French643fbce2020-01-16 19:55:33 -06002306 2 * sizeof(struct cifs_ace);
Steve Frenchfdef6652019-12-06 02:02:38 -06002307
2308 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2309 (struct crt_sd_ctxt, sd));
2310 buf->ccontext.DataLength = cpu_to_le32(sdlen);
2311 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2312 (struct crt_sd_ctxt, Name));
2313 buf->ccontext.NameLength = cpu_to_le16(4);
2314 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2315 buf->Name[0] = 'S';
2316 buf->Name[1] = 'e';
2317 buf->Name[2] = 'c';
2318 buf->Name[3] = 'D';
2319 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2320 /*
2321 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2322 * and "DP" ie the DACL is present
2323 */
2324 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2325
2326 /* offset owner, group and Sbz1 and SACL are all zero */
2327 buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd));
2328 buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2329
2330 /* create one ACE to hold the mode embedded in reserved special SID */
2331 pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf);
2332 acelen = setup_special_mode_ACE(pace, (__u64)mode);
Steve French643fbce2020-01-16 19:55:33 -06002333 /* and one more ACE to allow access for authenticated users */
2334 pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) +
2335 (char *)buf));
2336 acelen += setup_authusers_ACE(pace);
Steve Frenchfdef6652019-12-06 02:02:38 -06002337 buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen);
Steve French643fbce2020-01-16 19:55:33 -06002338 buf->acl.AceCount = cpu_to_le16(2);
Steve Frenchfdef6652019-12-06 02:02:38 -06002339 return buf;
2340}
2341
2342static int
2343add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
2344{
2345 struct smb2_create_req *req = iov[0].iov_base;
2346 unsigned int num = *num_iovec;
2347 unsigned int len = 0;
2348
2349 iov[num].iov_base = create_sd_buf(mode, &len);
2350 if (iov[num].iov_base == NULL)
2351 return -ENOMEM;
2352 iov[num].iov_len = len;
2353 if (!req->CreateContextsOffset)
2354 req->CreateContextsOffset = cpu_to_le32(
2355 sizeof(struct smb2_create_req) +
2356 iov[num - 1].iov_len);
2357 le32_add_cpu(&req->CreateContextsLength, len);
2358 *num_iovec = num + 1;
2359 return 0;
2360}
2361
Steve Frenchff2a09e2019-07-06 14:41:38 -05002362static struct crt_query_id_ctxt *
2363create_query_id_buf(void)
2364{
2365 struct crt_query_id_ctxt *buf;
2366
2367 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2368 if (!buf)
2369 return NULL;
2370
2371 buf->ccontext.DataOffset = cpu_to_le16(0);
2372 buf->ccontext.DataLength = cpu_to_le32(0);
2373 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2374 (struct crt_query_id_ctxt, Name));
2375 buf->ccontext.NameLength = cpu_to_le16(4);
2376 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2377 buf->Name[0] = 'Q';
2378 buf->Name[1] = 'F';
2379 buf->Name[2] = 'i';
2380 buf->Name[3] = 'd';
2381 return buf;
2382}
2383
2384/* See MS-SMB2 2.2.13.2.9 */
2385static int
2386add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2387{
2388 struct smb2_create_req *req = iov[0].iov_base;
2389 unsigned int num = *num_iovec;
2390
2391 iov[num].iov_base = create_query_id_buf();
2392 if (iov[num].iov_base == NULL)
2393 return -ENOMEM;
2394 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2395 if (!req->CreateContextsOffset)
2396 req->CreateContextsOffset = cpu_to_le32(
2397 sizeof(struct smb2_create_req) +
2398 iov[num - 1].iov_len);
2399 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2400 *num_iovec = num + 1;
2401 return 0;
2402}
2403
Aurelien Aptelf0712922017-02-22 14:47:17 +01002404static int
2405alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2406 const char *treename, const __le16 *path)
2407{
2408 int treename_len, path_len;
2409 struct nls_table *cp;
2410 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2411
2412 /*
2413 * skip leading "\\"
2414 */
2415 treename_len = strlen(treename);
2416 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2417 return -EINVAL;
2418
2419 treename += 2;
2420 treename_len -= 2;
2421
2422 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2423
2424 /*
2425 * make room for one path separator between the treename and
2426 * path
2427 */
2428 *out_len = treename_len + 1 + path_len;
2429
2430 /*
2431 * final path needs to be null-terminated UTF16 with a
2432 * size aligned to 8
2433 */
2434
2435 *out_size = roundup((*out_len+1)*2, 8);
2436 *out_path = kzalloc(*out_size, GFP_KERNEL);
2437 if (!*out_path)
2438 return -ENOMEM;
2439
2440 cp = load_nls_default();
2441 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2442 UniStrcat(*out_path, sep);
2443 UniStrcat(*out_path, path);
2444 unload_nls(cp);
2445
2446 return 0;
2447}
2448
Steve Frenchbea851b2018-06-14 21:56:32 -05002449int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2450 umode_t mode, struct cifs_tcon *tcon,
2451 const char *full_path,
2452 struct cifs_sb_info *cifs_sb)
2453{
2454 struct smb_rqst rqst;
2455 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002456 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002457 struct cifs_ses *ses = tcon->ses;
2458 struct kvec iov[3]; /* make sure at least one for each open context */
2459 struct kvec rsp_iov = {NULL, 0};
2460 int resp_buftype;
2461 int uni_path_len;
2462 __le16 *copy_path = NULL;
2463 int copy_size;
2464 int rc = 0;
2465 unsigned int n_iov = 2;
2466 __u32 file_attributes = 0;
2467 char *pc_buf = NULL;
2468 int flags = 0;
2469 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002470 __le16 *utf16_path = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002471
2472 cifs_dbg(FYI, "mkdir\n");
2473
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002474 /* resource #1: path allocation */
2475 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2476 if (!utf16_path)
2477 return -ENOMEM;
2478
YueHaibing29cbfa12018-12-18 02:51:51 +00002479 if (!ses || !(ses->server)) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002480 rc = -EIO;
2481 goto err_free_path;
2482 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002483
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002484 /* resource #2: request */
Steve Frenchbea851b2018-06-14 21:56:32 -05002485 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002486 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002487 goto err_free_path;
2488
Steve Frenchbea851b2018-06-14 21:56:32 -05002489
2490 if (smb3_encryption_required(tcon))
2491 flags |= CIFS_TRANSFORM_REQ;
2492
Steve Frenchbea851b2018-06-14 21:56:32 -05002493 req->ImpersonationLevel = IL_IMPERSONATION;
2494 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2495 /* File attributes ignored on open (used in create though) */
2496 req->FileAttributes = cpu_to_le32(file_attributes);
2497 req->ShareAccess = FILE_SHARE_ALL_LE;
2498 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2499 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2500
2501 iov[0].iov_base = (char *)req;
2502 /* -1 since last byte is buf[0] which is sent below (path) */
2503 iov[0].iov_len = total_len - 1;
2504
2505 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2506
2507 /* [MS-SMB2] 2.2.13 NameOffset:
2508 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2509 * the SMB2 header, the file name includes a prefix that will
2510 * be processed during DFS name normalization as specified in
2511 * section 3.3.5.9. Otherwise, the file name is relative to
2512 * the share that is identified by the TreeId in the SMB2
2513 * header.
2514 */
2515 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2516 int name_len;
2517
2518 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2519 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2520 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002521 tcon->treeName, utf16_path);
2522 if (rc)
2523 goto err_free_req;
2524
Steve Frenchbea851b2018-06-14 21:56:32 -05002525 req->NameLength = cpu_to_le16(name_len * 2);
2526 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002527 /* free before overwriting resource */
2528 kfree(utf16_path);
2529 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002530 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002531 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002532 /* MUST set path len (NameLength) to 0 opening root of share */
2533 req->NameLength = cpu_to_le16(uni_path_len - 2);
2534 if (uni_path_len % 8 != 0) {
2535 copy_size = roundup(uni_path_len, 8);
2536 copy_path = kzalloc(copy_size, GFP_KERNEL);
2537 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002538 rc = -ENOMEM;
2539 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002540 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002541 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002542 uni_path_len);
2543 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002544 /* free before overwriting resource */
2545 kfree(utf16_path);
2546 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002547 }
2548 }
2549
2550 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002551 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002552 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2553
2554 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002555 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002556 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002557 if (rc)
2558 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002559 pc_buf = iov[n_iov-1].iov_base;
2560 }
2561
2562
2563 memset(&rqst, 0, sizeof(struct smb_rqst));
2564 rqst.rq_iov = iov;
2565 rqst.rq_nvec = n_iov;
2566
Steve Frenchd2f15422019-09-22 00:55:46 -05002567 /* no need to inc num_remote_opens because we close it just below */
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002568 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2569 FILE_WRITE_ATTRIBUTES);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002570 /* resource #4: response buffer */
2571 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2572 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002573 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2574 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002575 CREATE_NOT_FILE,
2576 FILE_WRITE_ATTRIBUTES, rc);
2577 goto err_free_rsp_buf;
2578 }
2579
2580 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2581 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2582 ses->Suid, CREATE_NOT_FILE,
2583 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002584
2585 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2586
2587 /* Eventually save off posix specific response info and timestaps */
2588
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002589err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002590 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002591 kfree(pc_buf);
2592err_free_req:
2593 cifs_small_buf_release(req);
2594err_free_path:
2595 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002596 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002597}
Steve Frenchbea851b2018-06-14 21:56:32 -05002598
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002599int
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002600SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2601 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002602{
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002603 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002604 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002605 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002606 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002607 int copy_size;
2608 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002609 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002610 struct kvec *iov = rqst->rq_iov;
2611 __le16 *copy_path;
2612 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002613
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002614 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002615 if (rc)
2616 return rc;
2617
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002618 iov[0].iov_base = (char *)req;
2619 /* -1 since last byte is buf[0] which is sent below (path) */
2620 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002621
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002622 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002623 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002624 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2625 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002626
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002627 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002628 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002629 /* File attributes ignored on open (used in create though) */
2630 req->FileAttributes = cpu_to_le32(file_attributes);
2631 req->ShareAccess = FILE_SHARE_ALL_LE;
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002632
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002633 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2634 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002635 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002636
2637 /* [MS-SMB2] 2.2.13 NameOffset:
2638 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2639 * the SMB2 header, the file name includes a prefix that will
2640 * be processed during DFS name normalization as specified in
2641 * section 3.3.5.9. Otherwise, the file name is relative to
2642 * the share that is identified by the TreeId in the SMB2
2643 * header.
2644 */
2645 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2646 int name_len;
2647
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002648 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002649 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2650 &name_len,
2651 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002652 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002653 return rc;
2654 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002655 uni_path_len = copy_size;
2656 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002657 } else {
2658 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2659 /* MUST set path len (NameLength) to 0 opening root of share */
2660 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002661 copy_size = uni_path_len;
2662 if (copy_size % 8 != 0)
2663 copy_size = roundup(copy_size, 8);
2664 copy_path = kzalloc(copy_size, GFP_KERNEL);
2665 if (!copy_path)
2666 return -ENOMEM;
2667 memcpy((char *)copy_path, (const char *)path,
2668 uni_path_len);
2669 uni_path_len = copy_size;
2670 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002671 }
2672
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002673 iov[1].iov_len = uni_path_len;
2674 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002675
Steve French3e7a02d2019-09-11 21:46:20 -05002676 if ((!server->oplocks) || (tcon->no_lease))
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002677 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2678
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002679 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002680 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2681 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002682 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2683 (oparms->create_options & CREATE_NOT_FILE))
2684 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002685 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002686 rc = add_lease_context(server, iov, &n_iov,
2687 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002688 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002689 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002690 }
2691
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002692 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2693 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002694 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002695 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002696 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002697 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002698 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002699 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002700
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002701 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002702 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002703 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002704 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002705 }
2706
Steve Frenchce558b02018-05-31 19:16:54 -05002707 if (tcon->posix_extensions) {
2708 if (n_iov > 2) {
2709 struct create_context *ccontext =
2710 (struct create_context *)iov[n_iov-1].iov_base;
2711 ccontext->Next =
2712 cpu_to_le32(iov[n_iov-1].iov_len);
2713 }
2714
2715 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002716 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002717 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002718 }
Steve Frenchce558b02018-05-31 19:16:54 -05002719
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002720 if (tcon->snapshot_time) {
2721 cifs_dbg(FYI, "adding snapshot context\n");
2722 if (n_iov > 2) {
2723 struct create_context *ccontext =
2724 (struct create_context *)iov[n_iov-1].iov_base;
2725 ccontext->Next =
2726 cpu_to_le32(iov[n_iov-1].iov_len);
2727 }
2728
2729 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2730 if (rc)
2731 return rc;
2732 }
2733
Steve Frenchfdef6652019-12-06 02:02:38 -06002734 if ((oparms->disposition != FILE_OPEN) &&
Steve French231e2a02019-12-07 17:38:22 -06002735 (oparms->cifs_sb) &&
2736 (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
Steve Frenchd0959b02019-10-05 10:53:58 -05002737 (oparms->mode != ACL_NO_MODE)) {
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002738 if (n_iov > 2) {
2739 struct create_context *ccontext =
2740 (struct create_context *)iov[n_iov-1].iov_base;
2741 ccontext->Next =
2742 cpu_to_le32(iov[n_iov-1].iov_len);
2743 }
2744
Steve Frenchfdef6652019-12-06 02:02:38 -06002745 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2746 rc = add_sd_context(iov, &n_iov, oparms->mode);
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002747 if (rc)
2748 return rc;
2749 }
2750
Steve Frenchff2a09e2019-07-06 14:41:38 -05002751 if (n_iov > 2) {
2752 struct create_context *ccontext =
2753 (struct create_context *)iov[n_iov-1].iov_base;
2754 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2755 }
2756 add_query_id_context(iov, &n_iov);
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002757
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002758 rqst->rq_nvec = n_iov;
2759 return 0;
2760}
2761
2762/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2763 * All other vectors are freed by kfree().
2764 */
2765void
2766SMB2_open_free(struct smb_rqst *rqst)
2767{
2768 int i;
2769
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002770 if (rqst && rqst->rq_iov) {
2771 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2772 for (i = 1; i < rqst->rq_nvec; i++)
2773 if (rqst->rq_iov[i].iov_base != smb2_padding)
2774 kfree(rqst->rq_iov[i].iov_base);
2775 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002776}
2777
2778int
2779SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2780 __u8 *oplock, struct smb2_file_all_info *buf,
Aurelien Aptel69dda302020-03-02 17:53:22 +01002781 struct create_posix_rsp *posix,
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002782 struct kvec *err_iov, int *buftype)
2783{
2784 struct smb_rqst rqst;
2785 struct smb2_create_rsp *rsp = NULL;
2786 struct TCP_Server_Info *server;
2787 struct cifs_tcon *tcon = oparms->tcon;
2788 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002789 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002790 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002791 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002792 int rc = 0;
2793 int flags = 0;
2794
2795 cifs_dbg(FYI, "create/open\n");
2796 if (ses && (ses->server))
2797 server = ses->server;
2798 else
2799 return -EIO;
2800
2801 if (smb3_encryption_required(tcon))
2802 flags |= CIFS_TRANSFORM_REQ;
2803
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002804 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002805 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002806 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002807 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002808
2809 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2810 if (rc)
2811 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002812
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002813 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2814 oparms->create_options, oparms->desired_access);
2815
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002816 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002817 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002818 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002819
2820 if (rc != 0) {
2821 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002822 if (err_iov && rsp) {
2823 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002824 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002825 resp_buftype = CIFS_NO_BUFFER;
2826 rsp = NULL;
2827 }
Steve French28d59362018-05-30 21:42:34 -05002828 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2829 oparms->create_options, oparms->desired_access, rc);
Steve French7dcc82c2019-09-11 00:07:36 -05002830 if (rc == -EREMCHG) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002831 pr_warn_once("server share %s deleted\n",
2832 tcon->treeName);
Steve French7dcc82c2019-09-11 00:07:36 -05002833 tcon->need_reconnect = true;
2834 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002835 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002836 } else
2837 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2838 ses->Suid, oparms->create_options,
2839 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002840
Steve Frenchfae80442018-10-19 17:14:32 -05002841 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002842 oparms->fid->persistent_fid = rsp->PersistentFileId;
2843 oparms->fid->volatile_fid = rsp->VolatileFileId;
Aurelien Aptel86f740f2020-02-21 11:19:06 +01002844 oparms->fid->access = oparms->desired_access;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002845#ifdef CONFIG_CIFS_DEBUG2
2846 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2847#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002848
2849 if (buf) {
2850 memcpy(buf, &rsp->CreationTime, 32);
2851 buf->AllocationSize = rsp->AllocationSize;
2852 buf->EndOfFile = rsp->EndofFile;
2853 buf->Attributes = rsp->FileAttributes;
2854 buf->NumberOfLinks = cpu_to_le32(1);
2855 buf->DeletePending = 0;
2856 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002857
Steve French89a5bfa2019-07-18 17:22:18 -05002858
2859 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
Aurelien Aptel69dda302020-03-02 17:53:22 +01002860 oparms->fid->lease_key, oplock, buf, posix);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002861creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002862 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002863 free_rsp_buf(resp_buftype, rsp);
2864 return rc;
2865}
2866
Steve French4a72daf2013-06-25 00:20:49 -05002867int
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002868SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2869 u64 persistent_fid, u64 volatile_fid, u32 opcode,
Steve French153322f2019-03-28 22:32:49 -05002870 bool is_fsctl, char *in_data, u32 indatalen,
2871 __u32 max_response_size)
Steve French4a72daf2013-06-25 00:20:49 -05002872{
2873 struct smb2_ioctl_req *req;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002874 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002875 unsigned int total_len;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002876 int rc;
Long Li2c87d6a2019-05-15 14:09:05 -07002877 char *in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002878
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -03002879 rc = smb2_ioctl_req_init(opcode, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002880 if (rc)
2881 return rc;
2882
Long Li2c87d6a2019-05-15 14:09:05 -07002883 if (indatalen) {
2884 /*
2885 * indatalen is usually small at a couple of bytes max, so
2886 * just allocate through generic pool
2887 */
YueHaibingd81f0972019-06-01 03:31:10 +00002888 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
Long Li2c87d6a2019-05-15 14:09:05 -07002889 if (!in_data_buf) {
2890 cifs_small_buf_release(req);
2891 return -ENOMEM;
2892 }
Long Li2c87d6a2019-05-15 14:09:05 -07002893 }
2894
Steve French4a72daf2013-06-25 00:20:49 -05002895 req->CtlCode = cpu_to_le32(opcode);
2896 req->PersistentFileId = persistent_fid;
2897 req->VolatileFileId = volatile_fid;
2898
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002899 iov[0].iov_base = (char *)req;
2900 /*
2901 * If no input data, the size of ioctl struct in
2902 * protocol spec still includes a 1 byte data buffer,
2903 * but if input data passed to ioctl, we do not
2904 * want to double count this, so we do not send
2905 * the dummy one byte of data in iovec[0] if sending
2906 * input data (in iovec[1]).
2907 */
Steve French4a72daf2013-06-25 00:20:49 -05002908 if (indatalen) {
2909 req->InputCount = cpu_to_le32(indatalen);
2910 /* do not set InputOffset if no input data */
2911 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002912 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002913 rqst->rq_nvec = 2;
2914 iov[0].iov_len = total_len - 1;
Long Li2c87d6a2019-05-15 14:09:05 -07002915 iov[1].iov_base = in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002916 iov[1].iov_len = indatalen;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002917 } else {
2918 rqst->rq_nvec = 1;
2919 iov[0].iov_len = total_len;
2920 }
Steve French4a72daf2013-06-25 00:20:49 -05002921
2922 req->OutputOffset = 0;
2923 req->OutputCount = 0; /* MBZ */
2924
2925 /*
Steve French153322f2019-03-28 22:32:49 -05002926 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2927 * We Could increase default MaxOutputResponse, but that could require
2928 * more credits. Windows typically sets this smaller, but for some
Steve French4a72daf2013-06-25 00:20:49 -05002929 * ioctls it may be useful to allow server to send more. No point
2930 * limiting what the server can send as long as fits in one credit
Steve French153322f2019-03-28 22:32:49 -05002931 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2932 * to increase this limit up in the future.
2933 * Note that for snapshot queries that servers like Azure expect that
2934 * the first query be minimal size (and just used to get the number/size
2935 * of previous versions) so response size must be specified as EXACTLY
2936 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2937 * of eight bytes. Currently that is the only case where we set max
2938 * response size smaller.
Steve French4a72daf2013-06-25 00:20:49 -05002939 */
Steve French153322f2019-03-28 22:32:49 -05002940 req->MaxOutputResponse = cpu_to_le32(max_response_size);
Steve French4a72daf2013-06-25 00:20:49 -05002941
2942 if (is_fsctl)
2943 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2944 else
2945 req->Flags = 0;
2946
Steve French4587eee2017-10-25 15:58:31 -05002947 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2948 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002949 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002950
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002951 return 0;
2952}
2953
2954void
2955SMB2_ioctl_free(struct smb_rqst *rqst)
2956{
Murphy Zhou6457c202019-05-23 12:12:43 +08002957 int i;
Long Li2c87d6a2019-05-15 14:09:05 -07002958 if (rqst && rqst->rq_iov) {
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002959 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Murphy Zhou6457c202019-05-23 12:12:43 +08002960 for (i = 1; i < rqst->rq_nvec; i++)
2961 if (rqst->rq_iov[i].iov_base != smb2_padding)
2962 kfree(rqst->rq_iov[i].iov_base);
Long Li2c87d6a2019-05-15 14:09:05 -07002963 }
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002964}
2965
Steve French153322f2019-03-28 22:32:49 -05002966
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002967/*
2968 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2969 */
2970int
2971SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2972 u64 volatile_fid, u32 opcode, bool is_fsctl,
Steve French153322f2019-03-28 22:32:49 -05002973 char *in_data, u32 indatalen, u32 max_out_data_len,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002974 char **out_data, u32 *plen /* returned data len */)
2975{
2976 struct smb_rqst rqst;
2977 struct smb2_ioctl_rsp *rsp = NULL;
2978 struct cifs_ses *ses;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002979 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002980 struct kvec rsp_iov = {NULL, 0};
2981 int resp_buftype = CIFS_NO_BUFFER;
2982 int rc = 0;
2983 int flags = 0;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002984 struct TCP_Server_Info *server;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002985
2986 cifs_dbg(FYI, "SMB2 IOCTL\n");
2987
2988 if (out_data != NULL)
2989 *out_data = NULL;
2990
2991 /* zero out returned data len, in case of error */
2992 if (plen)
2993 *plen = 0;
2994
2995 if (tcon)
2996 ses = tcon->ses;
2997 else
2998 return -EIO;
2999
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003000 if (!ses)
3001 return -EIO;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003002 server = ses->server;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003003 if (!server)
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003004 return -EIO;
3005
3006 if (smb3_encryption_required(tcon))
3007 flags |= CIFS_TRANSFORM_REQ;
3008
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003009 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003010 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003011 rqst.rq_iov = iov;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003012 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003013
Steve French153322f2019-03-28 22:32:49 -05003014 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
3015 is_fsctl, in_data, indatalen, max_out_data_len);
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003016 if (rc)
3017 goto ioctl_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003018
3019 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11003020 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003021 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05003022
Steve Frencheccb4422018-05-17 21:16:55 -05003023 if (rc != 0)
3024 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3025 ses->Suid, 0, opcode, rc);
3026
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003027 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
Steve French8e353102015-03-26 19:47:02 -05003028 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05003029 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06003030 } else if (rc == -EINVAL) {
3031 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3032 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05003033 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06003034 goto ioctl_exit;
3035 }
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003036 } else if (rc == -E2BIG) {
3037 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3038 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3039 goto ioctl_exit;
3040 }
Steve French4a72daf2013-06-25 00:20:49 -05003041 }
3042
3043 /* check if caller wants to look at return data or just return rc */
3044 if ((plen == NULL) || (out_data == NULL))
3045 goto ioctl_exit;
3046
3047 *plen = le32_to_cpu(rsp->OutputCount);
3048
3049 /* We check for obvious errors in the output buffer length and offset */
3050 if (*plen == 0)
3051 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03003052 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003053 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
Steve French4a72daf2013-06-25 00:20:49 -05003054 *plen = 0;
3055 rc = -EIO;
3056 goto ioctl_exit;
3057 }
3058
Dan Carpenter2d204ee2018-09-10 14:12:07 +03003059 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003060 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
Steve French4a72daf2013-06-25 00:20:49 -05003061 le32_to_cpu(rsp->OutputOffset));
3062 *plen = 0;
3063 rc = -EIO;
3064 goto ioctl_exit;
3065 }
3066
YueHaibingd034fee2018-09-10 01:33:06 +00003067 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3068 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05003069 if (*out_data == NULL) {
3070 rc = -ENOMEM;
3071 goto ioctl_exit;
3072 }
3073
Steve French4a72daf2013-06-25 00:20:49 -05003074ioctl_exit:
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003075 SMB2_ioctl_free(&rqst);
Steve French4a72daf2013-06-25 00:20:49 -05003076 free_rsp_buf(resp_buftype, rsp);
3077 return rc;
3078}
3079
Steve French64a5cfa2013-10-14 15:31:32 -05003080/*
3081 * Individual callers to ioctl worker function follow
3082 */
3083
3084int
3085SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3086 u64 persistent_fid, u64 volatile_fid)
3087{
3088 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05003089 struct compress_ioctl fsctl_input;
3090 char *ret_data = NULL;
3091
3092 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08003093 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05003094
3095 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3096 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3097 (char *)&fsctl_input /* data input */,
Steve French153322f2019-03-28 22:32:49 -05003098 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3099 &ret_data /* out data */, NULL);
Steve French64a5cfa2013-10-14 15:31:32 -05003100
3101 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05003102
3103 return rc;
3104}
3105
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003106int
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003107SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Steve French43f8a6a2019-12-02 21:46:54 -06003108 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003109{
3110 struct smb2_close_req *req;
3111 struct kvec *iov = rqst->rq_iov;
3112 unsigned int total_len;
3113 int rc;
3114
3115 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
3116 if (rc)
3117 return rc;
3118
3119 req->PersistentFileId = persistent_fid;
3120 req->VolatileFileId = volatile_fid;
Steve French43f8a6a2019-12-02 21:46:54 -06003121 if (query_attrs)
3122 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3123 else
3124 req->Flags = 0;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003125 iov[0].iov_base = (char *)req;
3126 iov[0].iov_len = total_len;
3127
3128 return 0;
3129}
3130
3131void
3132SMB2_close_free(struct smb_rqst *rqst)
3133{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003134 if (rqst && rqst->rq_iov)
3135 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003136}
3137
3138int
Steve French43f8a6a2019-12-02 21:46:54 -06003139__SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3140 u64 persistent_fid, u64 volatile_fid,
3141 struct smb2_file_network_open_info *pbuf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003142{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003143 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003144 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003145 struct cifs_ses *ses = tcon->ses;
3146 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003147 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01003148 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003149 int rc = 0;
Steve French9e8fae22019-12-02 17:55:41 -06003150 int flags = 0;
Steve French43f8a6a2019-12-02 21:46:54 -06003151 bool query_attrs = false;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003152
Joe Perchesf96637b2013-05-04 22:12:25 -05003153 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003154
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003155 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003156 return -EIO;
3157
Steve French5a77e752018-05-09 17:43:08 -05003158 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003159 flags |= CIFS_TRANSFORM_REQ;
3160
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003161 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003162 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003163 rqst.rq_iov = iov;
3164 rqst.rq_nvec = 1;
3165
Steve French43f8a6a2019-12-02 21:46:54 -06003166 /* check if need to ask server to return timestamps in close response */
3167 if (pbuf)
3168 query_attrs = true;
3169
Steve Frenchf90f9792019-09-03 18:35:42 -05003170 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Steve French43f8a6a2019-12-02 21:46:54 -06003171 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid,
3172 query_attrs);
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003173 if (rc)
3174 goto close_exit;
3175
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003176 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003177 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003178
3179 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09003180 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003181 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3182 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003183 goto close_exit;
Steve French43f8a6a2019-12-02 21:46:54 -06003184 } else {
Steve Frenchf90f9792019-09-03 18:35:42 -05003185 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3186 ses->Suid);
Steve French43f8a6a2019-12-02 21:46:54 -06003187 /*
3188 * Note that have to subtract 4 since struct network_open_info
3189 * has a final 4 byte pad that close response does not have
3190 */
3191 if (pbuf)
3192 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3193 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003194
Steve Frenchfae80442018-10-19 17:14:32 -05003195 atomic_dec(&tcon->num_remote_opens);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003196close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003197 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003198 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003199
3200 /* retry close in a worker thread if this one is interrupted */
3201 if (rc == -EINTR) {
Steve French9e8fae22019-12-02 17:55:41 -06003202 int tmp_rc;
3203
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003204 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3205 volatile_fid);
3206 if (tmp_rc)
3207 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3208 persistent_fid, tmp_rc);
3209 }
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003210 return rc;
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06003211}
3212
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003213int
Steve French43f8a6a2019-12-02 21:46:54 -06003214SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3215 u64 persistent_fid, u64 volatile_fid)
3216{
3217 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3218}
3219
3220int
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003221smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3222 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003223{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003224 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003225 char *end_of_smb = smb_len + (char *)iov->iov_base;
3226 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003227 char *end_of_buf = begin_of_buf + buffer_length;
3228
3229
3230 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003231 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3232 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003233 return -EINVAL;
3234 }
3235
3236 /* check if beyond RFC1001 maximum length */
3237 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003238 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3239 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003240 return -EINVAL;
3241 }
3242
3243 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07003244 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003245 return -EINVAL;
3246 }
3247
3248 return 0;
3249}
3250
3251/*
3252 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3253 * Caller must free buffer.
3254 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003255int
3256smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3257 struct kvec *iov, unsigned int minbufsize,
3258 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003259{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003260 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003261 int rc;
3262
3263 if (!data)
3264 return -EINVAL;
3265
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003266 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003267 if (rc)
3268 return rc;
3269
3270 memcpy(data, begin_of_buf, buffer_length);
3271
3272 return 0;
3273}
3274
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003275int
3276SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3277 u64 persistent_fid, u64 volatile_fid,
3278 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003279 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003280{
3281 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003282 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003283 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003284 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003285
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003286 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3287 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003288 if (rc)
3289 return rc;
3290
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003291 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003292 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003293 req->PersistentFileId = persistent_fid;
3294 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003295 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02003296
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003297 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003298 if (input_len) {
3299 req->InputBufferLength = cpu_to_le32(input_len);
3300 /* total_len for smb query request never close to le16 max */
3301 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3302 memcpy(req->Buffer, input, input_len);
3303 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003304
3305 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003306 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003307 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003308 return 0;
3309}
3310
3311void
3312SMB2_query_info_free(struct smb_rqst *rqst)
3313{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003314 if (rqst && rqst->rq_iov)
3315 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003316}
3317
3318static int
3319query_info(const unsigned int xid, struct cifs_tcon *tcon,
3320 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3321 u32 additional_info, size_t output_len, size_t min_len, void **data,
3322 u32 *dlen)
3323{
3324 struct smb_rqst rqst;
3325 struct smb2_query_info_rsp *rsp = NULL;
3326 struct kvec iov[1];
3327 struct kvec rsp_iov;
3328 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01003329 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003330 struct cifs_ses *ses = tcon->ses;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003331 struct TCP_Server_Info *server;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003332 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003333 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003334
3335 cifs_dbg(FYI, "Query Info\n");
3336
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003337 if (!ses)
3338 return -EIO;
3339 server = ses->server;
3340 if (!server)
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003341 return -EIO;
3342
3343 if (smb3_encryption_required(tcon))
3344 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003345
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003346 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003347 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003348 rqst.rq_iov = iov;
3349 rqst.rq_nvec = 1;
3350
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003351 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3352 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003353 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003354 if (rc)
3355 goto qinf_exit;
3356
Steve Frenchd42043a2019-02-26 21:58:30 -06003357 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3358 ses->Suid, info_class, (__u32)info_type);
3359
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003360 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003361 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003362
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003363 if (rc) {
3364 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003365 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3366 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003367 goto qinf_exit;
3368 }
3369
Steve Frenchd42043a2019-02-26 21:58:30 -06003370 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3371 ses->Suid, info_class, (__u32)info_type);
3372
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003373 if (dlen) {
3374 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3375 if (!*data) {
3376 *data = kmalloc(*dlen, GFP_KERNEL);
3377 if (!*data) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003378 cifs_tcon_dbg(VFS,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003379 "Error %d allocating memory for acl\n",
3380 rc);
3381 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003382 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003383 goto qinf_exit;
3384 }
Colin Ian King73aaf922019-01-16 16:28:59 +00003385 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003386 }
3387 }
3388
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003389 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3390 le32_to_cpu(rsp->OutputBufferLength),
3391 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00003392 if (rc && allocated) {
3393 kfree(*data);
3394 *data = NULL;
3395 *dlen = 0;
3396 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003397
3398qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003399 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003400 free_rsp_buf(resp_buftype, rsp);
3401 return rc;
3402}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003403
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003404int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3405 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003406{
3407 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003408 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04003409 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003410 sizeof(struct smb2_file_all_info), (void **)&data,
3411 NULL);
3412}
3413
3414int
3415SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3416 u64 persistent_fid, u64 volatile_fid,
3417 void **data, u32 *plen)
3418{
3419 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3420 *plen = 0;
3421
3422 return query_info(xid, tcon, persistent_fid, volatile_fid,
3423 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05003424 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003425}
3426
3427int
3428SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3429 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3430{
3431 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003432 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003433 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003434 sizeof(struct smb2_file_internal_info),
3435 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003436}
3437
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003438/*
Steve Frenchc3498182019-09-15 22:38:52 -05003439 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3440 * See MS-SMB2 2.2.35 and 2.2.36
3441 */
3442
zhengbin388962e2019-09-23 15:06:18 +08003443static int
Steve Frenchc3498182019-09-15 22:38:52 -05003444SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3445 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid,
3446 u32 completion_filter, bool watch_tree)
3447{
3448 struct smb2_change_notify_req *req;
3449 struct kvec *iov = rqst->rq_iov;
3450 unsigned int total_len;
3451 int rc;
3452
3453 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len);
3454 if (rc)
3455 return rc;
3456
3457 req->PersistentFileId = persistent_fid;
3458 req->VolatileFileId = volatile_fid;
Steve Frenchd26c2dd2020-02-06 06:00:14 -06003459 /* See note 354 of MS-SMB2, 64K max */
Steve French52870d52019-10-01 21:25:46 -05003460 req->OutputBufferLength =
3461 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
Steve Frenchc3498182019-09-15 22:38:52 -05003462 req->CompletionFilter = cpu_to_le32(completion_filter);
3463 if (watch_tree)
3464 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3465 else
3466 req->Flags = 0;
3467
3468 iov[0].iov_base = (char *)req;
3469 iov[0].iov_len = total_len;
3470
3471 return 0;
3472}
3473
3474int
3475SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3476 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3477 u32 completion_filter)
3478{
3479 struct cifs_ses *ses = tcon->ses;
3480 struct smb_rqst rqst;
3481 struct kvec iov[1];
3482 struct kvec rsp_iov = {NULL, 0};
3483 int resp_buftype = CIFS_NO_BUFFER;
3484 int flags = 0;
3485 int rc = 0;
3486
3487 cifs_dbg(FYI, "change notify\n");
3488 if (!ses || !(ses->server))
3489 return -EIO;
3490
3491 if (smb3_encryption_required(tcon))
3492 flags |= CIFS_TRANSFORM_REQ;
3493
3494 memset(&rqst, 0, sizeof(struct smb_rqst));
3495 memset(&iov, 0, sizeof(iov));
3496 rqst.rq_iov = iov;
3497 rqst.rq_nvec = 1;
3498
3499 rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid,
3500 completion_filter, watch_tree);
3501 if (rc)
3502 goto cnotify_exit;
3503
3504 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3505 (u8)watch_tree, completion_filter);
3506 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3507
3508 if (rc != 0) {
3509 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3510 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3511 (u8)watch_tree, completion_filter, rc);
3512 } else
3513 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3514 ses->Suid, (u8)watch_tree, completion_filter);
3515
3516 cnotify_exit:
3517 if (rqst.rq_iov)
3518 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3519 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3520 return rc;
3521}
3522
3523
3524
3525/*
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003526 * This is a no-op for now. We're not really interested in the reply, but
3527 * rather in the fact that the server sent one and that server->lstrp
3528 * gets updated.
3529 *
3530 * FIXME: maybe we should consider checking that the reply matches request?
3531 */
3532static void
3533smb2_echo_callback(struct mid_q_entry *mid)
3534{
3535 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003536 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003537 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003538
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003539 if (mid->mid_state == MID_RESPONSE_RECEIVED
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003540 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3541 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3542 credits.instance = server->reconnect_instance;
3543 }
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003544
3545 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003546 add_credits(server, &credits, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003547}
3548
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003549void smb2_reconnect_server(struct work_struct *work)
3550{
3551 struct TCP_Server_Info *server = container_of(work,
3552 struct TCP_Server_Info, reconnect.work);
3553 struct cifs_ses *ses;
3554 struct cifs_tcon *tcon, *tcon2;
3555 struct list_head tmp_list;
3556 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01003557 int rc;
3558 int resched = false;
3559
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003560
3561 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3562 mutex_lock(&server->reconnect_mutex);
3563
3564 INIT_LIST_HEAD(&tmp_list);
3565 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3566
3567 spin_lock(&cifs_tcp_ses_lock);
3568 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3569 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003570 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003571 tcon->tc_count++;
3572 list_add_tail(&tcon->rlist, &tmp_list);
3573 tcon_exist = true;
3574 }
3575 }
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003576 /*
3577 * IPC has the same lifetime as its session and uses its
3578 * refcount.
3579 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003580 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3581 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3582 tcon_exist = true;
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003583 ses->ses_count++;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003584 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003585 }
3586 /*
3587 * Get the reference to server struct to be sure that the last call of
3588 * cifs_put_tcon() in the loop below won't release the server pointer.
3589 */
3590 if (tcon_exist)
3591 server->srv_count++;
3592
3593 spin_unlock(&cifs_tcp_ses_lock);
3594
3595 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01003596 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3597 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003598 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01003599 else
3600 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003601 list_del_init(&tcon->rlist);
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003602 if (tcon->ipc)
3603 cifs_put_smb_ses(tcon->ses);
3604 else
3605 cifs_put_tcon(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003606 }
3607
3608 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01003609 if (resched)
3610 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003611 mutex_unlock(&server->reconnect_mutex);
3612
3613 /* now we can safely release srv struct */
3614 if (tcon_exist)
3615 cifs_put_tcp_session(server, 1);
3616}
3617
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003618int
3619SMB2_echo(struct TCP_Server_Info *server)
3620{
3621 struct smb2_echo_req *req;
3622 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003623 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003624 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003625 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003626 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003627
Joe Perchesf96637b2013-05-04 22:12:25 -05003628 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003629
Steve French4fcd1812016-06-22 20:12:05 -05003630 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003631 /* No need to send echo on newly established connections */
Stefan Metzmacherb08484d2020-02-24 14:14:59 +01003632 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003633 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003634 }
3635
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003636 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003637 if (rc)
3638 return rc;
3639
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003640 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003641
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003642 iov[0].iov_len = total_len;
3643 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003644
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003645 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003646 server, CIFS_ECHO_OP, NULL);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003647 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003648 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003649
3650 cifs_small_buf_release(req);
3651 return rc;
3652}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003653
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003654void
3655SMB2_flush_free(struct smb_rqst *rqst)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003656{
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003657 if (rqst && rqst->rq_iov)
3658 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3659}
3660
3661int
3662SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3663 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3664{
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003665 struct smb2_flush_req *req;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003666 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003667 unsigned int total_len;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003668 int rc;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003669
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003670 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003671 if (rc)
3672 return rc;
3673
3674 req->PersistentFileId = persistent_fid;
3675 req->VolatileFileId = volatile_fid;
3676
3677 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003678 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003679
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003680 return 0;
3681}
3682
3683int
3684SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3685 u64 volatile_fid)
3686{
3687 struct cifs_ses *ses = tcon->ses;
3688 struct smb_rqst rqst;
3689 struct kvec iov[1];
3690 struct kvec rsp_iov = {NULL, 0};
3691 int resp_buftype = CIFS_NO_BUFFER;
3692 int flags = 0;
3693 int rc = 0;
3694
3695 cifs_dbg(FYI, "flush\n");
3696 if (!ses || !(ses->server))
3697 return -EIO;
3698
3699 if (smb3_encryption_required(tcon))
3700 flags |= CIFS_TRANSFORM_REQ;
3701
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003702 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003703 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003704 rqst.rq_iov = iov;
3705 rqst.rq_nvec = 1;
3706
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003707 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3708 if (rc)
3709 goto flush_exit;
3710
Steve Frenchf90f9792019-09-03 18:35:42 -05003711 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003712 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003713
Steve Frencheccb4422018-05-17 21:16:55 -05003714 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003715 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003716 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3717 rc);
Steve Frenchf90f9792019-09-03 18:35:42 -05003718 } else
3719 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3720 ses->Suid);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003721
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003722 flush_exit:
3723 SMB2_flush_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003724 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003725 return rc;
3726}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003727
3728/*
3729 * To form a chain of read requests, any read requests after the first should
3730 * have the end_of_chain boolean set to true.
3731 */
3732static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003733smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003734 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3735 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003736{
3737 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003738 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003739 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07003740 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003741
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003742 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3743 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003744 if (rc)
3745 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003746
3747 server = io_parms->tcon->ses->server;
3748 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003749 return -ECONNABORTED;
3750
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003751 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003752 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003753
3754 req->PersistentFileId = io_parms->persistent_fid;
3755 req->VolatileFileId = io_parms->volatile_fid;
3756 req->ReadChannelInfoOffset = 0; /* reserved */
3757 req->ReadChannelInfoLength = 0; /* reserved */
3758 req->Channel = 0; /* reserved */
3759 req->MinimumCount = 0;
3760 req->Length = cpu_to_le32(io_parms->length);
3761 req->Offset = cpu_to_le64(io_parms->offset);
Steve Frenchd323c2462019-02-25 00:52:43 -06003762
3763 trace_smb3_read_enter(0 /* xid */,
3764 io_parms->persistent_fid,
3765 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3766 io_parms->offset, io_parms->length);
Long Libd3dcc62017-11-22 17:38:47 -07003767#ifdef CONFIG_CIFS_SMB_DIRECT
3768 /*
3769 * If we want to do a RDMA write, fill in and append
3770 * smbd_buffer_descriptor_v1 to the end of read request
3771 */
Long Libb4c0412018-04-17 12:17:08 -07003772 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003773 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003774
Long Libd3dcc62017-11-22 17:38:47 -07003775 struct smbd_buffer_descriptor_v1 *v1;
3776 bool need_invalidate =
3777 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3778
3779 rdata->mr = smbd_register_mr(
3780 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003781 rdata->nr_pages, rdata->page_offset,
3782 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003783 if (!rdata->mr)
Long Lib7972092019-04-05 21:36:34 +00003784 return -EAGAIN;
Long Libd3dcc62017-11-22 17:38:47 -07003785
3786 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3787 if (need_invalidate)
3788 req->Channel = SMB2_CHANNEL_RDMA_V1;
3789 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003790 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003791 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003792 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003793 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003794 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3795 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3796 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003797
3798 *total_len += sizeof(*v1) - 1;
3799 }
3800#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003801 if (request_type & CHAINED_REQUEST) {
3802 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003803 /* next 8-byte aligned request */
3804 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3805 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003806 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003807 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003808 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003809 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003810 /*
3811 * Related requests use info from previous read request
3812 * in chain.
3813 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003814 shdr->SessionId = 0xFFFFFFFF;
3815 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003816 req->PersistentFileId = 0xFFFFFFFF;
3817 req->VolatileFileId = 0xFFFFFFFF;
3818 }
3819 }
3820 if (remaining_bytes > io_parms->length)
3821 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3822 else
3823 req->RemainingBytes = 0;
3824
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003825 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003826 return rc;
3827}
3828
3829static void
3830smb2_readv_callback(struct mid_q_entry *mid)
3831{
3832 struct cifs_readdata *rdata = mid->callback_data;
3833 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3834 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003835 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003836 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003837 struct cifs_credits credits = { .value = 0, .instance = 0 };
Steve French46f17d12019-09-04 23:07:52 -05003838 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3839 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07003840 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003841 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003842 .rq_npages = rdata->nr_pages,
3843 .rq_pagesz = rdata->pagesz,
3844 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003845
Joe Perchesf96637b2013-05-04 22:12:25 -05003846 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3847 __func__, mid->mid, mid->mid_state, rdata->result,
3848 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003849
3850 switch (mid->mid_state) {
3851 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003852 credits.value = le16_to_cpu(shdr->CreditRequest);
3853 credits.instance = server->reconnect_instance;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003854 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003855 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003856 int rc;
3857
Jeff Layton0b688cf2012-09-18 16:20:34 -07003858 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003859 if (rc)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003860 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003861 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003862 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003863 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003864 task_io_account_read(rdata->got_bytes);
3865 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003866 break;
3867 case MID_REQUEST_SUBMITTED:
3868 case MID_RETRY_NEEDED:
3869 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003870 if (server->sign && rdata->got_bytes)
3871 /* reset bytes number since we can not check a sign */
3872 rdata->got_bytes = 0;
3873 /* FIXME: should this be counted toward the initiating task? */
3874 task_io_account_read(rdata->got_bytes);
3875 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003876 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003877 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003878 credits.value = le16_to_cpu(shdr->CreditRequest);
3879 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003880 /* fall through */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003881 default:
Pavel Shilovsky6b15eb12019-01-18 15:46:14 -08003882 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003883 }
Long Libd3dcc62017-11-22 17:38:47 -07003884#ifdef CONFIG_CIFS_SMB_DIRECT
3885 /*
3886 * If this rdata has a memmory registered, the MR can be freed
3887 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3888 * because they have limited number and are used for future I/Os
3889 */
3890 if (rdata->mr) {
3891 smbd_deregister_mr(rdata->mr);
3892 rdata->mr = NULL;
3893 }
3894#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08003895 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003896 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003897 trace_smb3_read_err(0 /* xid */,
3898 rdata->cfile->fid.persistent_fid,
3899 tcon->tid, tcon->ses->Suid, rdata->offset,
3900 rdata->bytes, rdata->result);
3901 } else
3902 trace_smb3_read_done(0 /* xid */,
3903 rdata->cfile->fid.persistent_fid,
3904 tcon->tid, tcon->ses->Suid,
3905 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003906
3907 queue_work(cifsiod_wq, &rdata->work);
3908 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003909 add_credits(server, &credits, 0);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003910}
3911
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003912/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003913int
3914smb2_async_readv(struct cifs_readdata *rdata)
3915{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003916 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003917 char *buf;
3918 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003919 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003920 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003921 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003922 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003923 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003924
Joe Perchesf96637b2013-05-04 22:12:25 -05003925 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3926 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003927
3928 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3929 io_parms.offset = rdata->offset;
3930 io_parms.length = rdata->bytes;
3931 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3932 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3933 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003934
3935 server = io_parms.tcon->ses->server;
3936
Long Li2dabfd52017-11-07 01:54:53 -07003937 rc = smb2_new_read_req(
3938 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003939 if (rc)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003940 return rc;
3941
Steve French5a77e752018-05-09 17:43:08 -05003942 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003943 flags |= CIFS_TRANSFORM_REQ;
3944
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003945 rdata->iov[0].iov_base = buf;
3946 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003947
3948 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003949
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003950 if (rdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003951 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003952 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003953 shdr->CreditRequest =
3954 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003955
3956 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3957 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003958 goto async_readv_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003959
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003960 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003961 }
3962
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003963 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07003964 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003965 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003966 smb3_handle_read_data, rdata, flags,
3967 &rdata->credits);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003968 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003969 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003970 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003971 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3972 io_parms.tcon->tid,
3973 io_parms.tcon->ses->Suid,
3974 io_parms.offset, io_parms.length, rc);
3975 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003976
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003977async_readv_out:
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003978 cifs_small_buf_release(buf);
3979 return rc;
3980}
Pavel Shilovsky33319142012-09-18 16:20:29 -07003981
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003982int
3983SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3984 unsigned int *nbytes, char **buf, int *buf_type)
3985{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003986 struct smb_rqst rqst;
Colin Ian King1efd4fc2019-07-31 10:05:26 +01003987 int resp_buftype, rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003988 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003989 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003990 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003991 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003992 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003993 int flags = CIFS_LOG_ERROR;
3994 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003995
3996 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07003997 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003998 if (rc)
3999 return rc;
4000
Steve French5a77e752018-05-09 17:43:08 -05004001 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004002 flags |= CIFS_TRANSFORM_REQ;
4003
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004004 iov[0].iov_base = (char *)req;
4005 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004006
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004007 memset(&rqst, 0, sizeof(struct smb_rqst));
4008 rqst.rq_iov = iov;
4009 rqst.rq_nvec = 1;
4010
4011 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004012 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004013
4014 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004015 if (rc != -ENODATA) {
4016 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4017 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004018 trace_smb3_read_err(xid, req->PersistentFileId,
4019 io_parms->tcon->tid, ses->Suid,
4020 io_parms->offset, io_parms->length,
4021 rc);
Steve Frenchb0a42f22019-02-25 15:02:58 -06004022 } else
4023 trace_smb3_read_done(xid, req->PersistentFileId,
4024 io_parms->tcon->tid, ses->Suid,
4025 io_parms->offset, 0);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004026 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Ronnie Sahlberg05fd5c22019-04-23 16:39:45 +10004027 cifs_small_buf_release(req);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004028 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05004029 } else
4030 trace_smb3_read_done(xid, req->PersistentFileId,
4031 io_parms->tcon->tid, ses->Suid,
4032 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004033
ZhangXiaoxu088aaf12019-04-06 15:47:39 +08004034 cifs_small_buf_release(req);
4035
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004036 *nbytes = le32_to_cpu(rsp->DataLength);
4037 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4038 (*nbytes > io_parms->length)) {
4039 cifs_dbg(FYI, "bad length %d for count %d\n",
4040 *nbytes, io_parms->length);
4041 rc = -EIO;
4042 *nbytes = 0;
4043 }
4044
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004045 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004046 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004047 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004048 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004049 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004050 if (resp_buftype == CIFS_SMALL_BUFFER)
4051 *buf_type = CIFS_SMALL_BUFFER;
4052 else if (resp_buftype == CIFS_LARGE_BUFFER)
4053 *buf_type = CIFS_LARGE_BUFFER;
4054 }
4055 return rc;
4056}
4057
Pavel Shilovsky33319142012-09-18 16:20:29 -07004058/*
4059 * Check the mid_state and signature on received buffer (if any), and queue the
4060 * workqueue completion task.
4061 */
4062static void
4063smb2_writev_callback(struct mid_q_entry *mid)
4064{
4065 struct cifs_writedata *wdata = mid->callback_data;
4066 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004067 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004068 unsigned int written;
4069 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004070 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky33319142012-09-18 16:20:29 -07004071
4072 switch (mid->mid_state) {
4073 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004074 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4075 credits.instance = server->reconnect_instance;
4076 wdata->result = smb2_check_receive(mid, server, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004077 if (wdata->result != 0)
4078 break;
4079
4080 written = le32_to_cpu(rsp->DataLength);
4081 /*
4082 * Mask off high 16 bits when bytes written as returned
4083 * by the server is greater than bytes requested by the
4084 * client. OS/2 servers are known to set incorrect
4085 * CountHigh values.
4086 */
4087 if (written > wdata->bytes)
4088 written &= 0xFFFF;
4089
4090 if (written < wdata->bytes)
4091 wdata->result = -ENOSPC;
4092 else
4093 wdata->bytes = written;
4094 break;
4095 case MID_REQUEST_SUBMITTED:
4096 case MID_RETRY_NEEDED:
4097 wdata->result = -EAGAIN;
4098 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08004099 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004100 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4101 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08004102 /* fall through */
Pavel Shilovsky33319142012-09-18 16:20:29 -07004103 default:
4104 wdata->result = -EIO;
4105 break;
4106 }
Long Lidb223a52017-11-22 17:38:45 -07004107#ifdef CONFIG_CIFS_SMB_DIRECT
4108 /*
4109 * If this wdata has a memory registered, the MR can be freed
4110 * The number of MRs available is limited, it's important to recover
4111 * used MR as soon as I/O is finished. Hold MR longer in the later
4112 * I/O process can possibly result in I/O deadlock due to lack of MR
4113 * to send request on I/O retry
4114 */
4115 if (wdata->mr) {
4116 smbd_deregister_mr(wdata->mr);
4117 wdata->mr = NULL;
4118 }
4119#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004120 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07004121 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004122 trace_smb3_write_err(0 /* no xid */,
4123 wdata->cfile->fid.persistent_fid,
4124 tcon->tid, tcon->ses->Suid, wdata->offset,
4125 wdata->bytes, wdata->result);
Steve Frenchd6fd4192020-02-05 16:52:11 -06004126 if (wdata->result == -ENOSPC)
Joe Perchesa0a30362020-04-14 22:42:53 -07004127 pr_warn_once("Out of space writing to %s\n",
4128 tcon->treeName);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004129 } else
4130 trace_smb3_write_done(0 /* no xid */,
4131 wdata->cfile->fid.persistent_fid,
4132 tcon->tid, tcon->ses->Suid,
4133 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004134
4135 queue_work(cifsiod_wq, &wdata->work);
4136 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004137 add_credits(server, &credits, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004138}
4139
4140/* smb2_async_writev - send an async write, and set up mid to handle result */
4141int
Steve French4a5c80d2014-02-07 20:45:12 -06004142smb2_async_writev(struct cifs_writedata *wdata,
4143 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07004144{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004145 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004146 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004147 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004148 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004149 struct TCP_Server_Info *server = tcon->ses->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004150 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004151 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004152 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004153
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004154 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08004155 if (rc)
4156 return rc;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004157
Steve French5a77e752018-05-09 17:43:08 -05004158 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004159 flags |= CIFS_TRANSFORM_REQ;
4160
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004161 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004162 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004163
4164 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4165 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4166 req->WriteChannelInfoOffset = 0;
4167 req->WriteChannelInfoLength = 0;
4168 req->Channel = 0;
4169 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004170 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004171 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07004172 req->RemainingBytes = 0;
Steve Frenchd323c2462019-02-25 00:52:43 -06004173
4174 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4175 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07004176#ifdef CONFIG_CIFS_SMB_DIRECT
4177 /*
4178 * If we want to do a server RDMA read, fill in and append
4179 * smbd_buffer_descriptor_v1 to the end of write request
4180 */
Long Libb4c0412018-04-17 12:17:08 -07004181 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07004182 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07004183
Long Lidb223a52017-11-22 17:38:45 -07004184 struct smbd_buffer_descriptor_v1 *v1;
4185 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4186
4187 wdata->mr = smbd_register_mr(
4188 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07004189 wdata->nr_pages, wdata->page_offset,
4190 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07004191 if (!wdata->mr) {
Long Lib7972092019-04-05 21:36:34 +00004192 rc = -EAGAIN;
Long Lidb223a52017-11-22 17:38:45 -07004193 goto async_writev_out;
4194 }
4195 req->Length = 0;
4196 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07004197 if (wdata->nr_pages > 1)
4198 req->RemainingBytes =
4199 cpu_to_le32(
4200 (wdata->nr_pages - 1) * wdata->pagesz -
4201 wdata->page_offset + wdata->tailsz
4202 );
4203 else
4204 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07004205 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4206 if (need_invalidate)
4207 req->Channel = SMB2_CHANNEL_RDMA_V1;
4208 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06004209 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07004210 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06004211 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07004212 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06004213 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4214 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4215 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07004216 }
4217#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004218 iov[0].iov_len = total_len - 1;
4219 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004220
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004221 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004222 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07004223 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07004224 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07004225 rqst.rq_npages = wdata->nr_pages;
4226 rqst.rq_pagesz = wdata->pagesz;
4227 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07004228#ifdef CONFIG_CIFS_SMB_DIRECT
4229 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004230 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07004231 rqst.rq_npages = 0;
4232 }
4233#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05004234 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4235 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004236
Long Lidb223a52017-11-22 17:38:45 -07004237#ifdef CONFIG_CIFS_SMB_DIRECT
4238 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4239 if (!wdata->mr)
4240 req->Length = cpu_to_le32(wdata->bytes);
4241#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07004242 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07004243#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07004244
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004245 if (wdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004246 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004247 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00004248 shdr->CreditRequest =
4249 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004250
4251 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4252 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004253 goto async_writev_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004254
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004255 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004256 }
4257
Pavel Shilovsky33319142012-09-18 16:20:29 -07004258 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08004259 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08004260 wdata, flags, &wdata->credits);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004261
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004262 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004263 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4264 tcon->tid, tcon->ses->Suid, wdata->offset,
4265 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06004266 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004267 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004268 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07004269
Pavel Shilovsky33319142012-09-18 16:20:29 -07004270async_writev_out:
4271 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004272 return rc;
4273}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004274
4275/*
4276 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4277 * The length field from io_parms must be at least 1 and indicates a number of
4278 * elements with data to write that begins with position 1 in iov array. All
4279 * data length is specified by count.
4280 */
4281int
4282SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4283 unsigned int *nbytes, struct kvec *iov, int n_vec)
4284{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004285 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004286 int rc = 0;
4287 struct smb2_write_req *req = NULL;
4288 struct smb2_write_rsp *rsp = NULL;
4289 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004290 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004291 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004292 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004293
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004294 *nbytes = 0;
4295
4296 if (n_vec < 1)
4297 return rc;
4298
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004299 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
4300 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004301 if (rc)
4302 return rc;
4303
4304 if (io_parms->tcon->ses->server == NULL)
4305 return -ECONNABORTED;
4306
Steve French5a77e752018-05-09 17:43:08 -05004307 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004308 flags |= CIFS_TRANSFORM_REQ;
4309
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004310 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004311
4312 req->PersistentFileId = io_parms->persistent_fid;
4313 req->VolatileFileId = io_parms->volatile_fid;
4314 req->WriteChannelInfoOffset = 0;
4315 req->WriteChannelInfoLength = 0;
4316 req->Channel = 0;
4317 req->Length = cpu_to_le32(io_parms->length);
4318 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004319 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004320 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004321 req->RemainingBytes = 0;
4322
Steve Frenchd323c2462019-02-25 00:52:43 -06004323 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4324 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4325 io_parms->offset, io_parms->length);
4326
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004327 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004328 /* 1 for Buffer */
4329 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004330
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004331 memset(&rqst, 0, sizeof(struct smb_rqst));
4332 rqst.rq_iov = iov;
4333 rqst.rq_nvec = n_vec + 1;
4334
4335 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004336 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004337 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004338
4339 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004340 trace_smb3_write_err(xid, req->PersistentFileId,
4341 io_parms->tcon->tid,
4342 io_parms->tcon->ses->Suid,
4343 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004344 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004345 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05004346 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004347 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05004348 trace_smb3_write_done(xid, req->PersistentFileId,
4349 io_parms->tcon->tid,
4350 io_parms->tcon->ses->Suid,
4351 io_parms->offset, *nbytes);
4352 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004353
ZhangXiaoxu6a3eb332019-04-06 15:47:38 +08004354 cifs_small_buf_release(req);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004355 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004356 return rc;
4357}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004358
Aurelien Aptel69dda302020-03-02 17:53:22 +01004359int posix_info_sid_size(const void *beg, const void *end)
Aurelien Aptel349e13a2020-02-08 15:50:57 +01004360{
4361 size_t subauth;
4362 int total;
4363
4364 if (beg + 1 > end)
4365 return -1;
4366
4367 subauth = *(u8 *)(beg+1);
4368 if (subauth < 1 || subauth > 15)
4369 return -1;
4370
4371 total = 1 + 1 + 6 + 4*subauth;
4372 if (beg + total > end)
4373 return -1;
4374
4375 return total;
4376}
4377
4378int posix_info_parse(const void *beg, const void *end,
4379 struct smb2_posix_info_parsed *out)
4380
4381{
4382 int total_len = 0;
4383 int sid_len;
4384 int name_len;
4385 const void *owner_sid;
4386 const void *group_sid;
4387 const void *name;
4388
4389 /* if no end bound given, assume payload to be correct */
4390 if (!end) {
4391 const struct smb2_posix_info *p = beg;
4392
4393 end = beg + le32_to_cpu(p->NextEntryOffset);
4394 /* last element will have a 0 offset, pick a sensible bound */
4395 if (end == beg)
4396 end += 0xFFFF;
4397 }
4398
4399 /* check base buf */
4400 if (beg + sizeof(struct smb2_posix_info) > end)
4401 return -1;
4402 total_len = sizeof(struct smb2_posix_info);
4403
4404 /* check owner sid */
4405 owner_sid = beg + total_len;
4406 sid_len = posix_info_sid_size(owner_sid, end);
4407 if (sid_len < 0)
4408 return -1;
4409 total_len += sid_len;
4410
4411 /* check group sid */
4412 group_sid = beg + total_len;
4413 sid_len = posix_info_sid_size(group_sid, end);
4414 if (sid_len < 0)
4415 return -1;
4416 total_len += sid_len;
4417
4418 /* check name len */
4419 if (beg + total_len + 4 > end)
4420 return -1;
4421 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4422 if (name_len < 1 || name_len > 0xFFFF)
4423 return -1;
4424 total_len += 4;
4425
4426 /* check name */
4427 name = beg + total_len;
4428 if (name + name_len > end)
4429 return -1;
4430 total_len += name_len;
4431
4432 if (out) {
4433 out->base = beg;
4434 out->size = total_len;
4435 out->name_len = name_len;
4436 out->name = name;
4437 memcpy(&out->owner, owner_sid,
4438 posix_info_sid_size(owner_sid, end));
4439 memcpy(&out->group, group_sid,
4440 posix_info_sid_size(group_sid, end));
4441 }
4442 return total_len;
4443}
4444
4445static int posix_info_extra_size(const void *beg, const void *end)
4446{
4447 int len = posix_info_parse(beg, end, NULL);
4448
4449 if (len < 0)
4450 return -1;
4451 return len - sizeof(struct smb2_posix_info);
4452}
4453
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004454static unsigned int
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004455num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4456 size_t size)
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004457{
4458 int len;
4459 unsigned int entrycount = 0;
4460 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03004461 char *entryptr;
4462 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004463
4464 if (bufstart == NULL)
4465 return 0;
4466
Dan Carpenter56446f22018-09-06 12:48:22 +03004467 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004468
4469 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03004470 if (entryptr + next_offset < entryptr ||
4471 entryptr + next_offset > end_of_buf ||
4472 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004473 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004474 break;
4475 }
4476
Dan Carpenter56446f22018-09-06 12:48:22 +03004477 entryptr = entryptr + next_offset;
4478 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4479
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004480 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4481 len = posix_info_extra_size(entryptr, end_of_buf);
4482 else
4483 len = le32_to_cpu(dir_info->FileNameLength);
4484
4485 if (len < 0 ||
4486 entryptr + len < entryptr ||
Dan Carpenter56446f22018-09-06 12:48:22 +03004487 entryptr + len > end_of_buf ||
4488 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004489 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4490 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004491 break;
4492 }
4493
Dan Carpenter56446f22018-09-06 12:48:22 +03004494 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004495 entrycount++;
4496
Dan Carpenter56446f22018-09-06 12:48:22 +03004497 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004498 if (!next_offset)
4499 break;
4500 }
4501
4502 return entrycount;
4503}
4504
4505/*
4506 * Readdir/FindFirst
4507 */
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004508int SMB2_query_directory_init(const unsigned int xid,
4509 struct cifs_tcon *tcon, struct smb_rqst *rqst,
4510 u64 persistent_fid, u64 volatile_fid,
4511 int index, int info_level)
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004512{
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004513 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004514 struct smb2_query_directory_req *req;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004515 unsigned char *bufptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004516 __le16 asteriks = cpu_to_le16('*');
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004517 unsigned int output_size = CIFSMaxBufSize -
4518 MAX_SMB2_CREATE_RESPONSE_SIZE -
4519 MAX_SMB2_CLOSE_RESPONSE_SIZE;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004520 unsigned int total_len;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004521 struct kvec *iov = rqst->rq_iov;
4522 int len, rc;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004523
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004524 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4525 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004526 if (rc)
4527 return rc;
4528
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004529 switch (info_level) {
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004530 case SMB_FIND_FILE_DIRECTORY_INFO:
4531 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004532 break;
4533 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4534 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004535 break;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004536 case SMB_FIND_FILE_POSIX_INFO:
4537 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4538 break;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004539 default:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004540 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004541 info_level);
4542 return -EINVAL;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004543 }
4544
4545 req->FileIndex = cpu_to_le32(index);
4546 req->PersistentFileId = persistent_fid;
4547 req->VolatileFileId = volatile_fid;
4548
4549 len = 0x2;
4550 bufptr = req->Buffer;
4551 memcpy(bufptr, &asteriks, len);
4552
4553 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004554 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004555 req->FileNameLength = cpu_to_le16(len);
4556 /*
4557 * BB could be 30 bytes or so longer if we used SMB2 specific
4558 * buffer lengths, but this is safe and close enough.
4559 */
4560 output_size = min_t(unsigned int, output_size, server->maxBuf);
4561 output_size = min_t(unsigned int, output_size, 2 << 15);
4562 req->OutputBufferLength = cpu_to_le32(output_size);
4563
4564 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004565 /* 1 for Buffer */
4566 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004567
4568 iov[1].iov_base = (char *)(req->Buffer);
4569 iov[1].iov_len = len;
4570
Steve Frenchd323c2462019-02-25 00:52:43 -06004571 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4572 tcon->ses->Suid, index, output_size);
4573
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004574 return 0;
4575}
4576
4577void SMB2_query_directory_free(struct smb_rqst *rqst)
4578{
4579 if (rqst && rqst->rq_iov) {
4580 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4581 }
4582}
4583
4584int
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004585smb2_parse_query_directory(struct cifs_tcon *tcon,
4586 struct kvec *rsp_iov,
4587 int resp_buftype,
4588 struct cifs_search_info *srch_inf)
4589{
4590 struct smb2_query_directory_rsp *rsp;
4591 size_t info_buf_size;
4592 char *end_of_smb;
4593 int rc;
4594
4595 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4596
4597 switch (srch_inf->info_level) {
4598 case SMB_FIND_FILE_DIRECTORY_INFO:
4599 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4600 break;
4601 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4602 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4603 break;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004604 case SMB_FIND_FILE_POSIX_INFO:
4605 /* note that posix payload are variable size */
4606 info_buf_size = sizeof(struct smb2_posix_info);
4607 break;
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004608 default:
4609 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4610 srch_inf->info_level);
4611 return -EINVAL;
4612 }
4613
4614 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4615 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4616 info_buf_size);
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004617 if (rc) {
4618 cifs_tcon_dbg(VFS, "bad info payload");
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004619 return rc;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004620 }
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004621
4622 srch_inf->unicode = true;
4623
4624 if (srch_inf->ntwrk_buf_start) {
4625 if (srch_inf->smallBuf)
4626 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4627 else
4628 cifs_buf_release(srch_inf->ntwrk_buf_start);
4629 }
4630 srch_inf->ntwrk_buf_start = (char *)rsp;
4631 srch_inf->srch_entries_start = srch_inf->last_entry =
4632 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4633 end_of_smb = rsp_iov->iov_len + (char *)rsp;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004634
4635 srch_inf->entries_in_buffer = num_entries(
4636 srch_inf->info_level,
4637 srch_inf->srch_entries_start,
4638 end_of_smb,
4639 &srch_inf->last_entry,
4640 info_buf_size);
4641
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004642 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4643 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4644 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4645 srch_inf->srch_entries_start, srch_inf->last_entry);
4646 if (resp_buftype == CIFS_LARGE_BUFFER)
4647 srch_inf->smallBuf = false;
4648 else if (resp_buftype == CIFS_SMALL_BUFFER)
4649 srch_inf->smallBuf = true;
4650 else
Joe Perchesa0a30362020-04-14 22:42:53 -07004651 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004652
4653 return 0;
4654}
4655
4656int
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004657SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4658 u64 persistent_fid, u64 volatile_fid, int index,
4659 struct cifs_search_info *srch_inf)
4660{
4661 struct smb_rqst rqst;
4662 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4663 struct smb2_query_directory_rsp *rsp = NULL;
4664 int resp_buftype = CIFS_NO_BUFFER;
4665 struct kvec rsp_iov;
4666 int rc = 0;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004667 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004668 int flags = 0;
4669
YueHaibingc4985c32020-01-17 10:57:17 +08004670 if (!ses || !(ses->server))
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004671 return -EIO;
4672
4673 if (smb3_encryption_required(tcon))
4674 flags |= CIFS_TRANSFORM_REQ;
4675
4676 memset(&rqst, 0, sizeof(struct smb_rqst));
4677 memset(&iov, 0, sizeof(iov));
4678 rqst.rq_iov = iov;
4679 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4680
4681 rc = SMB2_query_directory_init(xid, tcon, &rqst, persistent_fid,
4682 volatile_fid, index,
4683 srch_inf->info_level);
4684 if (rc)
4685 goto qdir_exit;
4686
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004687 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004688 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004689
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004690 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004691 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004692 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004693 trace_smb3_query_dir_done(xid, persistent_fid,
4694 tcon->tid, tcon->ses->Suid, index, 0);
Pavel Shilovsky52755802014-08-18 20:49:57 +04004695 srch_inf->endOfSearch = true;
4696 rc = 0;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004697 } else {
4698 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4699 tcon->ses->Suid, index, 0, rc);
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08004700 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004701 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004702 goto qdir_exit;
4703 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004704
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004705 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4706 srch_inf);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004707 if (rc) {
4708 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4709 tcon->ses->Suid, index, 0, rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004710 goto qdir_exit;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004711 }
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004712 resp_buftype = CIFS_NO_BUFFER;
4713
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004714 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4715 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004716
4717qdir_exit:
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004718 SMB2_query_directory_free(&rqst);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004719 free_rsp_buf(resp_buftype, rsp);
4720 return rc;
4721}
4722
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004723int
4724SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004725 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004726 u8 info_type, u32 additional_info,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004727 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004728{
4729 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004730 struct kvec *iov = rqst->rq_iov;
4731 unsigned int i, total_len;
4732 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004733
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004734 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004735 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004736 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004737
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004738 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004739 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004740 req->FileInfoClass = info_class;
4741 req->PersistentFileId = persistent_fid;
4742 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004743 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004744
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004745 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004746 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004747 req->BufferLength = cpu_to_le32(*size);
4748
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004749 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004750 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004751
4752 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004753 /* 1 for Buffer */
4754 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004755
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004756 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004757 le32_add_cpu(&req->BufferLength, size[i]);
4758 iov[i].iov_base = (char *)data[i];
4759 iov[i].iov_len = size[i];
4760 }
4761
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004762 return 0;
4763}
4764
4765void
4766SMB2_set_info_free(struct smb_rqst *rqst)
4767{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10004768 if (rqst && rqst->rq_iov)
4769 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004770}
4771
4772static int
4773send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4774 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4775 u8 info_type, u32 additional_info, unsigned int num,
4776 void **data, unsigned int *size)
4777{
4778 struct smb_rqst rqst;
4779 struct smb2_set_info_rsp *rsp = NULL;
4780 struct kvec *iov;
4781 struct kvec rsp_iov;
4782 int rc = 0;
4783 int resp_buftype;
4784 struct cifs_ses *ses = tcon->ses;
4785 int flags = 0;
4786
4787 if (!ses || !(ses->server))
4788 return -EIO;
4789
4790 if (!num)
4791 return -EINVAL;
4792
4793 if (smb3_encryption_required(tcon))
4794 flags |= CIFS_TRANSFORM_REQ;
4795
4796 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4797 if (!iov)
4798 return -ENOMEM;
4799
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004800 memset(&rqst, 0, sizeof(struct smb_rqst));
4801 rqst.rq_iov = iov;
4802 rqst.rq_nvec = num;
4803
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004804 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4805 info_class, info_type, additional_info,
4806 data, size);
4807 if (rc) {
4808 kfree(iov);
4809 return rc;
4810 }
4811
4812
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004813 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004814 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004815 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004816 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004817
Steve Frencheccb4422018-05-17 21:16:55 -05004818 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004819 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004820 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4821 ses->Suid, info_class, (__u32)info_type, rc);
4822 }
Steve French7d3fb242013-11-18 09:56:28 -06004823
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004824 free_rsp_buf(resp_buftype, rsp);
4825 kfree(iov);
4826 return rc;
4827}
4828
4829int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004830SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004831 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004832{
4833 struct smb2_file_eof_info info;
4834 void *data;
4835 unsigned int size;
4836
4837 info.EndOfFile = *eof;
4838
4839 data = &info;
4840 size = sizeof(struct smb2_file_eof_info);
4841
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004842 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004843 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4844 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004845}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004846
4847int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004848SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4849 u64 persistent_fid, u64 volatile_fid,
4850 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4851{
4852 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4853 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4854 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004855}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004856
4857int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004858SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4859 u64 persistent_fid, u64 volatile_fid,
4860 struct smb2_file_full_ea_info *buf, int len)
4861{
4862 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4863 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4864 0, 1, (void **)&buf, &len);
4865}
4866
4867int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004868SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4869 const u64 persistent_fid, const u64 volatile_fid,
4870 __u8 oplock_level)
4871{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004872 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004873 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10004874 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004875 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004876 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004877 unsigned int total_len;
4878 struct kvec iov[1];
4879 struct kvec rsp_iov;
4880 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004881
Joe Perchesf96637b2013-05-04 22:12:25 -05004882 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004883 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4884 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004885 if (rc)
4886 return rc;
4887
Steve French5a77e752018-05-09 17:43:08 -05004888 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004889 flags |= CIFS_TRANSFORM_REQ;
4890
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004891 req->VolatileFid = volatile_fid;
4892 req->PersistentFid = persistent_fid;
4893 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004894 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004895
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004896 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004897
4898 iov[0].iov_base = (char *)req;
4899 iov[0].iov_len = total_len;
4900
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004901 memset(&rqst, 0, sizeof(struct smb_rqst));
4902 rqst.rq_iov = iov;
4903 rqst.rq_nvec = 1;
4904
4905 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004906 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004907
4908 if (rc) {
4909 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004910 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004911 }
4912
4913 return rc;
4914}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004915
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004916void
4917smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4918 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004919{
4920 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4921 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4922 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05304923 kst->f_bfree = kst->f_bavail =
4924 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004925 return;
4926}
4927
Steve French2d304212018-06-24 23:28:12 -05004928static void
4929copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4930 struct kstatfs *kst)
4931{
4932 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4933 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4934 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4935 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4936 kst->f_bavail = kst->f_bfree;
4937 else
4938 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4939 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4940 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4941 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4942 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4943
4944 return;
4945}
Steve French2d304212018-06-24 23:28:12 -05004946
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004947static int
4948build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4949 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4950{
4951 int rc;
4952 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004953 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004954
Joe Perchesf96637b2013-05-04 22:12:25 -05004955 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004956
4957 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4958 return -EIO;
4959
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004960 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4961 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004962 if (rc)
4963 return rc;
4964
4965 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4966 req->FileInfoClass = level;
4967 req->PersistentFileId = persistent_fid;
4968 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004969 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004970 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004971 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004972 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004973 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004974
4975 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004976 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004977 return 0;
4978}
4979
Steve French2d304212018-06-24 23:28:12 -05004980int
4981SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4982 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4983{
4984 struct smb_rqst rqst;
4985 struct smb2_query_info_rsp *rsp = NULL;
4986 struct kvec iov;
4987 struct kvec rsp_iov;
4988 int rc = 0;
4989 int resp_buftype;
4990 struct cifs_ses *ses = tcon->ses;
4991 FILE_SYSTEM_POSIX_INFO *info = NULL;
4992 int flags = 0;
4993
4994 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4995 sizeof(FILE_SYSTEM_POSIX_INFO),
4996 persistent_fid, volatile_fid);
4997 if (rc)
4998 return rc;
4999
5000 if (smb3_encryption_required(tcon))
5001 flags |= CIFS_TRANSFORM_REQ;
5002
5003 memset(&rqst, 0, sizeof(struct smb_rqst));
5004 rqst.rq_iov = &iov;
5005 rqst.rq_nvec = 1;
5006
5007 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
5008 cifs_small_buf_release(iov.iov_base);
5009 if (rc) {
5010 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5011 goto posix_qfsinf_exit;
5012 }
5013 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5014
5015 info = (FILE_SYSTEM_POSIX_INFO *)(
5016 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005017 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5018 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5019 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05005020 if (!rc)
5021 copy_posix_fs_info_to_kstatfs(info, fsdata);
5022
5023posix_qfsinf_exit:
5024 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5025 return rc;
5026}
Steve French2d304212018-06-24 23:28:12 -05005027
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005028int
5029SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5030 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5031{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005032 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005033 struct smb2_query_info_rsp *rsp = NULL;
5034 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005035 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005036 int rc = 0;
5037 int resp_buftype;
5038 struct cifs_ses *ses = tcon->ses;
5039 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005040 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005041
5042 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
5043 sizeof(struct smb2_fs_full_size_info),
5044 persistent_fid, volatile_fid);
5045 if (rc)
5046 return rc;
5047
Steve French5a77e752018-05-09 17:43:08 -05005048 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005049 flags |= CIFS_TRANSFORM_REQ;
5050
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005051 memset(&rqst, 0, sizeof(struct smb_rqst));
5052 rqst.rq_iov = &iov;
5053 rqst.rq_nvec = 1;
5054
5055 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005056 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005057 if (rc) {
5058 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05005059 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005060 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005061 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005062
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005063 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10005064 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005065 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5066 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5067 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005068 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005069 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005070
Steve French34f62642013-10-09 02:07:00 -05005071qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005072 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05005073 return rc;
5074}
5075
5076int
5077SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05005078 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05005079{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005080 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05005081 struct smb2_query_info_rsp *rsp = NULL;
5082 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005083 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05005084 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05005085 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05005086 struct cifs_ses *ses = tcon->ses;
5087 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005088 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05005089
Steven French21671142013-10-09 13:36:35 -05005090 if (level == FS_DEVICE_INFORMATION) {
5091 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5092 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5093 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5094 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5095 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005096 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5097 max_len = sizeof(struct smb3_fs_ss_info);
5098 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05005099 } else if (level == FS_VOLUME_INFORMATION) {
5100 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5101 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05005102 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005103 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05005104 return -EINVAL;
5105 }
5106
5107 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05005108 persistent_fid, volatile_fid);
5109 if (rc)
5110 return rc;
5111
Steve French5a77e752018-05-09 17:43:08 -05005112 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005113 flags |= CIFS_TRANSFORM_REQ;
5114
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005115 memset(&rqst, 0, sizeof(struct smb_rqst));
5116 rqst.rq_iov = &iov;
5117 rqst.rq_nvec = 1;
5118
5119 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005120 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05005121 if (rc) {
5122 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5123 goto qfsattr_exit;
5124 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005125 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05005126
5127 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5128 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005129 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05005130 if (rc)
5131 goto qfsattr_exit;
5132
5133 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005134 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10005135 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05005136 rsp_len, max_len));
5137 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005138 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10005139 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005140 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5141 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005142 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005143 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5144 tcon->perf_sector_size =
5145 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05005146 } else if (level == FS_VOLUME_INFORMATION) {
5147 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5148 (offset + (char *)rsp);
5149 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5150 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005151 }
Steve French34f62642013-10-09 02:07:00 -05005152
5153qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005154 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005155 return rc;
5156}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005157
5158int
5159smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5160 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5161 const __u32 num_lock, struct smb2_lock_element *buf)
5162{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005163 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005164 int rc = 0;
5165 struct smb2_lock_req *req = NULL;
5166 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005167 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005168 int resp_buf_type;
5169 unsigned int count;
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10005170 int flags = CIFS_NO_RSP_BUF;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005171 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005172
Joe Perchesf96637b2013-05-04 22:12:25 -05005173 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005174
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005175 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005176 if (rc)
5177 return rc;
5178
Steve French5a77e752018-05-09 17:43:08 -05005179 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005180 flags |= CIFS_TRANSFORM_REQ;
5181
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005182 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005183 req->LockCount = cpu_to_le16(num_lock);
5184
5185 req->PersistentFileId = persist_fid;
5186 req->VolatileFileId = volatile_fid;
5187
5188 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005189
5190 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005191 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005192 iov[1].iov_base = (char *)buf;
5193 iov[1].iov_len = count;
5194
5195 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005196
5197 memset(&rqst, 0, sizeof(struct smb_rqst));
5198 rqst.rq_iov = iov;
5199 rqst.rq_nvec = 2;
5200
5201 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005202 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005203 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005204 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005205 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005206 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05005207 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5208 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005209 }
5210
5211 return rc;
5212}
5213
5214int
5215SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5216 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5217 const __u64 length, const __u64 offset, const __u32 lock_flags,
5218 const bool wait)
5219{
5220 struct smb2_lock_element lock;
5221
5222 lock.Offset = cpu_to_le64(offset);
5223 lock.Length = cpu_to_le64(length);
5224 lock.Flags = cpu_to_le32(lock_flags);
5225 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5226 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5227
5228 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5229}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005230
5231int
5232SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5233 __u8 *lease_key, const __le32 lease_state)
5234{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005235 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005236 int rc;
5237 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005238 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005239 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005240 unsigned int total_len;
5241 struct kvec iov[1];
5242 struct kvec rsp_iov;
5243 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05005244 __u64 *please_key_high;
5245 __u64 *please_key_low;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005246
Joe Perchesf96637b2013-05-04 22:12:25 -05005247 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005248 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
5249 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005250 if (rc)
5251 return rc;
5252
Steve French5a77e752018-05-09 17:43:08 -05005253 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005254 flags |= CIFS_TRANSFORM_REQ;
5255
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005256 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005257 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005258 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005259
5260 memcpy(req->LeaseKey, lease_key, 16);
5261 req->LeaseState = lease_state;
5262
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10005263 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005264
5265 iov[0].iov_base = (char *)req;
5266 iov[0].iov_len = total_len;
5267
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005268 memset(&rqst, 0, sizeof(struct smb_rqst));
5269 rqst.rq_iov = iov;
5270 rqst.rq_nvec = 1;
5271
5272 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005273 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005274
Aurelien Apteld339adc2019-01-31 13:46:07 +01005275 please_key_low = (__u64 *)lease_key;
5276 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005277 if (rc) {
5278 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05005279 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5280 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05005281 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05005282 } else
5283 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5284 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005285
5286 return rc;
5287}