blob: 1234f9ccab0302b5df63a320f12bf7f397be26c9 [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020036#include <linux/uuid.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070037#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040038#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070048#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070049#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050050#include "cifs_spnego.h"
Long Lidb223a52017-11-22 17:38:45 -070051#include "smbdirect.h"
Steve Frencheccb4422018-05-17 21:16:55 -050052#include "trace.h"
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -020053#ifdef CONFIG_CIFS_DFS_UPCALL
54#include "dfs_cache.h"
55#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040056
57/*
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
60 *
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
63 */
64static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
67 /* SMB2_LOGOFF */ 4,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
70 /* SMB2_CREATE */ 57,
71 /* SMB2_CLOSE */ 24,
72 /* SMB2_FLUSH */ 24,
73 /* SMB2_READ */ 49,
74 /* SMB2_WRITE */ 49,
75 /* SMB2_LOCK */ 48,
76 /* SMB2_IOCTL */ 57,
77 /* SMB2_CANCEL */ 4,
78 /* SMB2_ECHO */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
84};
85
Ronnie Sahlberg730928c2018-08-08 15:07:49 +100086int smb3_encryption_required(const struct cifs_tcon *tcon)
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070087{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080088 if (!tcon)
89 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070090 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
91 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
92 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080093 if (tcon->seal &&
94 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
95 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070096 return 0;
97}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040098
99static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700100smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400101 const struct cifs_tcon *tcon)
102{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700103 shdr->ProtocolId = SMB2_PROTO_NUMBER;
104 shdr->StructureSize = cpu_to_le16(64);
105 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
108
109 spin_lock(&server->req_lock);
Steve French69dc4b12019-03-05 21:04:56 -0600110 /* Request up to 10 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500111 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700112 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100113 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700114 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500115 min_t(int, server->max_credits -
Steve French69dc4b12019-03-05 21:04:56 -0600116 server->credits, 10));
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100117 spin_unlock(&server->req_lock);
118 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700119 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100120 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400122
123 if (!tcon)
124 goto out;
125
Steve French2b80d042013-06-23 18:43:37 -0500126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500128 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700130 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500131 /* else CreditCharge MBZ */
132
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700133 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400134 /* Uid is not converted */
135 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700136 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500137
138 /*
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
147 */
148/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500150
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
Steve French5a77e752018-05-09 17:43:08 -0500152 !smb3_encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700153 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400154out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400155 return;
156}
157
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200158#ifdef CONFIG_CIFS_DFS_UPCALL
159static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
161{
162 int rc;
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100165 char *tree;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200166 const char *tcp_host;
167 size_t tcp_host_len;
168 const char *dfs_host;
169 size_t dfs_host_len;
170
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
172 if (!tree)
173 return -ENOMEM;
174
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200175 if (tcon->ipc) {
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000176 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
179 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200180 }
181
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
184 goto out;
185 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200186
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
188 if (rc)
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100189 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200190
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
192 &tcp_host_len);
193
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
196 const char *tgt = dfs_cache_get_tgt_name(it);
197
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
199
200 if (dfs_host_len != tcp_host_len
201 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
202 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
203 __func__,
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
206 continue;
207 }
208
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000209 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200210
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
212 if (!rc)
213 break;
214 if (rc == -EREMOTE)
215 break;
216 }
217
218 if (!rc) {
219 if (it)
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
221 it);
222 else
223 rc = -ENOENT;
224 }
225 dfs_cache_free_tgts(&tl);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100226out:
227 kfree(tree);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200228 return rc;
229}
230#else
231static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
233{
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
235}
236#endif
237
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400238static int
239smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
240{
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300241 int rc;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200245 int retries;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400246
247 /*
248 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
249 * check for tcp and smb session status done differently
250 * for those three - in the calling routine.
251 */
252 if (tcon == NULL)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300253 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400254
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300255 if (smb2_command == SMB2_TREE_CONNECT)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300256 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400257
258 if (tcon->tidStatus == CifsExiting) {
259 /*
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
263 */
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
268 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400269 return -ENODEV;
270 }
271 }
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
274 return -EIO;
275
276 ses = tcon->ses;
277 server = ses->server;
278
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200279 retries = server->nr_targets;
280
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400281 /*
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200282 * Give demultiplex thread up to 10 seconds to each target available for
283 * reconnect -- should be greater than cifs socket timeout which is 7
284 * seconds.
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400285 */
286 while (server->tcpStatus == CifsNeedReconnect) {
287 /*
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
290 */
291 switch (smb2_command) {
292 /*
293 * BB Should we keep oplock break and add flush to exceptions?
294 */
295 case SMB2_TREE_DISCONNECT:
296 case SMB2_CANCEL:
297 case SMB2_CLOSE:
298 case SMB2_OPLOCK_BREAK:
299 return -EAGAIN;
300 }
301
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
304 10 * HZ);
305 if (rc < 0) {
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
308 return -ERESTARTSYS;
309 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400310
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
313 break;
314
Ronnie Sahlbergc54849d2020-01-31 05:52:51 +1000315 if (retries && --retries)
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200316 continue;
317
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400318 /*
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
321 * back on-line
322 */
323 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400325 return -EHOSTDOWN;
326 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200327 retries = server->nr_targets;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400328 }
329
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300331 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400332
333 nls_codepage = load_nls_default();
334
335 /*
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
338 */
339 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200340
341 /*
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
345 */
346 if (server->tcpStatus == CifsNeedReconnect) {
347 rc = -EHOSTDOWN;
348 mutex_unlock(&tcon->ses->session_mutex);
349 goto out;
350 }
351
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400352 rc = cifs_negotiate_protocol(0, tcon->ses);
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000353 if (!rc && tcon->ses->need_reconnect) {
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000355 if ((rc == -EACCES) && !tcon->retry) {
356 rc = -EHOSTDOWN;
357 mutex_unlock(&tcon->ses->session_mutex);
358 goto failed;
359 }
360 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400361 if (rc || !tcon->need_reconnect) {
362 mutex_unlock(&tcon->ses->session_mutex);
363 goto out;
364 }
365
366 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800367 if (tcon->use_persistent)
368 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500369
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200370 rc = __smb2_reconnect(nls_codepage, tcon);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400371 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500372
Joe Perchesf96637b2013-05-04 22:12:25 -0500373 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500374 if (rc) {
375 /* If sess reconnected but tcon didn't, something strange ... */
376 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400377 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500378 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800379
380 if (smb2_command != SMB2_INTERNAL_CMD)
381 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
382
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400383 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400384out:
385 /*
386 * Check if handle based operation so we know whether we can continue
387 * or not without returning to caller to reset file handle.
388 */
389 /*
390 * BB Is flush done by server on drop of tcp session? Should we special
391 * case it and skip above?
392 */
393 switch (smb2_command) {
394 case SMB2_FLUSH:
395 case SMB2_READ:
396 case SMB2_WRITE:
397 case SMB2_LOCK:
398 case SMB2_IOCTL:
399 case SMB2_QUERY_DIRECTORY:
400 case SMB2_CHANGE_NOTIFY:
401 case SMB2_QUERY_INFO:
402 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800403 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400404 }
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000405failed:
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400406 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400407 return rc;
408}
409
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700410static void
411fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
412 unsigned int *total_len)
413{
414 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
415 /* lookup word count ie StructureSize from table */
416 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
417
418 /*
419 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
420 * largest operations (Create)
421 */
422 memset(buf, 0, 256);
423
424 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
425 spdu->StructureSize2 = cpu_to_le16(parmsize);
426
427 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
428}
429
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100430/*
431 * Allocate and return pointer to an SMB request hdr, and set basic
432 * SMB information in the SMB header. If the return code is zero, this
433 * function must have filled in request_buf pointer.
434 */
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300435static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
436 void **request_buf, unsigned int *total_len)
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800437{
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800438 /* BB eventually switch this to SMB2 specific small buf size */
Stefano Briviof46ecbd2018-07-05 11:46:42 +0200439 if (smb2_command == SMB2_SET_INFO)
440 *request_buf = cifs_buf_get();
441 else
442 *request_buf = cifs_small_buf_get();
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800443 if (*request_buf == NULL) {
444 /* BB should we add a retry in here if not a writepage? */
445 return -ENOMEM;
446 }
447
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100448 fill_small_buf(smb2_command, tcon,
449 (struct smb2_sync_hdr *)(*request_buf),
450 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400451
452 if (tcon != NULL) {
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400453 uint16_t com_code = le16_to_cpu(smb2_command);
454 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400455 cifs_stats_inc(&tcon->num_smbs_sent);
456 }
457
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300458 return 0;
459}
460
461static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
462 void **request_buf, unsigned int *total_len)
463{
464 int rc;
465
466 rc = smb2_reconnect(smb2_command, tcon);
467 if (rc)
468 return rc;
469
470 return __smb2_plain_req_init(smb2_command, tcon, request_buf,
471 total_len);
472}
473
474static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
475 void **request_buf, unsigned int *total_len)
476{
477 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
478 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
479 return __smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf,
480 total_len);
481 }
482 return smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf, total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400483}
484
Steve Frenchd7bef4c2019-04-18 11:03:58 -0500485/* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500486
487static void
488build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
489{
490 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
491 pneg_ctxt->DataLength = cpu_to_le16(38);
492 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
493 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
494 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
495 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
496}
497
498static void
Steve French26ea8882019-04-26 20:36:08 -0700499build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
500{
501 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
502 pneg_ctxt->DataLength =
503 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
504 - sizeof(struct smb2_neg_context));
505 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
506 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
507 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
508 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
509}
510
511static void
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500512build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
513{
514 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French9ac63ec2019-06-07 08:59:40 -0500515 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
516 pneg_ctxt->CipherCount = cpu_to_le16(2);
517 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
518 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500519}
520
Steve French96d3cca2019-06-25 04:39:51 -0500521static unsigned int
522build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
523{
524 struct nls_table *cp = load_nls_default();
525
526 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
527
528 /* copy up to max of first 100 bytes of server name to NetName field */
Steve Frenchdf58fae2019-08-05 17:07:26 -0500529 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
Steve French96d3cca2019-06-25 04:39:51 -0500530 /* context size is DataLength + minimal smb2_neg_context */
531 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
532 sizeof(struct smb2_neg_context), 8) * 8;
533}
534
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500535static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500536build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
537{
538 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
539 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
Steve French0d481322019-02-24 17:56:33 -0600540 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
541 pneg_ctxt->Name[0] = 0x93;
542 pneg_ctxt->Name[1] = 0xAD;
543 pneg_ctxt->Name[2] = 0x25;
544 pneg_ctxt->Name[3] = 0x50;
545 pneg_ctxt->Name[4] = 0x9C;
546 pneg_ctxt->Name[5] = 0xB4;
547 pneg_ctxt->Name[6] = 0x11;
548 pneg_ctxt->Name[7] = 0xE7;
549 pneg_ctxt->Name[8] = 0xB4;
550 pneg_ctxt->Name[9] = 0x23;
551 pneg_ctxt->Name[10] = 0x83;
552 pneg_ctxt->Name[11] = 0xDE;
553 pneg_ctxt->Name[12] = 0x96;
554 pneg_ctxt->Name[13] = 0x8B;
555 pneg_ctxt->Name[14] = 0xCD;
556 pneg_ctxt->Name[15] = 0x7C;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500557}
558
559static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100560assemble_neg_contexts(struct smb2_negotiate_req *req,
Steve French9fe5ff12019-06-24 20:39:04 -0500561 struct TCP_Server_Info *server, unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500562{
Colin Ian Kinga9f76cf2019-12-02 18:59:42 +0000563 char *pneg_ctxt;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500564 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500565
Steve Frenchd5c70762019-01-03 02:37:21 -0600566 if (*total_len > 200) {
567 /* In case length corrupted don't want to overrun smb buffer */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000568 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
Steve Frenchd5c70762019-01-03 02:37:21 -0600569 return;
570 }
571
572 /*
573 * round up total_len of fixed part of SMB3 negotiate request to 8
574 * byte boundary before adding negotiate contexts
575 */
576 *total_len = roundup(*total_len, 8);
577
578 pneg_ctxt = (*total_len) + (char *)req;
579 req->NegotiateContextOffset = cpu_to_le32(*total_len);
580
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500581 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500582 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
583 *total_len += ctxt_len;
584 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100585
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500586 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500587 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
588 *total_len += ctxt_len;
589 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100590
Steve French9fe5ff12019-06-24 20:39:04 -0500591 if (server->compress_algorithm) {
592 build_compression_ctxt((struct smb2_compression_capabilities_context *)
Steve French26ea8882019-04-26 20:36:08 -0700593 pneg_ctxt);
Steve French9fe5ff12019-06-24 20:39:04 -0500594 ctxt_len = DIV_ROUND_UP(
595 sizeof(struct smb2_compression_capabilities_context),
596 8) * 8;
597 *total_len += ctxt_len;
598 pneg_ctxt += ctxt_len;
Steve French96d3cca2019-06-25 04:39:51 -0500599 req->NegotiateContextCount = cpu_to_le16(5);
Steve French9fe5ff12019-06-24 20:39:04 -0500600 } else
Steve French96d3cca2019-06-25 04:39:51 -0500601 req->NegotiateContextCount = cpu_to_le16(4);
602
603 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
604 server->hostname);
605 *total_len += ctxt_len;
606 pneg_ctxt += ctxt_len;
607
Steve Frenchfcef0db2018-05-19 20:45:27 -0500608 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
609 *total_len += sizeof(struct smb2_posix_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500610}
Steve French5100d8a2018-04-09 10:47:14 -0500611
612static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
613{
614 unsigned int len = le16_to_cpu(ctxt->DataLength);
615
616 /* If invalid preauth context warn but use what we requested, SHA-512 */
617 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
618 printk_once(KERN_WARNING "server sent bad preauth context\n");
619 return;
620 }
621 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
622 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
623 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
624 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
625}
626
Steve French26ea8882019-04-26 20:36:08 -0700627static void decode_compress_ctx(struct TCP_Server_Info *server,
628 struct smb2_compression_capabilities_context *ctxt)
629{
630 unsigned int len = le16_to_cpu(ctxt->DataLength);
631
632 /* sizeof compress context is a one element compression capbility struct */
633 if (len < 10) {
634 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
635 return;
636 }
637 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
638 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
639 return;
640 }
641 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
642 printk_once(KERN_WARNING "unknown compression algorithm\n");
643 return;
644 }
645 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
646}
647
Steve French5100d8a2018-04-09 10:47:14 -0500648static int decode_encrypt_ctx(struct TCP_Server_Info *server,
649 struct smb2_encryption_neg_context *ctxt)
650{
651 unsigned int len = le16_to_cpu(ctxt->DataLength);
652
653 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
654 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
655 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
656 return -EINVAL;
657 }
658
659 if (le16_to_cpu(ctxt->CipherCount) != 1) {
660 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
661 return -EINVAL;
662 }
663 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
664 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
665 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
666 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
667 return -EINVAL;
668 }
669 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500670 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500671 return 0;
672}
673
674static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000675 struct TCP_Server_Info *server,
676 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500677{
678 struct smb2_neg_context *pctx;
679 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
680 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500681 unsigned int len_of_ctxts, i;
682 int rc = 0;
683
684 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
685 if (len_of_smb <= offset) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000686 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
Steve French5100d8a2018-04-09 10:47:14 -0500687 return -EINVAL;
688 }
689
690 len_of_ctxts = len_of_smb - offset;
691
692 for (i = 0; i < ctxt_cnt; i++) {
693 int clen;
694 /* check that offset is not beyond end of SMB */
695 if (len_of_ctxts == 0)
696 break;
697
698 if (len_of_ctxts < sizeof(struct smb2_neg_context))
699 break;
700
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000701 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500702 clen = le16_to_cpu(pctx->DataLength);
703 if (clen > len_of_ctxts)
704 break;
705
706 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
707 decode_preauth_context(
708 (struct smb2_preauth_neg_context *)pctx);
709 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
710 rc = decode_encrypt_ctx(server,
711 (struct smb2_encryption_neg_context *)pctx);
Steve French26ea8882019-04-26 20:36:08 -0700712 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
713 decode_compress_ctx(server,
714 (struct smb2_compression_capabilities_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500715 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
716 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500717 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000718 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
Steve French5100d8a2018-04-09 10:47:14 -0500719 le16_to_cpu(pctx->ContextType));
720
721 if (rc)
722 break;
723 /* offsets must be 8 byte aligned */
724 clen = (clen + 7) & ~0x7;
725 offset += clen + sizeof(struct smb2_neg_context);
726 len_of_ctxts -= clen;
727 }
728 return rc;
729}
730
Steve Frenchce558b02018-05-31 19:16:54 -0500731static struct create_posix *
732create_posix_buf(umode_t mode)
733{
734 struct create_posix *buf;
735
736 buf = kzalloc(sizeof(struct create_posix),
737 GFP_KERNEL);
738 if (!buf)
739 return NULL;
740
741 buf->ccontext.DataOffset =
742 cpu_to_le16(offsetof(struct create_posix, Mode));
743 buf->ccontext.DataLength = cpu_to_le32(4);
744 buf->ccontext.NameOffset =
745 cpu_to_le16(offsetof(struct create_posix, Name));
746 buf->ccontext.NameLength = cpu_to_le16(16);
747
748 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
749 buf->Name[0] = 0x93;
750 buf->Name[1] = 0xAD;
751 buf->Name[2] = 0x25;
752 buf->Name[3] = 0x50;
753 buf->Name[4] = 0x9C;
754 buf->Name[5] = 0xB4;
755 buf->Name[6] = 0x11;
756 buf->Name[7] = 0xE7;
757 buf->Name[8] = 0xB4;
758 buf->Name[9] = 0x23;
759 buf->Name[10] = 0x83;
760 buf->Name[11] = 0xDE;
761 buf->Name[12] = 0x96;
762 buf->Name[13] = 0x8B;
763 buf->Name[14] = 0xCD;
764 buf->Name[15] = 0x7C;
765 buf->Mode = cpu_to_le32(mode);
766 cifs_dbg(FYI, "mode on posix create 0%o", mode);
767 return buf;
768}
769
770static int
771add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
772{
773 struct smb2_create_req *req = iov[0].iov_base;
774 unsigned int num = *num_iovec;
775
776 iov[num].iov_base = create_posix_buf(mode);
Steve Frenchd0959b02019-10-05 10:53:58 -0500777 if (mode == ACL_NO_MODE)
778 cifs_dbg(FYI, "illegal mode\n");
Steve Frenchce558b02018-05-31 19:16:54 -0500779 if (iov[num].iov_base == NULL)
780 return -ENOMEM;
781 iov[num].iov_len = sizeof(struct create_posix);
782 if (!req->CreateContextsOffset)
783 req->CreateContextsOffset = cpu_to_le32(
784 sizeof(struct smb2_create_req) +
785 iov[num - 1].iov_len);
786 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
787 *num_iovec = num + 1;
788 return 0;
789}
790
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500791
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400792/*
793 *
794 * SMB2 Worker functions follow:
795 *
796 * The general structure of the worker functions is:
797 * 1) Call smb2_init (assembles SMB2 header)
798 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
799 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
800 * 4) Decode SMB2 command specific fields in the fixed length area
801 * 5) Decode variable length data area (if any for this SMB2 command type)
802 * 6) Call free smb buffer
803 * 7) return
804 *
805 */
806
807int
808SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
809{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000810 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400811 struct smb2_negotiate_req *req;
812 struct smb2_negotiate_rsp *rsp;
813 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700814 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400815 int rc = 0;
816 int resp_buftype;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200817 struct TCP_Server_Info *server = cifs_ses_server(ses);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400818 int blob_offset, blob_length;
819 char *security_blob;
820 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100821 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400822
Joe Perchesf96637b2013-05-04 22:12:25 -0500823 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400824
Jeff Layton3534b852013-05-24 07:41:01 -0400825 if (!server) {
826 WARN(1, "%s: server is NULL!\n", __func__);
827 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400828 }
829
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100830 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400831 if (rc)
832 return rc;
833
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100834 req->sync_hdr.SessionId = 0;
Steve French0fdfef92018-06-28 19:30:23 -0500835
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100836 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
837 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400838
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200839 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500840 SMB3ANY_VERSION_STRING) == 0) {
841 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
842 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
843 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100844 total_len += 4;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000845 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500846 SMBDEFAULT_VERSION_STRING) == 0) {
847 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
848 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
849 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600850 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
851 req->DialectCount = cpu_to_le16(4);
852 total_len += 8;
Steve French9764c022017-09-17 10:41:35 -0500853 } else {
854 /* otherwise send specific dialect */
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200855 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
Steve French9764c022017-09-17 10:41:35 -0500856 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100857 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500858 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400859
860 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400861 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500862 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400863 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500864 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400865 else
866 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400867
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000868 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400869
Steve French3c5f9be12014-05-13 13:37:45 -0700870 /* ClientGUID must be zero for SMB2.02 dialect */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000871 if (server->vals->protocol_id == SMB20_PROT_ID)
Steve French3c5f9be12014-05-13 13:37:45 -0700872 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500873 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700874 memcpy(req->ClientGUID, server->client_guid,
875 SMB2_CLIENT_GUID_SIZE);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000876 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
877 (strcmp(server->vals->version_string,
Steve Frenchd5c70762019-01-03 02:37:21 -0600878 SMBDEFAULT_VERSION_STRING) == 0))
Steve French9fe5ff12019-06-24 20:39:04 -0500879 assemble_neg_contexts(req, server, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500880 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400881 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100882 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400883
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000884 memset(&rqst, 0, sizeof(struct smb_rqst));
885 rqst.rq_iov = iov;
886 rqst.rq_nvec = 1;
887
888 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700889 cifs_small_buf_release(req);
890 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400891 /*
892 * No tcon so can't do
893 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
894 */
Steve French7e682f72017-08-31 21:34:24 -0500895 if (rc == -EOPNOTSUPP) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000896 cifs_server_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500897 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500898 " older servers\n");
899 goto neg_exit;
900 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400901 goto neg_exit;
902
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000903 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500904 SMB3ANY_VERSION_STRING) == 0) {
905 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000906 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500907 "SMB2 dialect returned but not requested\n");
908 return -EIO;
909 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000910 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500911 "SMB2.1 dialect returned but not requested\n");
912 return -EIO;
913 }
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000914 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500915 SMBDEFAULT_VERSION_STRING) == 0) {
916 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000917 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500918 "SMB2 dialect returned but not requested\n");
919 return -EIO;
920 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
921 /* ops set to 3.0 by default for default so update */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000922 server->ops = &smb21_operations;
923 server->vals = &smb21_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800924 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000925 server->ops = &smb311_operations;
926 server->vals = &smb311_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800927 }
Steve French590d08d2017-09-19 11:43:47 -0500928 } else if (le16_to_cpu(rsp->DialectRevision) !=
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000929 server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500930 /* if requested single dialect ensure returned dialect matched */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000931 cifs_server_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500932 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500933 return -EIO;
934 }
935
Joe Perchesf96637b2013-05-04 22:12:25 -0500936 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400937
Steve Frenche4aa25e2012-10-01 12:26:22 -0500938 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500939 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500940 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500941 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500942 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500943 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500944 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
945 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600946 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
947 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400948 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000949 cifs_server_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500950 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400951 rc = -EIO;
952 goto neg_exit;
953 }
954 server->dialect = le16_to_cpu(rsp->DialectRevision);
955
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100956 /*
957 * Keep a copy of the hash after negprot. This hash will be
958 * the starting hash value for all sessions made from this
959 * server.
960 */
961 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
962 SMB2_PREAUTH_HASH_SIZE);
Steve French0fdfef92018-06-28 19:30:23 -0500963
Jeff Laytone598d1d82013-05-26 07:00:59 -0400964 /* SMB2 only has an extended negflavor */
965 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400966 /* set it to the maximum buffer size value we can send with 1 credit */
967 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
968 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400969 server->max_read = le32_to_cpu(rsp->MaxReadSize);
970 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400971 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500972 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
973 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
974 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400975 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400976 /* Internal types */
977 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400978
979 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000980 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500981 /*
982 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
983 * for us will be
984 * ses->sectype = RawNTLMSSP;
985 * but for time being this is our only auth choice so doesn't matter.
986 * We just found a server which sets blob length to zero expecting raw.
987 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700988 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500989 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700990 server->sec_ntlmssp = true;
991 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700992
Jeff Layton38d77c52013-05-26 07:01:00 -0400993 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400994 if (rc)
995 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500996 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500997 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500998 if (rc == 1)
999 rc = 0;
1000 else if (rc == 0)
1001 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001002 }
Steve French5100d8a2018-04-09 10:47:14 -05001003
Steve French5100d8a2018-04-09 10:47:14 -05001004 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1005 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10001006 rc = smb311_decode_neg_context(rsp, server,
1007 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -05001008 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001009 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
Steve French5100d8a2018-04-09 10:47:14 -05001010 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001011neg_exit:
1012 free_rsp_buf(resp_buftype, rsp);
1013 return rc;
1014}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001015
Steve Frenchff1c0382013-11-19 23:44:46 -06001016int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1017{
Long Li2796d302018-04-25 11:30:04 -07001018 int rc;
1019 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001020 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -06001021 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -05001022 u32 inbuflen; /* max of 4 dialects */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001023 struct TCP_Server_Info *server = tcon->ses->server;
Steve Frenchff1c0382013-11-19 23:44:46 -06001024
1025 cifs_dbg(FYI, "validate negotiate\n");
1026
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001027 /* In SMB3.11 preauth integrity supersedes validate negotiate */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001028 if (server->dialect == SMB311_PROT_ID)
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001029 return 0;
1030
Steve Frenchff1c0382013-11-19 23:44:46 -06001031 /*
1032 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -05001033 * can not sign it (ie are not known user). Even if signing is not
1034 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -06001035 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -05001036 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -06001037 */
Steve French0603c962017-09-20 19:57:18 -05001038 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -06001039 return 0; /* validation requires signing */
1040
Steve French0603c962017-09-20 19:57:18 -05001041 if (tcon->ses->user_name == NULL) {
1042 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1043 return 0; /* validation requires signing */
1044 }
1045
1046 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001047 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
Steve French0603c962017-09-20 19:57:18 -05001048
Long Li2796d302018-04-25 11:30:04 -07001049 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1050 if (!pneg_inbuf)
1051 return -ENOMEM;
1052
1053 pneg_inbuf->Capabilities =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001054 cpu_to_le32(server->vals->req_capabilities);
1055 memcpy(pneg_inbuf->Guid, server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +01001056 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -06001057
1058 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -07001059 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001060 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1061 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -07001062 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001063 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1064 else
Long Li2796d302018-04-25 11:30:04 -07001065 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001066
Steve French9764c022017-09-17 10:41:35 -05001067
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001068 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001069 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001070 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1071 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1072 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -05001073 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -07001074 inbuflen = sizeof(*pneg_inbuf) -
Steve Frenchd5c70762019-01-03 02:37:21 -06001075 (2 * sizeof(pneg_inbuf->Dialects[0]));
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001076 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001077 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001078 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1079 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1080 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -06001081 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1082 pneg_inbuf->DialectCount = cpu_to_le16(4);
Steve French9764c022017-09-17 10:41:35 -05001083 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -07001084 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -05001085 } else {
1086 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -07001087 pneg_inbuf->Dialects[0] =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001088 cpu_to_le16(server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -07001089 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -05001090 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -07001091 inbuflen = sizeof(*pneg_inbuf) -
1092 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -05001093 }
Steve Frenchff1c0382013-11-19 23:44:46 -06001094
1095 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1096 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001097 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1098 (char **)&pneg_rsp, &rsplen);
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001099 if (rc == -EOPNOTSUPP) {
1100 /*
1101 * Old Windows versions or Netapp SMB server can return
1102 * not supported error. Client should accept it.
1103 */
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001104 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
Colin Ian King21078202019-05-17 09:12:33 +01001105 rc = 0;
1106 goto out_free_inbuf;
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001107 } else if (rc != 0) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001108 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -07001109 rc = -EIO;
1110 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -06001111 }
1112
Long Li2796d302018-04-25 11:30:04 -07001113 rc = -EIO;
1114 if (rsplen != sizeof(*pneg_rsp)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001115 cifs_tcon_dbg(VFS, "invalid protocol negotiate response size: %d\n",
Steve French7db0a6e2017-05-03 21:12:20 -05001116 rsplen);
1117
1118 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -07001119 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1120 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001121 }
1122
1123 /* check validate negotiate info response matches what we got earlier */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001124 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -06001125 goto vneg_out;
1126
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001127 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
Steve Frenchff1c0382013-11-19 23:44:46 -06001128 goto vneg_out;
1129
1130 /* do not validate server guid because not saved at negprot time yet */
1131
1132 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001133 SMB2_LARGE_FILES) != server->capabilities)
Steve Frenchff1c0382013-11-19 23:44:46 -06001134 goto vneg_out;
1135
1136 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -07001137 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001138 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -07001139 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001140
1141vneg_out:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001142 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -07001143out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001144 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -07001145out_free_inbuf:
1146 kfree(pneg_inbuf);
1147 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -06001148}
1149
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301150enum securityEnum
1151smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1152{
1153 switch (requested) {
1154 case Kerberos:
1155 case RawNTLMSSP:
1156 return requested;
1157 case NTLMv2:
1158 return RawNTLMSSP;
1159 case Unspecified:
1160 if (server->sec_ntlmssp &&
1161 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1162 return RawNTLMSSP;
1163 if ((server->sec_kerberos || server->sec_mskerberos) &&
1164 (global_secflags & CIFSSEC_MAY_KRB5))
1165 return Kerberos;
1166 /* Fallthrough */
1167 default:
1168 return Unspecified;
1169 }
1170}
1171
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001172struct SMB2_sess_data {
1173 unsigned int xid;
1174 struct cifs_ses *ses;
1175 struct nls_table *nls_cp;
1176 void (*func)(struct SMB2_sess_data *);
1177 int result;
1178 u64 previous_session;
1179
1180 /* we will send the SMB in three pieces:
1181 * a fixed length beginning part, an optional
1182 * SPNEGO blob (which can be zero length), and a
1183 * last part which will include the strings
1184 * and rest of bcc area. This allows us to avoid
1185 * a large buffer 17K allocation
1186 */
1187 int buf0_type;
1188 struct kvec iov[2];
1189};
1190
1191static int
1192SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1193{
1194 int rc;
1195 struct cifs_ses *ses = sess_data->ses;
1196 struct smb2_sess_setup_req *req;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001197 struct TCP_Server_Info *server = cifs_ses_server(ses);
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001198 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001199
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001200 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1201 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001202 if (rc)
1203 return rc;
1204
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001205 if (sess_data->ses->binding) {
1206 req->sync_hdr.SessionId = sess_data->ses->Suid;
1207 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1208 req->PreviousSessionId = 0;
1209 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1210 } else {
1211 /* First session, not a reauthenticate */
1212 req->sync_hdr.SessionId = 0;
1213 /*
1214 * if reconnect, we need to send previous sess id
1215 * otherwise it is 0
1216 */
1217 req->PreviousSessionId = sess_data->previous_session;
1218 req->Flags = 0; /* MBZ */
1219 }
Steve Frenchd4090142018-06-13 17:05:58 -05001220
1221 /* enough to enable echos and oplocks and one max size write */
1222 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001223
1224 /* only one of SMB2 signing flags may be set in SMB2 request */
1225 if (server->sign)
1226 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1227 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1228 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1229 else
1230 req->SecurityMode = 0;
1231
Steve French8d330962019-07-25 18:13:10 -05001232#ifdef CONFIG_CIFS_DFS_UPCALL
1233 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1234#else
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001235 req->Capabilities = 0;
Steve French8d330962019-07-25 18:13:10 -05001236#endif /* DFS_UPCALL */
1237
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001238 req->Channel = 0; /* MBZ */
1239
1240 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001241 /* 1 for pad */
1242 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001243 /*
1244 * This variable will be used to clear the buffer
1245 * allocated above in case of any error in the calling function.
1246 */
1247 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1248
1249 return 0;
1250}
1251
1252static void
1253SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1254{
1255 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1256 sess_data->buf0_type = CIFS_NO_BUFFER;
1257}
1258
1259static int
1260SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1261{
1262 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001263 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001264 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001265 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001266
1267 /* Testing shows that buffer offset must be at location of Buffer[0] */
1268 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001269 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001270 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1271
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001272 memset(&rqst, 0, sizeof(struct smb_rqst));
1273 rqst.rq_iov = sess_data->iov;
1274 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001275
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001276 /* BB add code to build os and lm fields */
1277 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1278 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001279 &sess_data->buf0_type,
1280 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001281 cifs_small_buf_release(sess_data->iov[0].iov_base);
1282 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001283
1284 return rc;
1285}
1286
1287static int
1288SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1289{
1290 int rc = 0;
1291 struct cifs_ses *ses = sess_data->ses;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001292 struct TCP_Server_Info *server = cifs_ses_server(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001293
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001294 mutex_lock(&server->srv_mutex);
1295 if (server->ops->generate_signingkey) {
1296 rc = server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001297 if (rc) {
1298 cifs_dbg(FYI,
1299 "SMB3 session key generation failed\n");
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001300 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001301 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001302 }
1303 }
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001304 if (!server->session_estab) {
1305 server->sequence_number = 0x2;
1306 server->session_estab = true;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001307 }
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001308 mutex_unlock(&server->srv_mutex);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001309
1310 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001311 /* keep existing ses state if binding */
1312 if (!ses->binding) {
1313 spin_lock(&GlobalMid_Lock);
1314 ses->status = CifsGood;
1315 ses->need_reconnect = false;
1316 spin_unlock(&GlobalMid_Lock);
1317 }
1318
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001319 return rc;
1320}
1321
1322#ifdef CONFIG_CIFS_UPCALL
1323static void
1324SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1325{
1326 int rc;
1327 struct cifs_ses *ses = sess_data->ses;
1328 struct cifs_spnego_msg *msg;
1329 struct key *spnego_key = NULL;
1330 struct smb2_sess_setup_rsp *rsp = NULL;
1331
1332 rc = SMB2_sess_alloc_buffer(sess_data);
1333 if (rc)
1334 goto out;
1335
1336 spnego_key = cifs_get_spnego_key(ses);
1337 if (IS_ERR(spnego_key)) {
1338 rc = PTR_ERR(spnego_key);
1339 spnego_key = NULL;
1340 goto out;
1341 }
1342
1343 msg = spnego_key->payload.data[0];
1344 /*
1345 * check version field to make sure that cifs.upcall is
1346 * sending us a response in an expected form
1347 */
1348 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1349 cifs_dbg(VFS,
1350 "bad cifs.upcall version. Expected %d got %d",
1351 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1352 rc = -EKEYREJECTED;
1353 goto out_put_spnego_key;
1354 }
1355
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001356 /* keep session key if binding */
1357 if (!ses->binding) {
1358 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1359 GFP_KERNEL);
1360 if (!ses->auth_key.response) {
1361 cifs_dbg(VFS,
1362 "Kerberos can't allocate (%u bytes) memory",
1363 msg->sesskey_len);
1364 rc = -ENOMEM;
1365 goto out_put_spnego_key;
1366 }
1367 ses->auth_key.len = msg->sesskey_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001368 }
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001369
1370 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1371 sess_data->iov[1].iov_len = msg->secblob_len;
1372
1373 rc = SMB2_sess_sendreceive(sess_data);
1374 if (rc)
1375 goto out_put_spnego_key;
1376
1377 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001378 /* keep session id and flags if binding */
1379 if (!ses->binding) {
1380 ses->Suid = rsp->sync_hdr.SessionId;
1381 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1382 }
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001383
1384 rc = SMB2_sess_establish_session(sess_data);
1385out_put_spnego_key:
1386 key_invalidate(spnego_key);
1387 key_put(spnego_key);
1388out:
1389 sess_data->result = rc;
1390 sess_data->func = NULL;
1391 SMB2_sess_free_buffer(sess_data);
1392}
1393#else
1394static void
1395SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1396{
1397 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1398 sess_data->result = -EOPNOTSUPP;
1399 sess_data->func = NULL;
1400}
1401#endif
1402
Sachin Prabhu166cea42016-10-07 19:11:22 +01001403static void
1404SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1405
1406static void
1407SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1408{
1409 int rc;
1410 struct cifs_ses *ses = sess_data->ses;
1411 struct smb2_sess_setup_rsp *rsp = NULL;
1412 char *ntlmssp_blob = NULL;
1413 bool use_spnego = false; /* else use raw ntlmssp */
1414 u16 blob_length = 0;
1415
1416 /*
1417 * If memory allocation is successful, caller of this function
1418 * frees it.
1419 */
1420 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1421 if (!ses->ntlmssp) {
1422 rc = -ENOMEM;
1423 goto out_err;
1424 }
1425 ses->ntlmssp->sesskey_per_smbsess = true;
1426
1427 rc = SMB2_sess_alloc_buffer(sess_data);
1428 if (rc)
1429 goto out_err;
1430
1431 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1432 GFP_KERNEL);
1433 if (ntlmssp_blob == NULL) {
1434 rc = -ENOMEM;
1435 goto out;
1436 }
1437
1438 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1439 if (use_spnego) {
1440 /* BB eventually need to add this */
1441 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1442 rc = -EOPNOTSUPP;
1443 goto out;
1444 } else {
1445 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1446 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1447 }
1448 sess_data->iov[1].iov_base = ntlmssp_blob;
1449 sess_data->iov[1].iov_len = blob_length;
1450
1451 rc = SMB2_sess_sendreceive(sess_data);
1452 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1453
1454 /* If true, rc here is expected and not an error */
1455 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001456 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001457 rc = 0;
1458
1459 if (rc)
1460 goto out;
1461
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001462 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001463 le16_to_cpu(rsp->SecurityBufferOffset)) {
1464 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1465 le16_to_cpu(rsp->SecurityBufferOffset));
1466 rc = -EIO;
1467 goto out;
1468 }
1469 rc = decode_ntlmssp_challenge(rsp->Buffer,
1470 le16_to_cpu(rsp->SecurityBufferLength), ses);
1471 if (rc)
1472 goto out;
1473
1474 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1475
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001476 /* keep existing ses id and flags if binding */
1477 if (!ses->binding) {
1478 ses->Suid = rsp->sync_hdr.SessionId;
1479 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1480 }
Sachin Prabhu166cea42016-10-07 19:11:22 +01001481
1482out:
1483 kfree(ntlmssp_blob);
1484 SMB2_sess_free_buffer(sess_data);
1485 if (!rc) {
1486 sess_data->result = 0;
1487 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1488 return;
1489 }
1490out_err:
1491 kfree(ses->ntlmssp);
1492 ses->ntlmssp = NULL;
1493 sess_data->result = rc;
1494 sess_data->func = NULL;
1495}
1496
1497static void
1498SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1499{
1500 int rc;
1501 struct cifs_ses *ses = sess_data->ses;
1502 struct smb2_sess_setup_req *req;
1503 struct smb2_sess_setup_rsp *rsp = NULL;
1504 unsigned char *ntlmssp_blob = NULL;
1505 bool use_spnego = false; /* else use raw ntlmssp */
1506 u16 blob_length = 0;
1507
1508 rc = SMB2_sess_alloc_buffer(sess_data);
1509 if (rc)
1510 goto out;
1511
1512 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001513 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001514
1515 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1516 sess_data->nls_cp);
1517 if (rc) {
1518 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1519 goto out;
1520 }
1521
1522 if (use_spnego) {
1523 /* BB eventually need to add this */
1524 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1525 rc = -EOPNOTSUPP;
1526 goto out;
1527 }
1528 sess_data->iov[1].iov_base = ntlmssp_blob;
1529 sess_data->iov[1].iov_len = blob_length;
1530
1531 rc = SMB2_sess_sendreceive(sess_data);
1532 if (rc)
1533 goto out;
1534
1535 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1536
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001537 /* keep existing ses id and flags if binding */
1538 if (!ses->binding) {
1539 ses->Suid = rsp->sync_hdr.SessionId;
1540 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1541 }
Sachin Prabhu166cea42016-10-07 19:11:22 +01001542
1543 rc = SMB2_sess_establish_session(sess_data);
1544out:
1545 kfree(ntlmssp_blob);
1546 SMB2_sess_free_buffer(sess_data);
1547 kfree(ses->ntlmssp);
1548 ses->ntlmssp = NULL;
1549 sess_data->result = rc;
1550 sess_data->func = NULL;
1551}
1552
1553static int
1554SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1555{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301556 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001557
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001558 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301559 cifs_dbg(FYI, "sess setup type %d\n", type);
1560 if (type == Unspecified) {
1561 cifs_dbg(VFS,
1562 "Unable to select appropriate authentication method!");
1563 return -EINVAL;
1564 }
1565
1566 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001567 case Kerberos:
1568 sess_data->func = SMB2_auth_kerberos;
1569 break;
1570 case RawNTLMSSP:
1571 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1572 break;
1573 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301574 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001575 return -EOPNOTSUPP;
1576 }
1577
1578 return 0;
1579}
1580
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001581int
1582SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1583 const struct nls_table *nls_cp)
1584{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001585 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001586 struct TCP_Server_Info *server = cifs_ses_server(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001587 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001588
Joe Perchesf96637b2013-05-04 22:12:25 -05001589 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001590
Jeff Layton3534b852013-05-24 07:41:01 -04001591 if (!server) {
1592 WARN(1, "%s: server is NULL!\n", __func__);
1593 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001594 }
1595
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001596 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1597 if (!sess_data)
1598 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001599
1600 rc = SMB2_select_sec(ses, sess_data);
1601 if (rc)
1602 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001603 sess_data->xid = xid;
1604 sess_data->ses = ses;
1605 sess_data->buf0_type = CIFS_NO_BUFFER;
1606 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001607 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001608
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001609 /*
1610 * Initialize the session hash with the server one.
1611 */
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001612 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001613 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001614
Sachin Prabhu166cea42016-10-07 19:11:22 +01001615 while (sess_data->func)
1616 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001617
Steve Frenchc721c382017-09-19 18:40:03 -05001618 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001619 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001620 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001621out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001622 kfree(sess_data);
1623 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001624}
1625
1626int
1627SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1628{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001629 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001630 struct smb2_logoff_req *req; /* response is also trivial struct */
1631 int rc = 0;
1632 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001633 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001634 unsigned int total_len;
1635 struct kvec iov[1];
1636 struct kvec rsp_iov;
1637 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001638
Joe Perchesf96637b2013-05-04 22:12:25 -05001639 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001640
1641 if (ses && (ses->server))
1642 server = ses->server;
1643 else
1644 return -EIO;
1645
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001646 /* no need to send SMB logoff if uid already closed due to reconnect */
1647 if (ses->need_reconnect)
1648 goto smb2_session_already_dead;
1649
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001650 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001651 if (rc)
1652 return rc;
1653
1654 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001655 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001656
1657 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1658 flags |= CIFS_TRANSFORM_REQ;
1659 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001660 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001661
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001662 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001663
1664 iov[0].iov_base = (char *)req;
1665 iov[0].iov_len = total_len;
1666
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001667 memset(&rqst, 0, sizeof(struct smb_rqst));
1668 rqst.rq_iov = iov;
1669 rqst.rq_nvec = 1;
1670
1671 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001672 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001673 /*
1674 * No tcon so can't do
1675 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1676 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001677
1678smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001679 return rc;
1680}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001681
1682static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1683{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001684 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001685}
1686
1687#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1688
Steve Frenchde9f68df2013-11-15 11:26:24 -06001689/* These are similar values to what Windows uses */
1690static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1691{
1692 tcon->max_chunks = 256;
1693 tcon->max_bytes_chunk = 1048576;
1694 tcon->max_bytes_copy = 16777216;
1695}
1696
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001697int
1698SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1699 struct cifs_tcon *tcon, const struct nls_table *cp)
1700{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001701 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001702 struct smb2_tree_connect_req *req;
1703 struct smb2_tree_connect_rsp *rsp = NULL;
1704 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001705 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001706 int rc = 0;
1707 int resp_buftype;
1708 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001709 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001710 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001711 unsigned int total_len;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001712 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001713
Joe Perchesf96637b2013-05-04 22:12:25 -05001714 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001715
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001716 if (!server || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001717 return -EIO;
1718
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001719 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1720 if (unc_path == NULL)
1721 return -ENOMEM;
1722
1723 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1724 unc_path_len *= 2;
1725 if (unc_path_len < 2) {
1726 kfree(unc_path);
1727 return -EINVAL;
1728 }
1729
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001730 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001731 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001732 atomic_set(&tcon->num_remote_opens, 0);
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001733 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1734 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001735 if (rc) {
1736 kfree(unc_path);
1737 return rc;
1738 }
1739
Steve French5a77e752018-05-09 17:43:08 -05001740 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001741 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001742
1743 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001744 /* 1 for pad */
1745 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001746
1747 /* Testing shows that buffer offset must be at location of Buffer[0] */
1748 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001749 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001750 req->PathLength = cpu_to_le16(unc_path_len - 2);
1751 iov[1].iov_base = unc_path;
1752 iov[1].iov_len = unc_path_len;
1753
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001754 /*
1755 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1756 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
Steve French8c11a602019-03-22 22:31:17 -05001757 * (Samba servers don't always set the flag so also check if null user)
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001758 */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001759 if ((server->dialect == SMB311_PROT_ID) &&
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001760 !smb3_encryption_required(tcon) &&
Steve French8c11a602019-03-22 22:31:17 -05001761 !(ses->session_flags &
1762 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1763 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
Steve French6188f282018-03-13 02:29:36 -05001764 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1765
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001766 memset(&rqst, 0, sizeof(struct smb_rqst));
1767 rqst.rq_iov = iov;
1768 rqst.rq_nvec = 2;
1769
Steve French4fe75c42019-02-14 01:19:02 -06001770 /* Need 64 for max size write so ask for more in case not there yet */
1771 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1772
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001773 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001774 cifs_small_buf_release(req);
1775 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001776 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001777 if (rc != 0) {
1778 if (tcon) {
1779 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1780 tcon->need_reconnect = true;
1781 }
1782 goto tcon_error_exit;
1783 }
1784
Christophe JAILLETcd123002017-05-12 17:59:32 +02001785 switch (rsp->ShareType) {
1786 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001787 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001788 break;
1789 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001790 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001791 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001792 break;
1793 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001794 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001795 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001796 break;
1797 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001798 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001799 rc = -EOPNOTSUPP;
1800 goto tcon_error_exit;
1801 }
1802
1803 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001804 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001805 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1806 tcon->tidStatus = CifsGood;
1807 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001808 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001809 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001810
1811 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1812 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001813 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001814
1815 if (tcon->seal &&
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001816 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001817 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001818
Steve Frenchde9f68df2013-11-15 11:26:24 -06001819 init_copy_chunk_defaults(tcon);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001820 if (server->ops->validate_negotiate)
1821 rc = server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001822tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001823
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001824 free_rsp_buf(resp_buftype, rsp);
1825 kfree(unc_path);
1826 return rc;
1827
1828tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001829 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001830 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001831 }
1832 goto tcon_exit;
1833}
1834
1835int
1836SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1837{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001838 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001839 struct smb2_tree_disconnect_req *req; /* response is trivial */
1840 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001841 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001842 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001843 unsigned int total_len;
1844 struct kvec iov[1];
1845 struct kvec rsp_iov;
1846 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001847
Joe Perchesf96637b2013-05-04 22:12:25 -05001848 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001849
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001850 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001851 return -EIO;
1852
1853 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1854 return 0;
1855
Pavel Shilovskyd9191312019-12-10 11:44:52 -08001856 close_shroot_lease(&tcon->crfid);
Ronnie Sahlberg72e73c72019-11-07 17:00:38 +10001857
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001858 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1859 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001860 if (rc)
1861 return rc;
1862
Steve French5a77e752018-05-09 17:43:08 -05001863 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001864 flags |= CIFS_TRANSFORM_REQ;
1865
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001866 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001867
1868 iov[0].iov_base = (char *)req;
1869 iov[0].iov_len = total_len;
1870
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001871 memset(&rqst, 0, sizeof(struct smb_rqst));
1872 rqst.rq_iov = iov;
1873 rqst.rq_nvec = 1;
1874
1875 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001876 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001877 if (rc)
1878 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1879
1880 return rc;
1881}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001882
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001883
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001884static struct create_durable *
1885create_durable_buf(void)
1886{
1887 struct create_durable *buf;
1888
1889 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1890 if (!buf)
1891 return NULL;
1892
1893 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001894 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001895 buf->ccontext.DataLength = cpu_to_le32(16);
1896 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1897 (struct create_durable, Name));
1898 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001899 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001900 buf->Name[0] = 'D';
1901 buf->Name[1] = 'H';
1902 buf->Name[2] = 'n';
1903 buf->Name[3] = 'Q';
1904 return buf;
1905}
1906
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001907static struct create_durable *
1908create_reconnect_durable_buf(struct cifs_fid *fid)
1909{
1910 struct create_durable *buf;
1911
1912 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1913 if (!buf)
1914 return NULL;
1915
1916 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1917 (struct create_durable, Data));
1918 buf->ccontext.DataLength = cpu_to_le32(16);
1919 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1920 (struct create_durable, Name));
1921 buf->ccontext.NameLength = cpu_to_le16(4);
1922 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1923 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001924 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001925 buf->Name[0] = 'D';
1926 buf->Name[1] = 'H';
1927 buf->Name[2] = 'n';
1928 buf->Name[3] = 'C';
1929 return buf;
1930}
1931
Steve French89a5bfa2019-07-18 17:22:18 -05001932static void
1933parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1934{
1935 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1936
1937 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1938 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1939 buf->IndexNumber = pdisk_id->DiskFileId;
1940}
1941
Steve Frenchab3459d2020-02-06 17:31:56 -06001942static void
1943parse_posix_ctxt(struct create_context *cc, struct smb_posix_info *pposix_inf)
1944{
1945 /* struct smb_posix_info *ppinf = (struct smb_posix_info *)cc; */
1946
1947 /* TODO: Need to add parsing for the context and return */
1948 printk_once(KERN_WARNING
1949 "SMB3 3.11 POSIX response context not completed yet\n");
1950}
1951
Steve French89a5bfa2019-07-18 17:22:18 -05001952void
1953smb2_parse_contexts(struct TCP_Server_Info *server,
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +10001954 struct smb2_create_rsp *rsp,
Steve French89a5bfa2019-07-18 17:22:18 -05001955 unsigned int *epoch, char *lease_key, __u8 *oplock,
1956 struct smb2_file_all_info *buf)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001957{
1958 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001959 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001960 unsigned int next;
1961 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001962 char *name;
Steve Frenchab3459d2020-02-06 17:31:56 -06001963 const char smb3_create_tag_posix[] = {0x93, 0xAD, 0x25, 0x50, 0x9C,
1964 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
1965 0xDE, 0x96, 0x8B, 0xCD, 0x7C};
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001966
Steve French89a5bfa2019-07-18 17:22:18 -05001967 *oplock = 0;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001968 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001969 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001970 cc = (struct create_context *)data_offset;
Steve French89a5bfa2019-07-18 17:22:18 -05001971
1972 /* Initialize inode number to 0 in case no valid data in qfid context */
1973 if (buf)
1974 buf->IndexNumber = 0;
1975
Justin Maggarddeb7def2016-02-09 15:52:08 -08001976 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001977 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001978 if (le16_to_cpu(cc->NameLength) == 4 &&
Steve French89a5bfa2019-07-18 17:22:18 -05001979 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1980 *oplock = server->ops->parse_lease_buf(cc, epoch,
1981 lease_key);
1982 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1983 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1984 parse_query_id_ctxt(cc, buf);
Steve Frenchab3459d2020-02-06 17:31:56 -06001985 else if ((le16_to_cpu(cc->NameLength) == 16)) {
1986 if (memcmp(name, smb3_create_tag_posix, 16) == 0)
1987 parse_posix_ctxt(cc, NULL);
1988 }
1989 /* else {
1990 cifs_dbg(FYI, "Context not matched with len %d\n",
1991 le16_to_cpu(cc->NameLength));
1992 cifs_dump_mem("Cctxt name: ", name, 4);
1993 } */
Justin Maggarddeb7def2016-02-09 15:52:08 -08001994
1995 next = le32_to_cpu(cc->Next);
1996 if (!next)
1997 break;
1998 remaining -= next;
1999 cc = (struct create_context *)((char *)cc + next);
2000 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002001
Steve French89a5bfa2019-07-18 17:22:18 -05002002 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2003 *oplock = rsp->OplockLevel;
2004
2005 return;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002006}
2007
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002008static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002009add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02002010 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002011{
2012 struct smb2_create_req *req = iov[0].iov_base;
2013 unsigned int num = *num_iovec;
2014
Stefano Brivio729c0c92018-07-05 15:10:02 +02002015 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002016 if (iov[num].iov_base == NULL)
2017 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002018 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002019 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2020 if (!req->CreateContextsOffset)
2021 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002022 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002023 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002024 le32_add_cpu(&req->CreateContextsLength,
2025 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002026 *num_iovec = num + 1;
2027 return 0;
2028}
2029
Steve Frenchb56eae42015-11-03 09:26:27 -06002030static struct create_durable_v2 *
Steve Frenchca567eb2019-03-29 16:31:07 -05002031create_durable_v2_buf(struct cifs_open_parms *oparms)
Steve Frenchb56eae42015-11-03 09:26:27 -06002032{
Steve Frenchca567eb2019-03-29 16:31:07 -05002033 struct cifs_fid *pfid = oparms->fid;
Steve Frenchb56eae42015-11-03 09:26:27 -06002034 struct create_durable_v2 *buf;
2035
2036 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2037 if (!buf)
2038 return NULL;
2039
2040 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2041 (struct create_durable_v2, dcontext));
2042 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2043 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2044 (struct create_durable_v2, Name));
2045 buf->ccontext.NameLength = cpu_to_le16(4);
2046
Steve Frenchca567eb2019-03-29 16:31:07 -05002047 /*
2048 * NB: Handle timeout defaults to 0, which allows server to choose
2049 * (most servers default to 120 seconds) and most clients default to 0.
2050 * This can be overridden at mount ("handletimeout=") if the user wants
2051 * a different persistent (or resilient) handle timeout for all opens
2052 * opens on a particular SMB3 mount.
2053 */
2054 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
Steve Frenchb56eae42015-11-03 09:26:27 -06002055 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05002056 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06002057 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2058
2059 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2060 buf->Name[0] = 'D';
2061 buf->Name[1] = 'H';
2062 buf->Name[2] = '2';
2063 buf->Name[3] = 'Q';
2064 return buf;
2065}
2066
2067static struct create_durable_handle_reconnect_v2 *
2068create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2069{
2070 struct create_durable_handle_reconnect_v2 *buf;
2071
2072 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2073 GFP_KERNEL);
2074 if (!buf)
2075 return NULL;
2076
2077 buf->ccontext.DataOffset =
2078 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2079 dcontext));
2080 buf->ccontext.DataLength =
2081 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2082 buf->ccontext.NameOffset =
2083 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2084 Name));
2085 buf->ccontext.NameLength = cpu_to_le16(4);
2086
2087 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2088 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2089 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2090 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2091
2092 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2093 buf->Name[0] = 'D';
2094 buf->Name[1] = 'H';
2095 buf->Name[2] = '2';
2096 buf->Name[3] = 'C';
2097 return buf;
2098}
2099
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002100static int
Steve Frenchb56eae42015-11-03 09:26:27 -06002101add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002102 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002103{
2104 struct smb2_create_req *req = iov[0].iov_base;
2105 unsigned int num = *num_iovec;
2106
Steve Frenchca567eb2019-03-29 16:31:07 -05002107 iov[num].iov_base = create_durable_v2_buf(oparms);
Steve Frenchb56eae42015-11-03 09:26:27 -06002108 if (iov[num].iov_base == NULL)
2109 return -ENOMEM;
2110 iov[num].iov_len = sizeof(struct create_durable_v2);
2111 if (!req->CreateContextsOffset)
2112 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002113 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002114 iov[1].iov_len);
2115 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002116 *num_iovec = num + 1;
2117 return 0;
2118}
2119
2120static int
2121add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2122 struct cifs_open_parms *oparms)
2123{
2124 struct smb2_create_req *req = iov[0].iov_base;
2125 unsigned int num = *num_iovec;
2126
2127 /* indicate that we don't need to relock the file */
2128 oparms->reconnect = false;
2129
2130 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2131 if (iov[num].iov_base == NULL)
2132 return -ENOMEM;
2133 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2134 if (!req->CreateContextsOffset)
2135 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002136 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002137 iov[1].iov_len);
2138 le32_add_cpu(&req->CreateContextsLength,
2139 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002140 *num_iovec = num + 1;
2141 return 0;
2142}
2143
2144static int
2145add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2146 struct cifs_open_parms *oparms, bool use_persistent)
2147{
2148 struct smb2_create_req *req = iov[0].iov_base;
2149 unsigned int num = *num_iovec;
2150
2151 if (use_persistent) {
2152 if (oparms->reconnect)
2153 return add_durable_reconnect_v2_context(iov, num_iovec,
2154 oparms);
2155 else
2156 return add_durable_v2_context(iov, num_iovec, oparms);
2157 }
2158
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002159 if (oparms->reconnect) {
2160 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2161 /* indicate that we don't need to relock the file */
2162 oparms->reconnect = false;
2163 } else
2164 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002165 if (iov[num].iov_base == NULL)
2166 return -ENOMEM;
2167 iov[num].iov_len = sizeof(struct create_durable);
2168 if (!req->CreateContextsOffset)
2169 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002170 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002171 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08002172 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002173 *num_iovec = num + 1;
2174 return 0;
2175}
2176
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002177/* See MS-SMB2 2.2.13.2.7 */
2178static struct crt_twarp_ctxt *
2179create_twarp_buf(__u64 timewarp)
2180{
2181 struct crt_twarp_ctxt *buf;
2182
2183 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2184 if (!buf)
2185 return NULL;
2186
2187 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2188 (struct crt_twarp_ctxt, Timestamp));
2189 buf->ccontext.DataLength = cpu_to_le32(8);
2190 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2191 (struct crt_twarp_ctxt, Name));
2192 buf->ccontext.NameLength = cpu_to_le16(4);
2193 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2194 buf->Name[0] = 'T';
2195 buf->Name[1] = 'W';
2196 buf->Name[2] = 'r';
2197 buf->Name[3] = 'p';
2198 buf->Timestamp = cpu_to_le64(timewarp);
2199 return buf;
2200}
2201
2202/* See MS-SMB2 2.2.13.2.7 */
2203static int
2204add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2205{
2206 struct smb2_create_req *req = iov[0].iov_base;
2207 unsigned int num = *num_iovec;
2208
2209 iov[num].iov_base = create_twarp_buf(timewarp);
2210 if (iov[num].iov_base == NULL)
2211 return -ENOMEM;
2212 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2213 if (!req->CreateContextsOffset)
2214 req->CreateContextsOffset = cpu_to_le32(
2215 sizeof(struct smb2_create_req) +
2216 iov[num - 1].iov_len);
2217 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2218 *num_iovec = num + 1;
2219 return 0;
2220}
2221
Steve Frenchfdef6652019-12-06 02:02:38 -06002222/* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2223static struct crt_sd_ctxt *
2224create_sd_buf(umode_t mode, unsigned int *len)
2225{
2226 struct crt_sd_ctxt *buf;
2227 struct cifs_ace *pace;
2228 unsigned int sdlen, acelen;
2229
Steve French643fbce2020-01-16 19:55:33 -06002230 *len = roundup(sizeof(struct crt_sd_ctxt) + sizeof(struct cifs_ace) * 2,
2231 8);
Steve Frenchfdef6652019-12-06 02:02:38 -06002232 buf = kzalloc(*len, GFP_KERNEL);
2233 if (buf == NULL)
2234 return buf;
2235
2236 sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) +
Steve French643fbce2020-01-16 19:55:33 -06002237 2 * sizeof(struct cifs_ace);
Steve Frenchfdef6652019-12-06 02:02:38 -06002238
2239 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2240 (struct crt_sd_ctxt, sd));
2241 buf->ccontext.DataLength = cpu_to_le32(sdlen);
2242 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2243 (struct crt_sd_ctxt, Name));
2244 buf->ccontext.NameLength = cpu_to_le16(4);
2245 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2246 buf->Name[0] = 'S';
2247 buf->Name[1] = 'e';
2248 buf->Name[2] = 'c';
2249 buf->Name[3] = 'D';
2250 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2251 /*
2252 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2253 * and "DP" ie the DACL is present
2254 */
2255 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2256
2257 /* offset owner, group and Sbz1 and SACL are all zero */
2258 buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd));
2259 buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2260
2261 /* create one ACE to hold the mode embedded in reserved special SID */
2262 pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf);
2263 acelen = setup_special_mode_ACE(pace, (__u64)mode);
Steve French643fbce2020-01-16 19:55:33 -06002264 /* and one more ACE to allow access for authenticated users */
2265 pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) +
2266 (char *)buf));
2267 acelen += setup_authusers_ACE(pace);
Steve Frenchfdef6652019-12-06 02:02:38 -06002268 buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen);
Steve French643fbce2020-01-16 19:55:33 -06002269 buf->acl.AceCount = cpu_to_le16(2);
Steve Frenchfdef6652019-12-06 02:02:38 -06002270 return buf;
2271}
2272
2273static int
2274add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
2275{
2276 struct smb2_create_req *req = iov[0].iov_base;
2277 unsigned int num = *num_iovec;
2278 unsigned int len = 0;
2279
2280 iov[num].iov_base = create_sd_buf(mode, &len);
2281 if (iov[num].iov_base == NULL)
2282 return -ENOMEM;
2283 iov[num].iov_len = len;
2284 if (!req->CreateContextsOffset)
2285 req->CreateContextsOffset = cpu_to_le32(
2286 sizeof(struct smb2_create_req) +
2287 iov[num - 1].iov_len);
2288 le32_add_cpu(&req->CreateContextsLength, len);
2289 *num_iovec = num + 1;
2290 return 0;
2291}
2292
Steve Frenchff2a09e2019-07-06 14:41:38 -05002293static struct crt_query_id_ctxt *
2294create_query_id_buf(void)
2295{
2296 struct crt_query_id_ctxt *buf;
2297
2298 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2299 if (!buf)
2300 return NULL;
2301
2302 buf->ccontext.DataOffset = cpu_to_le16(0);
2303 buf->ccontext.DataLength = cpu_to_le32(0);
2304 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2305 (struct crt_query_id_ctxt, Name));
2306 buf->ccontext.NameLength = cpu_to_le16(4);
2307 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2308 buf->Name[0] = 'Q';
2309 buf->Name[1] = 'F';
2310 buf->Name[2] = 'i';
2311 buf->Name[3] = 'd';
2312 return buf;
2313}
2314
2315/* See MS-SMB2 2.2.13.2.9 */
2316static int
2317add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2318{
2319 struct smb2_create_req *req = iov[0].iov_base;
2320 unsigned int num = *num_iovec;
2321
2322 iov[num].iov_base = create_query_id_buf();
2323 if (iov[num].iov_base == NULL)
2324 return -ENOMEM;
2325 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2326 if (!req->CreateContextsOffset)
2327 req->CreateContextsOffset = cpu_to_le32(
2328 sizeof(struct smb2_create_req) +
2329 iov[num - 1].iov_len);
2330 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2331 *num_iovec = num + 1;
2332 return 0;
2333}
2334
Aurelien Aptelf0712922017-02-22 14:47:17 +01002335static int
2336alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2337 const char *treename, const __le16 *path)
2338{
2339 int treename_len, path_len;
2340 struct nls_table *cp;
2341 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2342
2343 /*
2344 * skip leading "\\"
2345 */
2346 treename_len = strlen(treename);
2347 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2348 return -EINVAL;
2349
2350 treename += 2;
2351 treename_len -= 2;
2352
2353 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2354
2355 /*
2356 * make room for one path separator between the treename and
2357 * path
2358 */
2359 *out_len = treename_len + 1 + path_len;
2360
2361 /*
2362 * final path needs to be null-terminated UTF16 with a
2363 * size aligned to 8
2364 */
2365
2366 *out_size = roundup((*out_len+1)*2, 8);
2367 *out_path = kzalloc(*out_size, GFP_KERNEL);
2368 if (!*out_path)
2369 return -ENOMEM;
2370
2371 cp = load_nls_default();
2372 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2373 UniStrcat(*out_path, sep);
2374 UniStrcat(*out_path, path);
2375 unload_nls(cp);
2376
2377 return 0;
2378}
2379
Steve Frenchbea851b2018-06-14 21:56:32 -05002380int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2381 umode_t mode, struct cifs_tcon *tcon,
2382 const char *full_path,
2383 struct cifs_sb_info *cifs_sb)
2384{
2385 struct smb_rqst rqst;
2386 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002387 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002388 struct cifs_ses *ses = tcon->ses;
2389 struct kvec iov[3]; /* make sure at least one for each open context */
2390 struct kvec rsp_iov = {NULL, 0};
2391 int resp_buftype;
2392 int uni_path_len;
2393 __le16 *copy_path = NULL;
2394 int copy_size;
2395 int rc = 0;
2396 unsigned int n_iov = 2;
2397 __u32 file_attributes = 0;
2398 char *pc_buf = NULL;
2399 int flags = 0;
2400 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002401 __le16 *utf16_path = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002402
2403 cifs_dbg(FYI, "mkdir\n");
2404
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002405 /* resource #1: path allocation */
2406 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2407 if (!utf16_path)
2408 return -ENOMEM;
2409
YueHaibing29cbfa12018-12-18 02:51:51 +00002410 if (!ses || !(ses->server)) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002411 rc = -EIO;
2412 goto err_free_path;
2413 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002414
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002415 /* resource #2: request */
Steve Frenchbea851b2018-06-14 21:56:32 -05002416 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002417 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002418 goto err_free_path;
2419
Steve Frenchbea851b2018-06-14 21:56:32 -05002420
2421 if (smb3_encryption_required(tcon))
2422 flags |= CIFS_TRANSFORM_REQ;
2423
Steve Frenchbea851b2018-06-14 21:56:32 -05002424 req->ImpersonationLevel = IL_IMPERSONATION;
2425 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2426 /* File attributes ignored on open (used in create though) */
2427 req->FileAttributes = cpu_to_le32(file_attributes);
2428 req->ShareAccess = FILE_SHARE_ALL_LE;
2429 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2430 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2431
2432 iov[0].iov_base = (char *)req;
2433 /* -1 since last byte is buf[0] which is sent below (path) */
2434 iov[0].iov_len = total_len - 1;
2435
2436 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2437
2438 /* [MS-SMB2] 2.2.13 NameOffset:
2439 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2440 * the SMB2 header, the file name includes a prefix that will
2441 * be processed during DFS name normalization as specified in
2442 * section 3.3.5.9. Otherwise, the file name is relative to
2443 * the share that is identified by the TreeId in the SMB2
2444 * header.
2445 */
2446 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2447 int name_len;
2448
2449 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2450 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2451 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002452 tcon->treeName, utf16_path);
2453 if (rc)
2454 goto err_free_req;
2455
Steve Frenchbea851b2018-06-14 21:56:32 -05002456 req->NameLength = cpu_to_le16(name_len * 2);
2457 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002458 /* free before overwriting resource */
2459 kfree(utf16_path);
2460 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002461 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002462 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002463 /* MUST set path len (NameLength) to 0 opening root of share */
2464 req->NameLength = cpu_to_le16(uni_path_len - 2);
2465 if (uni_path_len % 8 != 0) {
2466 copy_size = roundup(uni_path_len, 8);
2467 copy_path = kzalloc(copy_size, GFP_KERNEL);
2468 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002469 rc = -ENOMEM;
2470 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002471 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002472 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002473 uni_path_len);
2474 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002475 /* free before overwriting resource */
2476 kfree(utf16_path);
2477 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002478 }
2479 }
2480
2481 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002482 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002483 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2484
2485 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002486 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002487 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002488 if (rc)
2489 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002490 pc_buf = iov[n_iov-1].iov_base;
2491 }
2492
2493
2494 memset(&rqst, 0, sizeof(struct smb_rqst));
2495 rqst.rq_iov = iov;
2496 rqst.rq_nvec = n_iov;
2497
Steve Frenchd2f15422019-09-22 00:55:46 -05002498 /* no need to inc num_remote_opens because we close it just below */
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002499 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2500 FILE_WRITE_ATTRIBUTES);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002501 /* resource #4: response buffer */
2502 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2503 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002504 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2505 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002506 CREATE_NOT_FILE,
2507 FILE_WRITE_ATTRIBUTES, rc);
2508 goto err_free_rsp_buf;
2509 }
2510
2511 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2512 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2513 ses->Suid, CREATE_NOT_FILE,
2514 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002515
2516 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2517
2518 /* Eventually save off posix specific response info and timestaps */
2519
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002520err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002521 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002522 kfree(pc_buf);
2523err_free_req:
2524 cifs_small_buf_release(req);
2525err_free_path:
2526 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002527 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002528}
Steve Frenchbea851b2018-06-14 21:56:32 -05002529
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002530int
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002531SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2532 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002533{
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002534 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002535 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002536 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002537 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002538 int copy_size;
2539 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002540 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002541 struct kvec *iov = rqst->rq_iov;
2542 __le16 *copy_path;
2543 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002544
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002545 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002546 if (rc)
2547 return rc;
2548
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002549 iov[0].iov_base = (char *)req;
2550 /* -1 since last byte is buf[0] which is sent below (path) */
2551 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002552
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002553 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002554 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002555 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2556 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002557
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002558 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002559 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002560 /* File attributes ignored on open (used in create though) */
2561 req->FileAttributes = cpu_to_le32(file_attributes);
2562 req->ShareAccess = FILE_SHARE_ALL_LE;
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002563
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002564 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2565 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002566 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002567
2568 /* [MS-SMB2] 2.2.13 NameOffset:
2569 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2570 * the SMB2 header, the file name includes a prefix that will
2571 * be processed during DFS name normalization as specified in
2572 * section 3.3.5.9. Otherwise, the file name is relative to
2573 * the share that is identified by the TreeId in the SMB2
2574 * header.
2575 */
2576 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2577 int name_len;
2578
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002579 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002580 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2581 &name_len,
2582 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002583 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002584 return rc;
2585 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002586 uni_path_len = copy_size;
2587 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002588 } else {
2589 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2590 /* MUST set path len (NameLength) to 0 opening root of share */
2591 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002592 copy_size = uni_path_len;
2593 if (copy_size % 8 != 0)
2594 copy_size = roundup(copy_size, 8);
2595 copy_path = kzalloc(copy_size, GFP_KERNEL);
2596 if (!copy_path)
2597 return -ENOMEM;
2598 memcpy((char *)copy_path, (const char *)path,
2599 uni_path_len);
2600 uni_path_len = copy_size;
2601 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002602 }
2603
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002604 iov[1].iov_len = uni_path_len;
2605 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002606
Steve French3e7a02d2019-09-11 21:46:20 -05002607 if ((!server->oplocks) || (tcon->no_lease))
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002608 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2609
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002610 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002611 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2612 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002613 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2614 (oparms->create_options & CREATE_NOT_FILE))
2615 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002616 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002617 rc = add_lease_context(server, iov, &n_iov,
2618 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002619 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002620 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002621 }
2622
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002623 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2624 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002625 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002626 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002627 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002628 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002629 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002630 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002631
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002632 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002633 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002634 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002635 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002636 }
2637
Steve Frenchce558b02018-05-31 19:16:54 -05002638 if (tcon->posix_extensions) {
2639 if (n_iov > 2) {
2640 struct create_context *ccontext =
2641 (struct create_context *)iov[n_iov-1].iov_base;
2642 ccontext->Next =
2643 cpu_to_le32(iov[n_iov-1].iov_len);
2644 }
2645
2646 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002647 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002648 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002649 }
Steve Frenchce558b02018-05-31 19:16:54 -05002650
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002651 if (tcon->snapshot_time) {
2652 cifs_dbg(FYI, "adding snapshot context\n");
2653 if (n_iov > 2) {
2654 struct create_context *ccontext =
2655 (struct create_context *)iov[n_iov-1].iov_base;
2656 ccontext->Next =
2657 cpu_to_le32(iov[n_iov-1].iov_len);
2658 }
2659
2660 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2661 if (rc)
2662 return rc;
2663 }
2664
Steve Frenchfdef6652019-12-06 02:02:38 -06002665 if ((oparms->disposition != FILE_OPEN) &&
Steve French231e2a02019-12-07 17:38:22 -06002666 (oparms->cifs_sb) &&
2667 (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
Steve Frenchd0959b02019-10-05 10:53:58 -05002668 (oparms->mode != ACL_NO_MODE)) {
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002669 if (n_iov > 2) {
2670 struct create_context *ccontext =
2671 (struct create_context *)iov[n_iov-1].iov_base;
2672 ccontext->Next =
2673 cpu_to_le32(iov[n_iov-1].iov_len);
2674 }
2675
Steve Frenchfdef6652019-12-06 02:02:38 -06002676 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2677 rc = add_sd_context(iov, &n_iov, oparms->mode);
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002678 if (rc)
2679 return rc;
2680 }
2681
Steve Frenchff2a09e2019-07-06 14:41:38 -05002682 if (n_iov > 2) {
2683 struct create_context *ccontext =
2684 (struct create_context *)iov[n_iov-1].iov_base;
2685 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2686 }
2687 add_query_id_context(iov, &n_iov);
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002688
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002689 rqst->rq_nvec = n_iov;
2690 return 0;
2691}
2692
2693/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2694 * All other vectors are freed by kfree().
2695 */
2696void
2697SMB2_open_free(struct smb_rqst *rqst)
2698{
2699 int i;
2700
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002701 if (rqst && rqst->rq_iov) {
2702 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2703 for (i = 1; i < rqst->rq_nvec; i++)
2704 if (rqst->rq_iov[i].iov_base != smb2_padding)
2705 kfree(rqst->rq_iov[i].iov_base);
2706 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002707}
2708
2709int
2710SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2711 __u8 *oplock, struct smb2_file_all_info *buf,
2712 struct kvec *err_iov, int *buftype)
2713{
2714 struct smb_rqst rqst;
2715 struct smb2_create_rsp *rsp = NULL;
2716 struct TCP_Server_Info *server;
2717 struct cifs_tcon *tcon = oparms->tcon;
2718 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002719 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002720 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002721 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002722 int rc = 0;
2723 int flags = 0;
2724
2725 cifs_dbg(FYI, "create/open\n");
2726 if (ses && (ses->server))
2727 server = ses->server;
2728 else
2729 return -EIO;
2730
2731 if (smb3_encryption_required(tcon))
2732 flags |= CIFS_TRANSFORM_REQ;
2733
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002734 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002735 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002736 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002737 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002738
2739 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2740 if (rc)
2741 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002742
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002743 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2744 oparms->create_options, oparms->desired_access);
2745
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002746 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002747 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002748 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002749
2750 if (rc != 0) {
2751 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002752 if (err_iov && rsp) {
2753 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002754 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002755 resp_buftype = CIFS_NO_BUFFER;
2756 rsp = NULL;
2757 }
Steve French28d59362018-05-30 21:42:34 -05002758 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2759 oparms->create_options, oparms->desired_access, rc);
Steve French7dcc82c2019-09-11 00:07:36 -05002760 if (rc == -EREMCHG) {
2761 printk_once(KERN_WARNING "server share %s deleted\n",
2762 tcon->treeName);
2763 tcon->need_reconnect = true;
2764 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002765 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002766 } else
2767 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2768 ses->Suid, oparms->create_options,
2769 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002770
Steve Frenchfae80442018-10-19 17:14:32 -05002771 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002772 oparms->fid->persistent_fid = rsp->PersistentFileId;
2773 oparms->fid->volatile_fid = rsp->VolatileFileId;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002774#ifdef CONFIG_CIFS_DEBUG2
2775 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2776#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002777
2778 if (buf) {
2779 memcpy(buf, &rsp->CreationTime, 32);
2780 buf->AllocationSize = rsp->AllocationSize;
2781 buf->EndOfFile = rsp->EndofFile;
2782 buf->Attributes = rsp->FileAttributes;
2783 buf->NumberOfLinks = cpu_to_le32(1);
2784 buf->DeletePending = 0;
2785 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002786
Steve French89a5bfa2019-07-18 17:22:18 -05002787
2788 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2789 oparms->fid->lease_key, oplock, buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002790creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002791 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002792 free_rsp_buf(resp_buftype, rsp);
2793 return rc;
2794}
2795
Steve French4a72daf2013-06-25 00:20:49 -05002796int
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002797SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2798 u64 persistent_fid, u64 volatile_fid, u32 opcode,
Steve French153322f2019-03-28 22:32:49 -05002799 bool is_fsctl, char *in_data, u32 indatalen,
2800 __u32 max_response_size)
Steve French4a72daf2013-06-25 00:20:49 -05002801{
2802 struct smb2_ioctl_req *req;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002803 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002804 unsigned int total_len;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002805 int rc;
Long Li2c87d6a2019-05-15 14:09:05 -07002806 char *in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002807
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -03002808 rc = smb2_ioctl_req_init(opcode, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002809 if (rc)
2810 return rc;
2811
Long Li2c87d6a2019-05-15 14:09:05 -07002812 if (indatalen) {
2813 /*
2814 * indatalen is usually small at a couple of bytes max, so
2815 * just allocate through generic pool
2816 */
YueHaibingd81f0972019-06-01 03:31:10 +00002817 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
Long Li2c87d6a2019-05-15 14:09:05 -07002818 if (!in_data_buf) {
2819 cifs_small_buf_release(req);
2820 return -ENOMEM;
2821 }
Long Li2c87d6a2019-05-15 14:09:05 -07002822 }
2823
Steve French4a72daf2013-06-25 00:20:49 -05002824 req->CtlCode = cpu_to_le32(opcode);
2825 req->PersistentFileId = persistent_fid;
2826 req->VolatileFileId = volatile_fid;
2827
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002828 iov[0].iov_base = (char *)req;
2829 /*
2830 * If no input data, the size of ioctl struct in
2831 * protocol spec still includes a 1 byte data buffer,
2832 * but if input data passed to ioctl, we do not
2833 * want to double count this, so we do not send
2834 * the dummy one byte of data in iovec[0] if sending
2835 * input data (in iovec[1]).
2836 */
Steve French4a72daf2013-06-25 00:20:49 -05002837 if (indatalen) {
2838 req->InputCount = cpu_to_le32(indatalen);
2839 /* do not set InputOffset if no input data */
2840 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002841 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002842 rqst->rq_nvec = 2;
2843 iov[0].iov_len = total_len - 1;
Long Li2c87d6a2019-05-15 14:09:05 -07002844 iov[1].iov_base = in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002845 iov[1].iov_len = indatalen;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002846 } else {
2847 rqst->rq_nvec = 1;
2848 iov[0].iov_len = total_len;
2849 }
Steve French4a72daf2013-06-25 00:20:49 -05002850
2851 req->OutputOffset = 0;
2852 req->OutputCount = 0; /* MBZ */
2853
2854 /*
Steve French153322f2019-03-28 22:32:49 -05002855 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2856 * We Could increase default MaxOutputResponse, but that could require
2857 * more credits. Windows typically sets this smaller, but for some
Steve French4a72daf2013-06-25 00:20:49 -05002858 * ioctls it may be useful to allow server to send more. No point
2859 * limiting what the server can send as long as fits in one credit
Steve French153322f2019-03-28 22:32:49 -05002860 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2861 * to increase this limit up in the future.
2862 * Note that for snapshot queries that servers like Azure expect that
2863 * the first query be minimal size (and just used to get the number/size
2864 * of previous versions) so response size must be specified as EXACTLY
2865 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2866 * of eight bytes. Currently that is the only case where we set max
2867 * response size smaller.
Steve French4a72daf2013-06-25 00:20:49 -05002868 */
Steve French153322f2019-03-28 22:32:49 -05002869 req->MaxOutputResponse = cpu_to_le32(max_response_size);
Steve French4a72daf2013-06-25 00:20:49 -05002870
2871 if (is_fsctl)
2872 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2873 else
2874 req->Flags = 0;
2875
Steve French4587eee2017-10-25 15:58:31 -05002876 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2877 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002878 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002879
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002880 return 0;
2881}
2882
2883void
2884SMB2_ioctl_free(struct smb_rqst *rqst)
2885{
Murphy Zhou6457c202019-05-23 12:12:43 +08002886 int i;
Long Li2c87d6a2019-05-15 14:09:05 -07002887 if (rqst && rqst->rq_iov) {
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002888 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Murphy Zhou6457c202019-05-23 12:12:43 +08002889 for (i = 1; i < rqst->rq_nvec; i++)
2890 if (rqst->rq_iov[i].iov_base != smb2_padding)
2891 kfree(rqst->rq_iov[i].iov_base);
Long Li2c87d6a2019-05-15 14:09:05 -07002892 }
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002893}
2894
Steve French153322f2019-03-28 22:32:49 -05002895
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002896/*
2897 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2898 */
2899int
2900SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2901 u64 volatile_fid, u32 opcode, bool is_fsctl,
Steve French153322f2019-03-28 22:32:49 -05002902 char *in_data, u32 indatalen, u32 max_out_data_len,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002903 char **out_data, u32 *plen /* returned data len */)
2904{
2905 struct smb_rqst rqst;
2906 struct smb2_ioctl_rsp *rsp = NULL;
2907 struct cifs_ses *ses;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002908 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002909 struct kvec rsp_iov = {NULL, 0};
2910 int resp_buftype = CIFS_NO_BUFFER;
2911 int rc = 0;
2912 int flags = 0;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002913 struct TCP_Server_Info *server;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002914
2915 cifs_dbg(FYI, "SMB2 IOCTL\n");
2916
2917 if (out_data != NULL)
2918 *out_data = NULL;
2919
2920 /* zero out returned data len, in case of error */
2921 if (plen)
2922 *plen = 0;
2923
2924 if (tcon)
2925 ses = tcon->ses;
2926 else
2927 return -EIO;
2928
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01002929 if (!ses)
2930 return -EIO;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002931 server = ses->server;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01002932 if (!server)
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002933 return -EIO;
2934
2935 if (smb3_encryption_required(tcon))
2936 flags |= CIFS_TRANSFORM_REQ;
2937
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002938 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002939 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002940 rqst.rq_iov = iov;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002941 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002942
Steve French153322f2019-03-28 22:32:49 -05002943 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2944 is_fsctl, in_data, indatalen, max_out_data_len);
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002945 if (rc)
2946 goto ioctl_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002947
2948 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002949 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002950 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002951
Steve Frencheccb4422018-05-17 21:16:55 -05002952 if (rc != 0)
2953 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2954 ses->Suid, 0, opcode, rc);
2955
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002956 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
Steve French8e353102015-03-26 19:47:02 -05002957 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002958 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002959 } else if (rc == -EINVAL) {
2960 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2961 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002962 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002963 goto ioctl_exit;
2964 }
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002965 } else if (rc == -E2BIG) {
2966 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2967 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2968 goto ioctl_exit;
2969 }
Steve French4a72daf2013-06-25 00:20:49 -05002970 }
2971
2972 /* check if caller wants to look at return data or just return rc */
2973 if ((plen == NULL) || (out_data == NULL))
2974 goto ioctl_exit;
2975
2976 *plen = le32_to_cpu(rsp->OutputCount);
2977
2978 /* We check for obvious errors in the output buffer length and offset */
2979 if (*plen == 0)
2980 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002981 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002982 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002983 *plen = 0;
2984 rc = -EIO;
2985 goto ioctl_exit;
2986 }
2987
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002988 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002989 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
Steve French4a72daf2013-06-25 00:20:49 -05002990 le32_to_cpu(rsp->OutputOffset));
2991 *plen = 0;
2992 rc = -EIO;
2993 goto ioctl_exit;
2994 }
2995
YueHaibingd034fee2018-09-10 01:33:06 +00002996 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2997 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05002998 if (*out_data == NULL) {
2999 rc = -ENOMEM;
3000 goto ioctl_exit;
3001 }
3002
Steve French4a72daf2013-06-25 00:20:49 -05003003ioctl_exit:
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003004 SMB2_ioctl_free(&rqst);
Steve French4a72daf2013-06-25 00:20:49 -05003005 free_rsp_buf(resp_buftype, rsp);
3006 return rc;
3007}
3008
Steve French64a5cfa2013-10-14 15:31:32 -05003009/*
3010 * Individual callers to ioctl worker function follow
3011 */
3012
3013int
3014SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3015 u64 persistent_fid, u64 volatile_fid)
3016{
3017 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05003018 struct compress_ioctl fsctl_input;
3019 char *ret_data = NULL;
3020
3021 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08003022 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05003023
3024 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3025 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3026 (char *)&fsctl_input /* data input */,
Steve French153322f2019-03-28 22:32:49 -05003027 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3028 &ret_data /* out data */, NULL);
Steve French64a5cfa2013-10-14 15:31:32 -05003029
3030 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05003031
3032 return rc;
3033}
3034
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003035int
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003036SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Steve French43f8a6a2019-12-02 21:46:54 -06003037 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003038{
3039 struct smb2_close_req *req;
3040 struct kvec *iov = rqst->rq_iov;
3041 unsigned int total_len;
3042 int rc;
3043
3044 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
3045 if (rc)
3046 return rc;
3047
3048 req->PersistentFileId = persistent_fid;
3049 req->VolatileFileId = volatile_fid;
Steve French43f8a6a2019-12-02 21:46:54 -06003050 if (query_attrs)
3051 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3052 else
3053 req->Flags = 0;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003054 iov[0].iov_base = (char *)req;
3055 iov[0].iov_len = total_len;
3056
3057 return 0;
3058}
3059
3060void
3061SMB2_close_free(struct smb_rqst *rqst)
3062{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003063 if (rqst && rqst->rq_iov)
3064 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003065}
3066
3067int
Steve French43f8a6a2019-12-02 21:46:54 -06003068__SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3069 u64 persistent_fid, u64 volatile_fid,
3070 struct smb2_file_network_open_info *pbuf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003071{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003072 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003073 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003074 struct cifs_ses *ses = tcon->ses;
3075 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003076 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01003077 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003078 int rc = 0;
Steve French9e8fae22019-12-02 17:55:41 -06003079 int flags = 0;
Steve French43f8a6a2019-12-02 21:46:54 -06003080 bool query_attrs = false;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003081
Joe Perchesf96637b2013-05-04 22:12:25 -05003082 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003083
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003084 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003085 return -EIO;
3086
Steve French5a77e752018-05-09 17:43:08 -05003087 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003088 flags |= CIFS_TRANSFORM_REQ;
3089
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003090 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003091 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003092 rqst.rq_iov = iov;
3093 rqst.rq_nvec = 1;
3094
Steve French43f8a6a2019-12-02 21:46:54 -06003095 /* check if need to ask server to return timestamps in close response */
3096 if (pbuf)
3097 query_attrs = true;
3098
Steve Frenchf90f9792019-09-03 18:35:42 -05003099 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Steve French43f8a6a2019-12-02 21:46:54 -06003100 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid,
3101 query_attrs);
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003102 if (rc)
3103 goto close_exit;
3104
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003105 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003106 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003107
3108 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09003109 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003110 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3111 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003112 goto close_exit;
Steve French43f8a6a2019-12-02 21:46:54 -06003113 } else {
Steve Frenchf90f9792019-09-03 18:35:42 -05003114 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3115 ses->Suid);
Steve French43f8a6a2019-12-02 21:46:54 -06003116 /*
3117 * Note that have to subtract 4 since struct network_open_info
3118 * has a final 4 byte pad that close response does not have
3119 */
3120 if (pbuf)
3121 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3122 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003123
Steve Frenchfae80442018-10-19 17:14:32 -05003124 atomic_dec(&tcon->num_remote_opens);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003125close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003126 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003127 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003128
3129 /* retry close in a worker thread if this one is interrupted */
3130 if (rc == -EINTR) {
Steve French9e8fae22019-12-02 17:55:41 -06003131 int tmp_rc;
3132
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003133 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3134 volatile_fid);
3135 if (tmp_rc)
3136 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3137 persistent_fid, tmp_rc);
3138 }
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003139 return rc;
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06003140}
3141
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003142int
Steve French43f8a6a2019-12-02 21:46:54 -06003143SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3144 u64 persistent_fid, u64 volatile_fid)
3145{
3146 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3147}
3148
3149int
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003150smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3151 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003152{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003153 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003154 char *end_of_smb = smb_len + (char *)iov->iov_base;
3155 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003156 char *end_of_buf = begin_of_buf + buffer_length;
3157
3158
3159 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003160 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3161 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003162 return -EINVAL;
3163 }
3164
3165 /* check if beyond RFC1001 maximum length */
3166 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003167 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3168 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003169 return -EINVAL;
3170 }
3171
3172 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003173 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003174 return -EINVAL;
3175 }
3176
3177 return 0;
3178}
3179
3180/*
3181 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3182 * Caller must free buffer.
3183 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003184int
3185smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3186 struct kvec *iov, unsigned int minbufsize,
3187 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003188{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003189 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003190 int rc;
3191
3192 if (!data)
3193 return -EINVAL;
3194
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003195 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003196 if (rc)
3197 return rc;
3198
3199 memcpy(data, begin_of_buf, buffer_length);
3200
3201 return 0;
3202}
3203
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003204int
3205SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3206 u64 persistent_fid, u64 volatile_fid,
3207 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003208 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003209{
3210 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003211 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003212 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003213 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003214
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003215 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3216 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003217 if (rc)
3218 return rc;
3219
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003220 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003221 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003222 req->PersistentFileId = persistent_fid;
3223 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003224 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02003225
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003226 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003227 if (input_len) {
3228 req->InputBufferLength = cpu_to_le32(input_len);
3229 /* total_len for smb query request never close to le16 max */
3230 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3231 memcpy(req->Buffer, input, input_len);
3232 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003233
3234 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003235 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003236 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003237 return 0;
3238}
3239
3240void
3241SMB2_query_info_free(struct smb_rqst *rqst)
3242{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003243 if (rqst && rqst->rq_iov)
3244 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003245}
3246
3247static int
3248query_info(const unsigned int xid, struct cifs_tcon *tcon,
3249 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3250 u32 additional_info, size_t output_len, size_t min_len, void **data,
3251 u32 *dlen)
3252{
3253 struct smb_rqst rqst;
3254 struct smb2_query_info_rsp *rsp = NULL;
3255 struct kvec iov[1];
3256 struct kvec rsp_iov;
3257 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01003258 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003259 struct cifs_ses *ses = tcon->ses;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003260 struct TCP_Server_Info *server;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003261 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003262 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003263
3264 cifs_dbg(FYI, "Query Info\n");
3265
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003266 if (!ses)
3267 return -EIO;
3268 server = ses->server;
3269 if (!server)
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003270 return -EIO;
3271
3272 if (smb3_encryption_required(tcon))
3273 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003274
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003275 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003276 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003277 rqst.rq_iov = iov;
3278 rqst.rq_nvec = 1;
3279
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003280 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3281 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003282 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003283 if (rc)
3284 goto qinf_exit;
3285
Steve Frenchd42043a2019-02-26 21:58:30 -06003286 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3287 ses->Suid, info_class, (__u32)info_type);
3288
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003289 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003290 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003291
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003292 if (rc) {
3293 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003294 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3295 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003296 goto qinf_exit;
3297 }
3298
Steve Frenchd42043a2019-02-26 21:58:30 -06003299 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3300 ses->Suid, info_class, (__u32)info_type);
3301
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003302 if (dlen) {
3303 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3304 if (!*data) {
3305 *data = kmalloc(*dlen, GFP_KERNEL);
3306 if (!*data) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003307 cifs_tcon_dbg(VFS,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003308 "Error %d allocating memory for acl\n",
3309 rc);
3310 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003311 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003312 goto qinf_exit;
3313 }
Colin Ian King73aaf922019-01-16 16:28:59 +00003314 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003315 }
3316 }
3317
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003318 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3319 le32_to_cpu(rsp->OutputBufferLength),
3320 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00003321 if (rc && allocated) {
3322 kfree(*data);
3323 *data = NULL;
3324 *dlen = 0;
3325 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003326
3327qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003328 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003329 free_rsp_buf(resp_buftype, rsp);
3330 return rc;
3331}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003332
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003333int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3334 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003335{
3336 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003337 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04003338 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003339 sizeof(struct smb2_file_all_info), (void **)&data,
3340 NULL);
3341}
3342
3343int
3344SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3345 u64 persistent_fid, u64 volatile_fid,
3346 void **data, u32 *plen)
3347{
3348 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3349 *plen = 0;
3350
3351 return query_info(xid, tcon, persistent_fid, volatile_fid,
3352 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05003353 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003354}
3355
3356int
3357SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3358 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3359{
3360 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003361 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003362 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003363 sizeof(struct smb2_file_internal_info),
3364 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003365}
3366
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003367/*
Steve Frenchc3498182019-09-15 22:38:52 -05003368 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3369 * See MS-SMB2 2.2.35 and 2.2.36
3370 */
3371
zhengbin388962e2019-09-23 15:06:18 +08003372static int
Steve Frenchc3498182019-09-15 22:38:52 -05003373SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3374 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid,
3375 u32 completion_filter, bool watch_tree)
3376{
3377 struct smb2_change_notify_req *req;
3378 struct kvec *iov = rqst->rq_iov;
3379 unsigned int total_len;
3380 int rc;
3381
3382 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len);
3383 if (rc)
3384 return rc;
3385
3386 req->PersistentFileId = persistent_fid;
3387 req->VolatileFileId = volatile_fid;
Steve Frenchd26c2dd2020-02-06 06:00:14 -06003388 /* See note 354 of MS-SMB2, 64K max */
Steve French52870d52019-10-01 21:25:46 -05003389 req->OutputBufferLength =
3390 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
Steve Frenchc3498182019-09-15 22:38:52 -05003391 req->CompletionFilter = cpu_to_le32(completion_filter);
3392 if (watch_tree)
3393 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3394 else
3395 req->Flags = 0;
3396
3397 iov[0].iov_base = (char *)req;
3398 iov[0].iov_len = total_len;
3399
3400 return 0;
3401}
3402
3403int
3404SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3405 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3406 u32 completion_filter)
3407{
3408 struct cifs_ses *ses = tcon->ses;
3409 struct smb_rqst rqst;
3410 struct kvec iov[1];
3411 struct kvec rsp_iov = {NULL, 0};
3412 int resp_buftype = CIFS_NO_BUFFER;
3413 int flags = 0;
3414 int rc = 0;
3415
3416 cifs_dbg(FYI, "change notify\n");
3417 if (!ses || !(ses->server))
3418 return -EIO;
3419
3420 if (smb3_encryption_required(tcon))
3421 flags |= CIFS_TRANSFORM_REQ;
3422
3423 memset(&rqst, 0, sizeof(struct smb_rqst));
3424 memset(&iov, 0, sizeof(iov));
3425 rqst.rq_iov = iov;
3426 rqst.rq_nvec = 1;
3427
3428 rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid,
3429 completion_filter, watch_tree);
3430 if (rc)
3431 goto cnotify_exit;
3432
3433 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3434 (u8)watch_tree, completion_filter);
3435 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3436
3437 if (rc != 0) {
3438 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3439 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3440 (u8)watch_tree, completion_filter, rc);
3441 } else
3442 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3443 ses->Suid, (u8)watch_tree, completion_filter);
3444
3445 cnotify_exit:
3446 if (rqst.rq_iov)
3447 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3448 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3449 return rc;
3450}
3451
3452
3453
3454/*
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003455 * This is a no-op for now. We're not really interested in the reply, but
3456 * rather in the fact that the server sent one and that server->lstrp
3457 * gets updated.
3458 *
3459 * FIXME: maybe we should consider checking that the reply matches request?
3460 */
3461static void
3462smb2_echo_callback(struct mid_q_entry *mid)
3463{
3464 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003465 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003466 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003467
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003468 if (mid->mid_state == MID_RESPONSE_RECEIVED
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003469 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3470 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3471 credits.instance = server->reconnect_instance;
3472 }
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003473
3474 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003475 add_credits(server, &credits, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003476}
3477
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003478void smb2_reconnect_server(struct work_struct *work)
3479{
3480 struct TCP_Server_Info *server = container_of(work,
3481 struct TCP_Server_Info, reconnect.work);
3482 struct cifs_ses *ses;
3483 struct cifs_tcon *tcon, *tcon2;
3484 struct list_head tmp_list;
3485 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01003486 int rc;
3487 int resched = false;
3488
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003489
3490 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3491 mutex_lock(&server->reconnect_mutex);
3492
3493 INIT_LIST_HEAD(&tmp_list);
3494 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3495
3496 spin_lock(&cifs_tcp_ses_lock);
3497 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3498 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003499 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003500 tcon->tc_count++;
3501 list_add_tail(&tcon->rlist, &tmp_list);
3502 tcon_exist = true;
3503 }
3504 }
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003505 /*
3506 * IPC has the same lifetime as its session and uses its
3507 * refcount.
3508 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003509 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3510 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3511 tcon_exist = true;
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003512 ses->ses_count++;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003513 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003514 }
3515 /*
3516 * Get the reference to server struct to be sure that the last call of
3517 * cifs_put_tcon() in the loop below won't release the server pointer.
3518 */
3519 if (tcon_exist)
3520 server->srv_count++;
3521
3522 spin_unlock(&cifs_tcp_ses_lock);
3523
3524 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01003525 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3526 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003527 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01003528 else
3529 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003530 list_del_init(&tcon->rlist);
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003531 if (tcon->ipc)
3532 cifs_put_smb_ses(tcon->ses);
3533 else
3534 cifs_put_tcon(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003535 }
3536
3537 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01003538 if (resched)
3539 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003540 mutex_unlock(&server->reconnect_mutex);
3541
3542 /* now we can safely release srv struct */
3543 if (tcon_exist)
3544 cifs_put_tcp_session(server, 1);
3545}
3546
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003547int
3548SMB2_echo(struct TCP_Server_Info *server)
3549{
3550 struct smb2_echo_req *req;
3551 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003552 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003553 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003554 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003555 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003556
Joe Perchesf96637b2013-05-04 22:12:25 -05003557 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003558
Steve French4fcd1812016-06-22 20:12:05 -05003559 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003560 /* No need to send echo on newly established connections */
3561 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3562 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003563 }
3564
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003565 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003566 if (rc)
3567 return rc;
3568
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003569 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003570
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003571 iov[0].iov_len = total_len;
3572 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003573
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003574 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003575 server, CIFS_ECHO_OP, NULL);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003576 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003577 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003578
3579 cifs_small_buf_release(req);
3580 return rc;
3581}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003582
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003583void
3584SMB2_flush_free(struct smb_rqst *rqst)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003585{
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003586 if (rqst && rqst->rq_iov)
3587 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3588}
3589
3590int
3591SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3592 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3593{
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003594 struct smb2_flush_req *req;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003595 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003596 unsigned int total_len;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003597 int rc;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003598
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003599 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003600 if (rc)
3601 return rc;
3602
3603 req->PersistentFileId = persistent_fid;
3604 req->VolatileFileId = volatile_fid;
3605
3606 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003607 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003608
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003609 return 0;
3610}
3611
3612int
3613SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3614 u64 volatile_fid)
3615{
3616 struct cifs_ses *ses = tcon->ses;
3617 struct smb_rqst rqst;
3618 struct kvec iov[1];
3619 struct kvec rsp_iov = {NULL, 0};
3620 int resp_buftype = CIFS_NO_BUFFER;
3621 int flags = 0;
3622 int rc = 0;
3623
3624 cifs_dbg(FYI, "flush\n");
3625 if (!ses || !(ses->server))
3626 return -EIO;
3627
3628 if (smb3_encryption_required(tcon))
3629 flags |= CIFS_TRANSFORM_REQ;
3630
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003631 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003632 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003633 rqst.rq_iov = iov;
3634 rqst.rq_nvec = 1;
3635
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003636 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3637 if (rc)
3638 goto flush_exit;
3639
Steve Frenchf90f9792019-09-03 18:35:42 -05003640 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003641 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003642
Steve Frencheccb4422018-05-17 21:16:55 -05003643 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003644 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003645 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3646 rc);
Steve Frenchf90f9792019-09-03 18:35:42 -05003647 } else
3648 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3649 ses->Suid);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003650
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003651 flush_exit:
3652 SMB2_flush_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003653 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003654 return rc;
3655}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003656
3657/*
3658 * To form a chain of read requests, any read requests after the first should
3659 * have the end_of_chain boolean set to true.
3660 */
3661static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003662smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003663 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3664 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003665{
3666 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003667 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003668 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07003669 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003670
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003671 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3672 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003673 if (rc)
3674 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003675
3676 server = io_parms->tcon->ses->server;
3677 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003678 return -ECONNABORTED;
3679
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003680 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003681 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003682
3683 req->PersistentFileId = io_parms->persistent_fid;
3684 req->VolatileFileId = io_parms->volatile_fid;
3685 req->ReadChannelInfoOffset = 0; /* reserved */
3686 req->ReadChannelInfoLength = 0; /* reserved */
3687 req->Channel = 0; /* reserved */
3688 req->MinimumCount = 0;
3689 req->Length = cpu_to_le32(io_parms->length);
3690 req->Offset = cpu_to_le64(io_parms->offset);
Steve Frenchd323c2462019-02-25 00:52:43 -06003691
3692 trace_smb3_read_enter(0 /* xid */,
3693 io_parms->persistent_fid,
3694 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3695 io_parms->offset, io_parms->length);
Long Libd3dcc62017-11-22 17:38:47 -07003696#ifdef CONFIG_CIFS_SMB_DIRECT
3697 /*
3698 * If we want to do a RDMA write, fill in and append
3699 * smbd_buffer_descriptor_v1 to the end of read request
3700 */
Long Libb4c0412018-04-17 12:17:08 -07003701 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003702 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003703
Long Libd3dcc62017-11-22 17:38:47 -07003704 struct smbd_buffer_descriptor_v1 *v1;
3705 bool need_invalidate =
3706 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3707
3708 rdata->mr = smbd_register_mr(
3709 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003710 rdata->nr_pages, rdata->page_offset,
3711 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003712 if (!rdata->mr)
Long Lib7972092019-04-05 21:36:34 +00003713 return -EAGAIN;
Long Libd3dcc62017-11-22 17:38:47 -07003714
3715 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3716 if (need_invalidate)
3717 req->Channel = SMB2_CHANNEL_RDMA_V1;
3718 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003719 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003720 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003721 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003722 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003723 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3724 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3725 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003726
3727 *total_len += sizeof(*v1) - 1;
3728 }
3729#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003730 if (request_type & CHAINED_REQUEST) {
3731 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003732 /* next 8-byte aligned request */
3733 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3734 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003735 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003736 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003737 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003738 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003739 /*
3740 * Related requests use info from previous read request
3741 * in chain.
3742 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003743 shdr->SessionId = 0xFFFFFFFF;
3744 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003745 req->PersistentFileId = 0xFFFFFFFF;
3746 req->VolatileFileId = 0xFFFFFFFF;
3747 }
3748 }
3749 if (remaining_bytes > io_parms->length)
3750 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3751 else
3752 req->RemainingBytes = 0;
3753
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003754 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003755 return rc;
3756}
3757
3758static void
3759smb2_readv_callback(struct mid_q_entry *mid)
3760{
3761 struct cifs_readdata *rdata = mid->callback_data;
3762 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3763 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003764 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003765 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003766 struct cifs_credits credits = { .value = 0, .instance = 0 };
Steve French46f17d12019-09-04 23:07:52 -05003767 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3768 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07003769 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003770 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003771 .rq_npages = rdata->nr_pages,
3772 .rq_pagesz = rdata->pagesz,
3773 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003774
Joe Perchesf96637b2013-05-04 22:12:25 -05003775 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3776 __func__, mid->mid, mid->mid_state, rdata->result,
3777 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003778
3779 switch (mid->mid_state) {
3780 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003781 credits.value = le16_to_cpu(shdr->CreditRequest);
3782 credits.instance = server->reconnect_instance;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003783 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003784 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003785 int rc;
3786
Jeff Layton0b688cf2012-09-18 16:20:34 -07003787 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003788 if (rc)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003789 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003790 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003791 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003792 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003793 task_io_account_read(rdata->got_bytes);
3794 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003795 break;
3796 case MID_REQUEST_SUBMITTED:
3797 case MID_RETRY_NEEDED:
3798 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003799 if (server->sign && rdata->got_bytes)
3800 /* reset bytes number since we can not check a sign */
3801 rdata->got_bytes = 0;
3802 /* FIXME: should this be counted toward the initiating task? */
3803 task_io_account_read(rdata->got_bytes);
3804 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003805 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003806 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003807 credits.value = le16_to_cpu(shdr->CreditRequest);
3808 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003809 /* fall through */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003810 default:
Pavel Shilovsky6b15eb12019-01-18 15:46:14 -08003811 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003812 }
Long Libd3dcc62017-11-22 17:38:47 -07003813#ifdef CONFIG_CIFS_SMB_DIRECT
3814 /*
3815 * If this rdata has a memmory registered, the MR can be freed
3816 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3817 * because they have limited number and are used for future I/Os
3818 */
3819 if (rdata->mr) {
3820 smbd_deregister_mr(rdata->mr);
3821 rdata->mr = NULL;
3822 }
3823#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08003824 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003825 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003826 trace_smb3_read_err(0 /* xid */,
3827 rdata->cfile->fid.persistent_fid,
3828 tcon->tid, tcon->ses->Suid, rdata->offset,
3829 rdata->bytes, rdata->result);
3830 } else
3831 trace_smb3_read_done(0 /* xid */,
3832 rdata->cfile->fid.persistent_fid,
3833 tcon->tid, tcon->ses->Suid,
3834 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003835
3836 queue_work(cifsiod_wq, &rdata->work);
3837 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003838 add_credits(server, &credits, 0);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003839}
3840
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003841/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003842int
3843smb2_async_readv(struct cifs_readdata *rdata)
3844{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003845 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003846 char *buf;
3847 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003848 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003849 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003850 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003851 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003852 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003853
Joe Perchesf96637b2013-05-04 22:12:25 -05003854 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3855 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003856
3857 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3858 io_parms.offset = rdata->offset;
3859 io_parms.length = rdata->bytes;
3860 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3861 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3862 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003863
3864 server = io_parms.tcon->ses->server;
3865
Long Li2dabfd52017-11-07 01:54:53 -07003866 rc = smb2_new_read_req(
3867 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003868 if (rc)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003869 return rc;
3870
Steve French5a77e752018-05-09 17:43:08 -05003871 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003872 flags |= CIFS_TRANSFORM_REQ;
3873
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003874 rdata->iov[0].iov_base = buf;
3875 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003876
3877 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003878
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003879 if (rdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003880 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003881 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003882 shdr->CreditRequest =
3883 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003884
3885 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3886 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003887 goto async_readv_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003888
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003889 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003890 }
3891
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003892 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07003893 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003894 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003895 smb3_handle_read_data, rdata, flags,
3896 &rdata->credits);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003897 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003898 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003899 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003900 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3901 io_parms.tcon->tid,
3902 io_parms.tcon->ses->Suid,
3903 io_parms.offset, io_parms.length, rc);
3904 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003905
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003906async_readv_out:
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003907 cifs_small_buf_release(buf);
3908 return rc;
3909}
Pavel Shilovsky33319142012-09-18 16:20:29 -07003910
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003911int
3912SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3913 unsigned int *nbytes, char **buf, int *buf_type)
3914{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003915 struct smb_rqst rqst;
Colin Ian King1efd4fc2019-07-31 10:05:26 +01003916 int resp_buftype, rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003917 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003918 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003919 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003920 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003921 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003922 int flags = CIFS_LOG_ERROR;
3923 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003924
3925 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07003926 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003927 if (rc)
3928 return rc;
3929
Steve French5a77e752018-05-09 17:43:08 -05003930 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003931 flags |= CIFS_TRANSFORM_REQ;
3932
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003933 iov[0].iov_base = (char *)req;
3934 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003935
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003936 memset(&rqst, 0, sizeof(struct smb_rqst));
3937 rqst.rq_iov = iov;
3938 rqst.rq_nvec = 1;
3939
3940 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003941 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003942
3943 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003944 if (rc != -ENODATA) {
3945 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3946 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003947 trace_smb3_read_err(xid, req->PersistentFileId,
3948 io_parms->tcon->tid, ses->Suid,
3949 io_parms->offset, io_parms->length,
3950 rc);
Steve Frenchb0a42f22019-02-25 15:02:58 -06003951 } else
3952 trace_smb3_read_done(xid, req->PersistentFileId,
3953 io_parms->tcon->tid, ses->Suid,
3954 io_parms->offset, 0);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003955 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Ronnie Sahlberg05fd5c22019-04-23 16:39:45 +10003956 cifs_small_buf_release(req);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003957 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05003958 } else
3959 trace_smb3_read_done(xid, req->PersistentFileId,
3960 io_parms->tcon->tid, ses->Suid,
3961 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003962
ZhangXiaoxu088aaf12019-04-06 15:47:39 +08003963 cifs_small_buf_release(req);
3964
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003965 *nbytes = le32_to_cpu(rsp->DataLength);
3966 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3967 (*nbytes > io_parms->length)) {
3968 cifs_dbg(FYI, "bad length %d for count %d\n",
3969 *nbytes, io_parms->length);
3970 rc = -EIO;
3971 *nbytes = 0;
3972 }
3973
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003974 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003975 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003976 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003977 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003978 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003979 if (resp_buftype == CIFS_SMALL_BUFFER)
3980 *buf_type = CIFS_SMALL_BUFFER;
3981 else if (resp_buftype == CIFS_LARGE_BUFFER)
3982 *buf_type = CIFS_LARGE_BUFFER;
3983 }
3984 return rc;
3985}
3986
Pavel Shilovsky33319142012-09-18 16:20:29 -07003987/*
3988 * Check the mid_state and signature on received buffer (if any), and queue the
3989 * workqueue completion task.
3990 */
3991static void
3992smb2_writev_callback(struct mid_q_entry *mid)
3993{
3994 struct cifs_writedata *wdata = mid->callback_data;
3995 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003996 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003997 unsigned int written;
3998 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003999 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky33319142012-09-18 16:20:29 -07004000
4001 switch (mid->mid_state) {
4002 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004003 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4004 credits.instance = server->reconnect_instance;
4005 wdata->result = smb2_check_receive(mid, server, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004006 if (wdata->result != 0)
4007 break;
4008
4009 written = le32_to_cpu(rsp->DataLength);
4010 /*
4011 * Mask off high 16 bits when bytes written as returned
4012 * by the server is greater than bytes requested by the
4013 * client. OS/2 servers are known to set incorrect
4014 * CountHigh values.
4015 */
4016 if (written > wdata->bytes)
4017 written &= 0xFFFF;
4018
4019 if (written < wdata->bytes)
4020 wdata->result = -ENOSPC;
4021 else
4022 wdata->bytes = written;
4023 break;
4024 case MID_REQUEST_SUBMITTED:
4025 case MID_RETRY_NEEDED:
4026 wdata->result = -EAGAIN;
4027 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08004028 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004029 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4030 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08004031 /* fall through */
Pavel Shilovsky33319142012-09-18 16:20:29 -07004032 default:
4033 wdata->result = -EIO;
4034 break;
4035 }
Long Lidb223a52017-11-22 17:38:45 -07004036#ifdef CONFIG_CIFS_SMB_DIRECT
4037 /*
4038 * If this wdata has a memory registered, the MR can be freed
4039 * The number of MRs available is limited, it's important to recover
4040 * used MR as soon as I/O is finished. Hold MR longer in the later
4041 * I/O process can possibly result in I/O deadlock due to lack of MR
4042 * to send request on I/O retry
4043 */
4044 if (wdata->mr) {
4045 smbd_deregister_mr(wdata->mr);
4046 wdata->mr = NULL;
4047 }
4048#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004049 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07004050 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004051 trace_smb3_write_err(0 /* no xid */,
4052 wdata->cfile->fid.persistent_fid,
4053 tcon->tid, tcon->ses->Suid, wdata->offset,
4054 wdata->bytes, wdata->result);
Steve Frenchd6fd4192020-02-05 16:52:11 -06004055 if (wdata->result == -ENOSPC)
4056 printk_once(KERN_WARNING "Out of space writing to %s\n",
4057 tcon->treeName);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004058 } else
4059 trace_smb3_write_done(0 /* no xid */,
4060 wdata->cfile->fid.persistent_fid,
4061 tcon->tid, tcon->ses->Suid,
4062 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004063
4064 queue_work(cifsiod_wq, &wdata->work);
4065 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004066 add_credits(server, &credits, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004067}
4068
4069/* smb2_async_writev - send an async write, and set up mid to handle result */
4070int
Steve French4a5c80d2014-02-07 20:45:12 -06004071smb2_async_writev(struct cifs_writedata *wdata,
4072 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07004073{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004074 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004075 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004076 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004077 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004078 struct TCP_Server_Info *server = tcon->ses->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004079 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004080 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004081 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004082
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004083 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08004084 if (rc)
4085 return rc;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004086
Steve French5a77e752018-05-09 17:43:08 -05004087 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004088 flags |= CIFS_TRANSFORM_REQ;
4089
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004090 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004091 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004092
4093 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4094 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4095 req->WriteChannelInfoOffset = 0;
4096 req->WriteChannelInfoLength = 0;
4097 req->Channel = 0;
4098 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004099 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004100 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07004101 req->RemainingBytes = 0;
Steve Frenchd323c2462019-02-25 00:52:43 -06004102
4103 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4104 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07004105#ifdef CONFIG_CIFS_SMB_DIRECT
4106 /*
4107 * If we want to do a server RDMA read, fill in and append
4108 * smbd_buffer_descriptor_v1 to the end of write request
4109 */
Long Libb4c0412018-04-17 12:17:08 -07004110 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07004111 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07004112
Long Lidb223a52017-11-22 17:38:45 -07004113 struct smbd_buffer_descriptor_v1 *v1;
4114 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4115
4116 wdata->mr = smbd_register_mr(
4117 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07004118 wdata->nr_pages, wdata->page_offset,
4119 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07004120 if (!wdata->mr) {
Long Lib7972092019-04-05 21:36:34 +00004121 rc = -EAGAIN;
Long Lidb223a52017-11-22 17:38:45 -07004122 goto async_writev_out;
4123 }
4124 req->Length = 0;
4125 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07004126 if (wdata->nr_pages > 1)
4127 req->RemainingBytes =
4128 cpu_to_le32(
4129 (wdata->nr_pages - 1) * wdata->pagesz -
4130 wdata->page_offset + wdata->tailsz
4131 );
4132 else
4133 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07004134 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4135 if (need_invalidate)
4136 req->Channel = SMB2_CHANNEL_RDMA_V1;
4137 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06004138 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07004139 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06004140 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07004141 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06004142 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4143 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4144 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07004145 }
4146#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004147 iov[0].iov_len = total_len - 1;
4148 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004149
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004150 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004151 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07004152 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07004153 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07004154 rqst.rq_npages = wdata->nr_pages;
4155 rqst.rq_pagesz = wdata->pagesz;
4156 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07004157#ifdef CONFIG_CIFS_SMB_DIRECT
4158 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004159 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07004160 rqst.rq_npages = 0;
4161 }
4162#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05004163 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4164 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004165
Long Lidb223a52017-11-22 17:38:45 -07004166#ifdef CONFIG_CIFS_SMB_DIRECT
4167 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4168 if (!wdata->mr)
4169 req->Length = cpu_to_le32(wdata->bytes);
4170#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07004171 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07004172#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07004173
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004174 if (wdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004175 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004176 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00004177 shdr->CreditRequest =
4178 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004179
4180 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4181 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004182 goto async_writev_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004183
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004184 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004185 }
4186
Pavel Shilovsky33319142012-09-18 16:20:29 -07004187 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08004188 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08004189 wdata, flags, &wdata->credits);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004190
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004191 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004192 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4193 tcon->tid, tcon->ses->Suid, wdata->offset,
4194 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06004195 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004196 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004197 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07004198
Pavel Shilovsky33319142012-09-18 16:20:29 -07004199async_writev_out:
4200 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004201 return rc;
4202}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004203
4204/*
4205 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4206 * The length field from io_parms must be at least 1 and indicates a number of
4207 * elements with data to write that begins with position 1 in iov array. All
4208 * data length is specified by count.
4209 */
4210int
4211SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4212 unsigned int *nbytes, struct kvec *iov, int n_vec)
4213{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004214 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004215 int rc = 0;
4216 struct smb2_write_req *req = NULL;
4217 struct smb2_write_rsp *rsp = NULL;
4218 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004219 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004220 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004221 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004222
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004223 *nbytes = 0;
4224
4225 if (n_vec < 1)
4226 return rc;
4227
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004228 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
4229 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004230 if (rc)
4231 return rc;
4232
4233 if (io_parms->tcon->ses->server == NULL)
4234 return -ECONNABORTED;
4235
Steve French5a77e752018-05-09 17:43:08 -05004236 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004237 flags |= CIFS_TRANSFORM_REQ;
4238
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004239 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004240
4241 req->PersistentFileId = io_parms->persistent_fid;
4242 req->VolatileFileId = io_parms->volatile_fid;
4243 req->WriteChannelInfoOffset = 0;
4244 req->WriteChannelInfoLength = 0;
4245 req->Channel = 0;
4246 req->Length = cpu_to_le32(io_parms->length);
4247 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004248 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004249 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004250 req->RemainingBytes = 0;
4251
Steve Frenchd323c2462019-02-25 00:52:43 -06004252 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4253 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4254 io_parms->offset, io_parms->length);
4255
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004256 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004257 /* 1 for Buffer */
4258 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004259
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004260 memset(&rqst, 0, sizeof(struct smb_rqst));
4261 rqst.rq_iov = iov;
4262 rqst.rq_nvec = n_vec + 1;
4263
4264 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004265 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004266 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004267
4268 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004269 trace_smb3_write_err(xid, req->PersistentFileId,
4270 io_parms->tcon->tid,
4271 io_parms->tcon->ses->Suid,
4272 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004273 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004274 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05004275 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004276 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05004277 trace_smb3_write_done(xid, req->PersistentFileId,
4278 io_parms->tcon->tid,
4279 io_parms->tcon->ses->Suid,
4280 io_parms->offset, *nbytes);
4281 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004282
ZhangXiaoxu6a3eb332019-04-06 15:47:38 +08004283 cifs_small_buf_release(req);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004284 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004285 return rc;
4286}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004287
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004288static unsigned int
4289num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
4290{
4291 int len;
4292 unsigned int entrycount = 0;
4293 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03004294 char *entryptr;
4295 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004296
4297 if (bufstart == NULL)
4298 return 0;
4299
Dan Carpenter56446f22018-09-06 12:48:22 +03004300 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004301
4302 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03004303 if (entryptr + next_offset < entryptr ||
4304 entryptr + next_offset > end_of_buf ||
4305 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004306 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004307 break;
4308 }
4309
Dan Carpenter56446f22018-09-06 12:48:22 +03004310 entryptr = entryptr + next_offset;
4311 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4312
4313 len = le32_to_cpu(dir_info->FileNameLength);
4314 if (entryptr + len < entryptr ||
4315 entryptr + len > end_of_buf ||
4316 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004317 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4318 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004319 break;
4320 }
4321
Dan Carpenter56446f22018-09-06 12:48:22 +03004322 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004323 entrycount++;
4324
Dan Carpenter56446f22018-09-06 12:48:22 +03004325 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004326 if (!next_offset)
4327 break;
4328 }
4329
4330 return entrycount;
4331}
4332
4333/*
4334 * Readdir/FindFirst
4335 */
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004336int SMB2_query_directory_init(const unsigned int xid,
4337 struct cifs_tcon *tcon, struct smb_rqst *rqst,
4338 u64 persistent_fid, u64 volatile_fid,
4339 int index, int info_level)
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004340{
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004341 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004342 struct smb2_query_directory_req *req;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004343 unsigned char *bufptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004344 __le16 asteriks = cpu_to_le16('*');
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004345 unsigned int output_size = CIFSMaxBufSize -
4346 MAX_SMB2_CREATE_RESPONSE_SIZE -
4347 MAX_SMB2_CLOSE_RESPONSE_SIZE;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004348 unsigned int total_len;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004349 struct kvec *iov = rqst->rq_iov;
4350 int len, rc;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004351
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004352 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4353 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004354 if (rc)
4355 return rc;
4356
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004357 switch (info_level) {
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004358 case SMB_FIND_FILE_DIRECTORY_INFO:
4359 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004360 break;
4361 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4362 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004363 break;
4364 default:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004365 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004366 info_level);
4367 return -EINVAL;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004368 }
4369
4370 req->FileIndex = cpu_to_le32(index);
4371 req->PersistentFileId = persistent_fid;
4372 req->VolatileFileId = volatile_fid;
4373
4374 len = 0x2;
4375 bufptr = req->Buffer;
4376 memcpy(bufptr, &asteriks, len);
4377
4378 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004379 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004380 req->FileNameLength = cpu_to_le16(len);
4381 /*
4382 * BB could be 30 bytes or so longer if we used SMB2 specific
4383 * buffer lengths, but this is safe and close enough.
4384 */
4385 output_size = min_t(unsigned int, output_size, server->maxBuf);
4386 output_size = min_t(unsigned int, output_size, 2 << 15);
4387 req->OutputBufferLength = cpu_to_le32(output_size);
4388
4389 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004390 /* 1 for Buffer */
4391 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004392
4393 iov[1].iov_base = (char *)(req->Buffer);
4394 iov[1].iov_len = len;
4395
Steve Frenchd323c2462019-02-25 00:52:43 -06004396 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4397 tcon->ses->Suid, index, output_size);
4398
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004399 return 0;
4400}
4401
4402void SMB2_query_directory_free(struct smb_rqst *rqst)
4403{
4404 if (rqst && rqst->rq_iov) {
4405 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4406 }
4407}
4408
4409int
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004410smb2_parse_query_directory(struct cifs_tcon *tcon,
4411 struct kvec *rsp_iov,
4412 int resp_buftype,
4413 struct cifs_search_info *srch_inf)
4414{
4415 struct smb2_query_directory_rsp *rsp;
4416 size_t info_buf_size;
4417 char *end_of_smb;
4418 int rc;
4419
4420 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4421
4422 switch (srch_inf->info_level) {
4423 case SMB_FIND_FILE_DIRECTORY_INFO:
4424 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4425 break;
4426 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4427 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4428 break;
4429 default:
4430 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4431 srch_inf->info_level);
4432 return -EINVAL;
4433 }
4434
4435 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4436 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4437 info_buf_size);
4438 if (rc)
4439 return rc;
4440
4441 srch_inf->unicode = true;
4442
4443 if (srch_inf->ntwrk_buf_start) {
4444 if (srch_inf->smallBuf)
4445 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4446 else
4447 cifs_buf_release(srch_inf->ntwrk_buf_start);
4448 }
4449 srch_inf->ntwrk_buf_start = (char *)rsp;
4450 srch_inf->srch_entries_start = srch_inf->last_entry =
4451 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4452 end_of_smb = rsp_iov->iov_len + (char *)rsp;
4453 srch_inf->entries_in_buffer =
4454 num_entries(srch_inf->srch_entries_start, end_of_smb,
4455 &srch_inf->last_entry, info_buf_size);
4456 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4457 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4458 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4459 srch_inf->srch_entries_start, srch_inf->last_entry);
4460 if (resp_buftype == CIFS_LARGE_BUFFER)
4461 srch_inf->smallBuf = false;
4462 else if (resp_buftype == CIFS_SMALL_BUFFER)
4463 srch_inf->smallBuf = true;
4464 else
4465 cifs_tcon_dbg(VFS, "illegal search buffer type\n");
4466
4467 return 0;
4468}
4469
4470int
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004471SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4472 u64 persistent_fid, u64 volatile_fid, int index,
4473 struct cifs_search_info *srch_inf)
4474{
4475 struct smb_rqst rqst;
4476 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4477 struct smb2_query_directory_rsp *rsp = NULL;
4478 int resp_buftype = CIFS_NO_BUFFER;
4479 struct kvec rsp_iov;
4480 int rc = 0;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004481 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004482 int flags = 0;
4483
YueHaibingc4985c32020-01-17 10:57:17 +08004484 if (!ses || !(ses->server))
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004485 return -EIO;
4486
4487 if (smb3_encryption_required(tcon))
4488 flags |= CIFS_TRANSFORM_REQ;
4489
4490 memset(&rqst, 0, sizeof(struct smb_rqst));
4491 memset(&iov, 0, sizeof(iov));
4492 rqst.rq_iov = iov;
4493 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4494
4495 rc = SMB2_query_directory_init(xid, tcon, &rqst, persistent_fid,
4496 volatile_fid, index,
4497 srch_inf->info_level);
4498 if (rc)
4499 goto qdir_exit;
4500
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004501 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004502 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004503
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004504 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004505 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004506 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004507 trace_smb3_query_dir_done(xid, persistent_fid,
4508 tcon->tid, tcon->ses->Suid, index, 0);
Pavel Shilovsky52755802014-08-18 20:49:57 +04004509 srch_inf->endOfSearch = true;
4510 rc = 0;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004511 } else {
4512 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4513 tcon->ses->Suid, index, 0, rc);
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08004514 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004515 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004516 goto qdir_exit;
4517 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004518
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004519 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4520 srch_inf);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004521 if (rc) {
4522 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4523 tcon->ses->Suid, index, 0, rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004524 goto qdir_exit;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004525 }
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004526 resp_buftype = CIFS_NO_BUFFER;
4527
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004528 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4529 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004530
4531qdir_exit:
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004532 SMB2_query_directory_free(&rqst);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004533 free_rsp_buf(resp_buftype, rsp);
4534 return rc;
4535}
4536
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004537int
4538SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004539 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004540 u8 info_type, u32 additional_info,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004541 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004542{
4543 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004544 struct kvec *iov = rqst->rq_iov;
4545 unsigned int i, total_len;
4546 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004547
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004548 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004549 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004550 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004551
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004552 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004553 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004554 req->FileInfoClass = info_class;
4555 req->PersistentFileId = persistent_fid;
4556 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004557 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004558
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004559 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004560 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004561 req->BufferLength = cpu_to_le32(*size);
4562
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004563 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004564 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004565
4566 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004567 /* 1 for Buffer */
4568 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004569
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004570 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004571 le32_add_cpu(&req->BufferLength, size[i]);
4572 iov[i].iov_base = (char *)data[i];
4573 iov[i].iov_len = size[i];
4574 }
4575
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004576 return 0;
4577}
4578
4579void
4580SMB2_set_info_free(struct smb_rqst *rqst)
4581{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10004582 if (rqst && rqst->rq_iov)
4583 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004584}
4585
4586static int
4587send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4588 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4589 u8 info_type, u32 additional_info, unsigned int num,
4590 void **data, unsigned int *size)
4591{
4592 struct smb_rqst rqst;
4593 struct smb2_set_info_rsp *rsp = NULL;
4594 struct kvec *iov;
4595 struct kvec rsp_iov;
4596 int rc = 0;
4597 int resp_buftype;
4598 struct cifs_ses *ses = tcon->ses;
4599 int flags = 0;
4600
4601 if (!ses || !(ses->server))
4602 return -EIO;
4603
4604 if (!num)
4605 return -EINVAL;
4606
4607 if (smb3_encryption_required(tcon))
4608 flags |= CIFS_TRANSFORM_REQ;
4609
4610 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4611 if (!iov)
4612 return -ENOMEM;
4613
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004614 memset(&rqst, 0, sizeof(struct smb_rqst));
4615 rqst.rq_iov = iov;
4616 rqst.rq_nvec = num;
4617
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004618 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4619 info_class, info_type, additional_info,
4620 data, size);
4621 if (rc) {
4622 kfree(iov);
4623 return rc;
4624 }
4625
4626
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004627 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004628 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004629 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004630 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004631
Steve Frencheccb4422018-05-17 21:16:55 -05004632 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004633 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004634 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4635 ses->Suid, info_class, (__u32)info_type, rc);
4636 }
Steve French7d3fb242013-11-18 09:56:28 -06004637
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004638 free_rsp_buf(resp_buftype, rsp);
4639 kfree(iov);
4640 return rc;
4641}
4642
4643int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004644SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004645 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004646{
4647 struct smb2_file_eof_info info;
4648 void *data;
4649 unsigned int size;
4650
4651 info.EndOfFile = *eof;
4652
4653 data = &info;
4654 size = sizeof(struct smb2_file_eof_info);
4655
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004656 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004657 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4658 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004659}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004660
4661int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004662SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4663 u64 persistent_fid, u64 volatile_fid,
4664 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4665{
4666 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4667 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4668 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004669}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004670
4671int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004672SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4673 u64 persistent_fid, u64 volatile_fid,
4674 struct smb2_file_full_ea_info *buf, int len)
4675{
4676 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4677 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4678 0, 1, (void **)&buf, &len);
4679}
4680
4681int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004682SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4683 const u64 persistent_fid, const u64 volatile_fid,
4684 __u8 oplock_level)
4685{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004686 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004687 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10004688 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004689 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004690 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004691 unsigned int total_len;
4692 struct kvec iov[1];
4693 struct kvec rsp_iov;
4694 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004695
Joe Perchesf96637b2013-05-04 22:12:25 -05004696 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004697 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4698 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004699 if (rc)
4700 return rc;
4701
Steve French5a77e752018-05-09 17:43:08 -05004702 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004703 flags |= CIFS_TRANSFORM_REQ;
4704
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004705 req->VolatileFid = volatile_fid;
4706 req->PersistentFid = persistent_fid;
4707 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004708 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004709
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004710 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004711
4712 iov[0].iov_base = (char *)req;
4713 iov[0].iov_len = total_len;
4714
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004715 memset(&rqst, 0, sizeof(struct smb_rqst));
4716 rqst.rq_iov = iov;
4717 rqst.rq_nvec = 1;
4718
4719 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004720 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004721
4722 if (rc) {
4723 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004724 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004725 }
4726
4727 return rc;
4728}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004729
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004730void
4731smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4732 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004733{
4734 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4735 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4736 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05304737 kst->f_bfree = kst->f_bavail =
4738 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004739 return;
4740}
4741
Steve French2d304212018-06-24 23:28:12 -05004742static void
4743copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4744 struct kstatfs *kst)
4745{
4746 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4747 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4748 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4749 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4750 kst->f_bavail = kst->f_bfree;
4751 else
4752 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4753 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4754 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4755 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4756 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4757
4758 return;
4759}
Steve French2d304212018-06-24 23:28:12 -05004760
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004761static int
4762build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4763 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4764{
4765 int rc;
4766 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004767 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004768
Joe Perchesf96637b2013-05-04 22:12:25 -05004769 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004770
4771 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4772 return -EIO;
4773
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004774 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4775 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004776 if (rc)
4777 return rc;
4778
4779 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4780 req->FileInfoClass = level;
4781 req->PersistentFileId = persistent_fid;
4782 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004783 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004784 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004785 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004786 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004787 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004788
4789 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004790 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004791 return 0;
4792}
4793
Steve French2d304212018-06-24 23:28:12 -05004794int
4795SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4796 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4797{
4798 struct smb_rqst rqst;
4799 struct smb2_query_info_rsp *rsp = NULL;
4800 struct kvec iov;
4801 struct kvec rsp_iov;
4802 int rc = 0;
4803 int resp_buftype;
4804 struct cifs_ses *ses = tcon->ses;
4805 FILE_SYSTEM_POSIX_INFO *info = NULL;
4806 int flags = 0;
4807
4808 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4809 sizeof(FILE_SYSTEM_POSIX_INFO),
4810 persistent_fid, volatile_fid);
4811 if (rc)
4812 return rc;
4813
4814 if (smb3_encryption_required(tcon))
4815 flags |= CIFS_TRANSFORM_REQ;
4816
4817 memset(&rqst, 0, sizeof(struct smb_rqst));
4818 rqst.rq_iov = &iov;
4819 rqst.rq_nvec = 1;
4820
4821 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4822 cifs_small_buf_release(iov.iov_base);
4823 if (rc) {
4824 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4825 goto posix_qfsinf_exit;
4826 }
4827 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4828
4829 info = (FILE_SYSTEM_POSIX_INFO *)(
4830 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004831 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4832 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4833 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05004834 if (!rc)
4835 copy_posix_fs_info_to_kstatfs(info, fsdata);
4836
4837posix_qfsinf_exit:
4838 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4839 return rc;
4840}
Steve French2d304212018-06-24 23:28:12 -05004841
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004842int
4843SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4844 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4845{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004846 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004847 struct smb2_query_info_rsp *rsp = NULL;
4848 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004849 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004850 int rc = 0;
4851 int resp_buftype;
4852 struct cifs_ses *ses = tcon->ses;
4853 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004854 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004855
4856 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4857 sizeof(struct smb2_fs_full_size_info),
4858 persistent_fid, volatile_fid);
4859 if (rc)
4860 return rc;
4861
Steve French5a77e752018-05-09 17:43:08 -05004862 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004863 flags |= CIFS_TRANSFORM_REQ;
4864
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004865 memset(&rqst, 0, sizeof(struct smb_rqst));
4866 rqst.rq_iov = &iov;
4867 rqst.rq_nvec = 1;
4868
4869 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004870 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004871 if (rc) {
4872 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05004873 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004874 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004875 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004876
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004877 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004878 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004879 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4880 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4881 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004882 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004883 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004884
Steve French34f62642013-10-09 02:07:00 -05004885qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004886 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004887 return rc;
4888}
4889
4890int
4891SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05004892 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05004893{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004894 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05004895 struct smb2_query_info_rsp *rsp = NULL;
4896 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004897 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05004898 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05004899 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05004900 struct cifs_ses *ses = tcon->ses;
4901 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004902 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05004903
Steven French21671142013-10-09 13:36:35 -05004904 if (level == FS_DEVICE_INFORMATION) {
4905 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4906 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4907 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4908 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4909 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004910 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4911 max_len = sizeof(struct smb3_fs_ss_info);
4912 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05004913 } else if (level == FS_VOLUME_INFORMATION) {
4914 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4915 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05004916 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004917 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05004918 return -EINVAL;
4919 }
4920
4921 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05004922 persistent_fid, volatile_fid);
4923 if (rc)
4924 return rc;
4925
Steve French5a77e752018-05-09 17:43:08 -05004926 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004927 flags |= CIFS_TRANSFORM_REQ;
4928
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004929 memset(&rqst, 0, sizeof(struct smb_rqst));
4930 rqst.rq_iov = &iov;
4931 rqst.rq_nvec = 1;
4932
4933 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004934 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004935 if (rc) {
4936 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4937 goto qfsattr_exit;
4938 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004939 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05004940
4941 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4942 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004943 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05004944 if (rc)
4945 goto qfsattr_exit;
4946
4947 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004948 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004949 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05004950 rsp_len, max_len));
4951 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004952 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004953 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004954 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4955 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004956 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004957 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4958 tcon->perf_sector_size =
4959 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05004960 } else if (level == FS_VOLUME_INFORMATION) {
4961 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4962 (offset + (char *)rsp);
4963 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4964 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004965 }
Steve French34f62642013-10-09 02:07:00 -05004966
4967qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004968 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004969 return rc;
4970}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004971
4972int
4973smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4974 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4975 const __u32 num_lock, struct smb2_lock_element *buf)
4976{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004977 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004978 int rc = 0;
4979 struct smb2_lock_req *req = NULL;
4980 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004981 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004982 int resp_buf_type;
4983 unsigned int count;
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004984 int flags = CIFS_NO_RSP_BUF;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004985 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004986
Joe Perchesf96637b2013-05-04 22:12:25 -05004987 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004988
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004989 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004990 if (rc)
4991 return rc;
4992
Steve French5a77e752018-05-09 17:43:08 -05004993 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004994 flags |= CIFS_TRANSFORM_REQ;
4995
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004996 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004997 req->LockCount = cpu_to_le16(num_lock);
4998
4999 req->PersistentFileId = persist_fid;
5000 req->VolatileFileId = volatile_fid;
5001
5002 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005003
5004 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005005 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005006 iov[1].iov_base = (char *)buf;
5007 iov[1].iov_len = count;
5008
5009 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005010
5011 memset(&rqst, 0, sizeof(struct smb_rqst));
5012 rqst.rq_iov = iov;
5013 rqst.rq_nvec = 2;
5014
5015 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005016 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005017 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005018 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005019 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005020 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05005021 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5022 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005023 }
5024
5025 return rc;
5026}
5027
5028int
5029SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5030 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5031 const __u64 length, const __u64 offset, const __u32 lock_flags,
5032 const bool wait)
5033{
5034 struct smb2_lock_element lock;
5035
5036 lock.Offset = cpu_to_le64(offset);
5037 lock.Length = cpu_to_le64(length);
5038 lock.Flags = cpu_to_le32(lock_flags);
5039 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5040 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5041
5042 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5043}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005044
5045int
5046SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5047 __u8 *lease_key, const __le32 lease_state)
5048{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005049 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005050 int rc;
5051 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005052 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005053 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005054 unsigned int total_len;
5055 struct kvec iov[1];
5056 struct kvec rsp_iov;
5057 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05005058 __u64 *please_key_high;
5059 __u64 *please_key_low;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005060
Joe Perchesf96637b2013-05-04 22:12:25 -05005061 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005062 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
5063 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005064 if (rc)
5065 return rc;
5066
Steve French5a77e752018-05-09 17:43:08 -05005067 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005068 flags |= CIFS_TRANSFORM_REQ;
5069
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005070 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005071 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005072 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005073
5074 memcpy(req->LeaseKey, lease_key, 16);
5075 req->LeaseState = lease_state;
5076
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10005077 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005078
5079 iov[0].iov_base = (char *)req;
5080 iov[0].iov_len = total_len;
5081
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005082 memset(&rqst, 0, sizeof(struct smb_rqst));
5083 rqst.rq_iov = iov;
5084 rqst.rq_nvec = 1;
5085
5086 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005087 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005088
Aurelien Apteld339adc2019-01-31 13:46:07 +01005089 please_key_low = (__u64 *)lease_key;
5090 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005091 if (rc) {
5092 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05005093 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5094 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05005095 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05005096 } else
5097 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5098 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005099
5100 return rc;
5101}