blob: 50466062c1da6f46d6941cff0d47cb8e28beea3f [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
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200315 if (--retries)
316 continue;
317
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400318 /*
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
321 * back on-line
322 */
323 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400325 return -EHOSTDOWN;
326 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200327 retries = server->nr_targets;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400328 }
329
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300331 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400332
333 nls_codepage = load_nls_default();
334
335 /*
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
338 */
339 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200340
341 /*
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
345 */
346 if (server->tcpStatus == CifsNeedReconnect) {
347 rc = -EHOSTDOWN;
348 mutex_unlock(&tcon->ses->session_mutex);
349 goto out;
350 }
351
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400352 rc = cifs_negotiate_protocol(0, tcon->ses);
353 if (!rc && tcon->ses->need_reconnect)
354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
355
356 if (rc || !tcon->need_reconnect) {
357 mutex_unlock(&tcon->ses->session_mutex);
358 goto out;
359 }
360
361 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800362 if (tcon->use_persistent)
363 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500364
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200365 rc = __smb2_reconnect(nls_codepage, tcon);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400366 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500367
Joe Perchesf96637b2013-05-04 22:12:25 -0500368 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500369 if (rc) {
370 /* If sess reconnected but tcon didn't, something strange ... */
371 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400372 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500373 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800374
375 if (smb2_command != SMB2_INTERNAL_CMD)
376 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
377
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400378 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400379out:
380 /*
381 * Check if handle based operation so we know whether we can continue
382 * or not without returning to caller to reset file handle.
383 */
384 /*
385 * BB Is flush done by server on drop of tcp session? Should we special
386 * case it and skip above?
387 */
388 switch (smb2_command) {
389 case SMB2_FLUSH:
390 case SMB2_READ:
391 case SMB2_WRITE:
392 case SMB2_LOCK:
393 case SMB2_IOCTL:
394 case SMB2_QUERY_DIRECTORY:
395 case SMB2_CHANGE_NOTIFY:
396 case SMB2_QUERY_INFO:
397 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800398 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400399 }
400 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400401 return rc;
402}
403
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700404static void
405fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
406 unsigned int *total_len)
407{
408 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
409 /* lookup word count ie StructureSize from table */
410 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
411
412 /*
413 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
414 * largest operations (Create)
415 */
416 memset(buf, 0, 256);
417
418 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
419 spdu->StructureSize2 = cpu_to_le16(parmsize);
420
421 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
422}
423
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100424/*
425 * Allocate and return pointer to an SMB request hdr, and set basic
426 * SMB information in the SMB header. If the return code is zero, this
427 * function must have filled in request_buf pointer.
428 */
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300429static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
430 void **request_buf, unsigned int *total_len)
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800431{
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800432 /* BB eventually switch this to SMB2 specific small buf size */
Stefano Briviof46ecbd2018-07-05 11:46:42 +0200433 if (smb2_command == SMB2_SET_INFO)
434 *request_buf = cifs_buf_get();
435 else
436 *request_buf = cifs_small_buf_get();
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800437 if (*request_buf == NULL) {
438 /* BB should we add a retry in here if not a writepage? */
439 return -ENOMEM;
440 }
441
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100442 fill_small_buf(smb2_command, tcon,
443 (struct smb2_sync_hdr *)(*request_buf),
444 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400445
446 if (tcon != NULL) {
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400447 uint16_t com_code = le16_to_cpu(smb2_command);
448 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400449 cifs_stats_inc(&tcon->num_smbs_sent);
450 }
451
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300452 return 0;
453}
454
455static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
456 void **request_buf, unsigned int *total_len)
457{
458 int rc;
459
460 rc = smb2_reconnect(smb2_command, tcon);
461 if (rc)
462 return rc;
463
464 return __smb2_plain_req_init(smb2_command, tcon, request_buf,
465 total_len);
466}
467
468static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
469 void **request_buf, unsigned int *total_len)
470{
471 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
472 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
473 return __smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf,
474 total_len);
475 }
476 return smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf, total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400477}
478
Steve Frenchd7bef4c2019-04-18 11:03:58 -0500479/* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500480
481static void
482build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
483{
484 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
485 pneg_ctxt->DataLength = cpu_to_le16(38);
486 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
487 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
488 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
489 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
490}
491
492static void
Steve French26ea8882019-04-26 20:36:08 -0700493build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
494{
495 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
496 pneg_ctxt->DataLength =
497 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
498 - sizeof(struct smb2_neg_context));
499 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
500 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
501 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
502 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
503}
504
505static void
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500506build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
507{
508 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French9ac63ec2019-06-07 08:59:40 -0500509 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
510 pneg_ctxt->CipherCount = cpu_to_le16(2);
511 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
512 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500513}
514
Steve French96d3cca2019-06-25 04:39:51 -0500515static unsigned int
516build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
517{
518 struct nls_table *cp = load_nls_default();
519
520 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
521
522 /* copy up to max of first 100 bytes of server name to NetName field */
Steve Frenchdf58fae2019-08-05 17:07:26 -0500523 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
Steve French96d3cca2019-06-25 04:39:51 -0500524 /* context size is DataLength + minimal smb2_neg_context */
525 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
526 sizeof(struct smb2_neg_context), 8) * 8;
527}
528
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500529static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500530build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
531{
532 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
533 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
Steve French0d481322019-02-24 17:56:33 -0600534 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
535 pneg_ctxt->Name[0] = 0x93;
536 pneg_ctxt->Name[1] = 0xAD;
537 pneg_ctxt->Name[2] = 0x25;
538 pneg_ctxt->Name[3] = 0x50;
539 pneg_ctxt->Name[4] = 0x9C;
540 pneg_ctxt->Name[5] = 0xB4;
541 pneg_ctxt->Name[6] = 0x11;
542 pneg_ctxt->Name[7] = 0xE7;
543 pneg_ctxt->Name[8] = 0xB4;
544 pneg_ctxt->Name[9] = 0x23;
545 pneg_ctxt->Name[10] = 0x83;
546 pneg_ctxt->Name[11] = 0xDE;
547 pneg_ctxt->Name[12] = 0x96;
548 pneg_ctxt->Name[13] = 0x8B;
549 pneg_ctxt->Name[14] = 0xCD;
550 pneg_ctxt->Name[15] = 0x7C;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500551}
552
553static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100554assemble_neg_contexts(struct smb2_negotiate_req *req,
Steve French9fe5ff12019-06-24 20:39:04 -0500555 struct TCP_Server_Info *server, unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500556{
Colin Ian Kinga9f76cf2019-12-02 18:59:42 +0000557 char *pneg_ctxt;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500558 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500559
Steve Frenchd5c70762019-01-03 02:37:21 -0600560 if (*total_len > 200) {
561 /* In case length corrupted don't want to overrun smb buffer */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000562 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
Steve Frenchd5c70762019-01-03 02:37:21 -0600563 return;
564 }
565
566 /*
567 * round up total_len of fixed part of SMB3 negotiate request to 8
568 * byte boundary before adding negotiate contexts
569 */
570 *total_len = roundup(*total_len, 8);
571
572 pneg_ctxt = (*total_len) + (char *)req;
573 req->NegotiateContextOffset = cpu_to_le32(*total_len);
574
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500575 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500576 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
577 *total_len += ctxt_len;
578 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100579
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500580 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500581 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
582 *total_len += ctxt_len;
583 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100584
Steve French9fe5ff12019-06-24 20:39:04 -0500585 if (server->compress_algorithm) {
586 build_compression_ctxt((struct smb2_compression_capabilities_context *)
Steve French26ea8882019-04-26 20:36:08 -0700587 pneg_ctxt);
Steve French9fe5ff12019-06-24 20:39:04 -0500588 ctxt_len = DIV_ROUND_UP(
589 sizeof(struct smb2_compression_capabilities_context),
590 8) * 8;
591 *total_len += ctxt_len;
592 pneg_ctxt += ctxt_len;
Steve French96d3cca2019-06-25 04:39:51 -0500593 req->NegotiateContextCount = cpu_to_le16(5);
Steve French9fe5ff12019-06-24 20:39:04 -0500594 } else
Steve French96d3cca2019-06-25 04:39:51 -0500595 req->NegotiateContextCount = cpu_to_le16(4);
596
597 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
598 server->hostname);
599 *total_len += ctxt_len;
600 pneg_ctxt += ctxt_len;
601
Steve Frenchfcef0db2018-05-19 20:45:27 -0500602 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
603 *total_len += sizeof(struct smb2_posix_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500604}
Steve French5100d8a2018-04-09 10:47:14 -0500605
606static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
607{
608 unsigned int len = le16_to_cpu(ctxt->DataLength);
609
610 /* If invalid preauth context warn but use what we requested, SHA-512 */
611 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
612 printk_once(KERN_WARNING "server sent bad preauth context\n");
613 return;
614 }
615 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
616 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
617 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
618 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
619}
620
Steve French26ea8882019-04-26 20:36:08 -0700621static void decode_compress_ctx(struct TCP_Server_Info *server,
622 struct smb2_compression_capabilities_context *ctxt)
623{
624 unsigned int len = le16_to_cpu(ctxt->DataLength);
625
626 /* sizeof compress context is a one element compression capbility struct */
627 if (len < 10) {
628 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
629 return;
630 }
631 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
632 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
633 return;
634 }
635 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
636 printk_once(KERN_WARNING "unknown compression algorithm\n");
637 return;
638 }
639 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
640}
641
Steve French5100d8a2018-04-09 10:47:14 -0500642static int decode_encrypt_ctx(struct TCP_Server_Info *server,
643 struct smb2_encryption_neg_context *ctxt)
644{
645 unsigned int len = le16_to_cpu(ctxt->DataLength);
646
647 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
648 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
649 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
650 return -EINVAL;
651 }
652
653 if (le16_to_cpu(ctxt->CipherCount) != 1) {
654 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
655 return -EINVAL;
656 }
657 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
658 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
659 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
660 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
661 return -EINVAL;
662 }
663 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500664 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500665 return 0;
666}
667
668static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000669 struct TCP_Server_Info *server,
670 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500671{
672 struct smb2_neg_context *pctx;
673 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
674 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500675 unsigned int len_of_ctxts, i;
676 int rc = 0;
677
678 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
679 if (len_of_smb <= offset) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000680 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
Steve French5100d8a2018-04-09 10:47:14 -0500681 return -EINVAL;
682 }
683
684 len_of_ctxts = len_of_smb - offset;
685
686 for (i = 0; i < ctxt_cnt; i++) {
687 int clen;
688 /* check that offset is not beyond end of SMB */
689 if (len_of_ctxts == 0)
690 break;
691
692 if (len_of_ctxts < sizeof(struct smb2_neg_context))
693 break;
694
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000695 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500696 clen = le16_to_cpu(pctx->DataLength);
697 if (clen > len_of_ctxts)
698 break;
699
700 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
701 decode_preauth_context(
702 (struct smb2_preauth_neg_context *)pctx);
703 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
704 rc = decode_encrypt_ctx(server,
705 (struct smb2_encryption_neg_context *)pctx);
Steve French26ea8882019-04-26 20:36:08 -0700706 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
707 decode_compress_ctx(server,
708 (struct smb2_compression_capabilities_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500709 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
710 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500711 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000712 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
Steve French5100d8a2018-04-09 10:47:14 -0500713 le16_to_cpu(pctx->ContextType));
714
715 if (rc)
716 break;
717 /* offsets must be 8 byte aligned */
718 clen = (clen + 7) & ~0x7;
719 offset += clen + sizeof(struct smb2_neg_context);
720 len_of_ctxts -= clen;
721 }
722 return rc;
723}
724
Steve Frenchce558b02018-05-31 19:16:54 -0500725static struct create_posix *
726create_posix_buf(umode_t mode)
727{
728 struct create_posix *buf;
729
730 buf = kzalloc(sizeof(struct create_posix),
731 GFP_KERNEL);
732 if (!buf)
733 return NULL;
734
735 buf->ccontext.DataOffset =
736 cpu_to_le16(offsetof(struct create_posix, Mode));
737 buf->ccontext.DataLength = cpu_to_le32(4);
738 buf->ccontext.NameOffset =
739 cpu_to_le16(offsetof(struct create_posix, Name));
740 buf->ccontext.NameLength = cpu_to_le16(16);
741
742 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
743 buf->Name[0] = 0x93;
744 buf->Name[1] = 0xAD;
745 buf->Name[2] = 0x25;
746 buf->Name[3] = 0x50;
747 buf->Name[4] = 0x9C;
748 buf->Name[5] = 0xB4;
749 buf->Name[6] = 0x11;
750 buf->Name[7] = 0xE7;
751 buf->Name[8] = 0xB4;
752 buf->Name[9] = 0x23;
753 buf->Name[10] = 0x83;
754 buf->Name[11] = 0xDE;
755 buf->Name[12] = 0x96;
756 buf->Name[13] = 0x8B;
757 buf->Name[14] = 0xCD;
758 buf->Name[15] = 0x7C;
759 buf->Mode = cpu_to_le32(mode);
760 cifs_dbg(FYI, "mode on posix create 0%o", mode);
761 return buf;
762}
763
764static int
765add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
766{
767 struct smb2_create_req *req = iov[0].iov_base;
768 unsigned int num = *num_iovec;
769
770 iov[num].iov_base = create_posix_buf(mode);
Steve Frenchd0959b02019-10-05 10:53:58 -0500771 if (mode == ACL_NO_MODE)
772 cifs_dbg(FYI, "illegal mode\n");
Steve Frenchce558b02018-05-31 19:16:54 -0500773 if (iov[num].iov_base == NULL)
774 return -ENOMEM;
775 iov[num].iov_len = sizeof(struct create_posix);
776 if (!req->CreateContextsOffset)
777 req->CreateContextsOffset = cpu_to_le32(
778 sizeof(struct smb2_create_req) +
779 iov[num - 1].iov_len);
780 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
781 *num_iovec = num + 1;
782 return 0;
783}
784
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500785
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400786/*
787 *
788 * SMB2 Worker functions follow:
789 *
790 * The general structure of the worker functions is:
791 * 1) Call smb2_init (assembles SMB2 header)
792 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
793 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
794 * 4) Decode SMB2 command specific fields in the fixed length area
795 * 5) Decode variable length data area (if any for this SMB2 command type)
796 * 6) Call free smb buffer
797 * 7) return
798 *
799 */
800
801int
802SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
803{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000804 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400805 struct smb2_negotiate_req *req;
806 struct smb2_negotiate_rsp *rsp;
807 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700808 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400809 int rc = 0;
810 int resp_buftype;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200811 struct TCP_Server_Info *server = cifs_ses_server(ses);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400812 int blob_offset, blob_length;
813 char *security_blob;
814 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100815 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400816
Joe Perchesf96637b2013-05-04 22:12:25 -0500817 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400818
Jeff Layton3534b852013-05-24 07:41:01 -0400819 if (!server) {
820 WARN(1, "%s: server is NULL!\n", __func__);
821 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400822 }
823
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100824 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400825 if (rc)
826 return rc;
827
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100828 req->sync_hdr.SessionId = 0;
Steve French0fdfef92018-06-28 19:30:23 -0500829
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100830 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
831 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400832
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200833 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500834 SMB3ANY_VERSION_STRING) == 0) {
835 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
836 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
837 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100838 total_len += 4;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000839 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500840 SMBDEFAULT_VERSION_STRING) == 0) {
841 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
842 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
843 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600844 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
845 req->DialectCount = cpu_to_le16(4);
846 total_len += 8;
Steve French9764c022017-09-17 10:41:35 -0500847 } else {
848 /* otherwise send specific dialect */
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200849 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
Steve French9764c022017-09-17 10:41:35 -0500850 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100851 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500852 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400853
854 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400855 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500856 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400857 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500858 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400859 else
860 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400861
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000862 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400863
Steve French3c5f9be12014-05-13 13:37:45 -0700864 /* ClientGUID must be zero for SMB2.02 dialect */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000865 if (server->vals->protocol_id == SMB20_PROT_ID)
Steve French3c5f9be12014-05-13 13:37:45 -0700866 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500867 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700868 memcpy(req->ClientGUID, server->client_guid,
869 SMB2_CLIENT_GUID_SIZE);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000870 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
871 (strcmp(server->vals->version_string,
Steve Frenchd5c70762019-01-03 02:37:21 -0600872 SMBDEFAULT_VERSION_STRING) == 0))
Steve French9fe5ff12019-06-24 20:39:04 -0500873 assemble_neg_contexts(req, server, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500874 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400875 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100876 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400877
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000878 memset(&rqst, 0, sizeof(struct smb_rqst));
879 rqst.rq_iov = iov;
880 rqst.rq_nvec = 1;
881
882 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700883 cifs_small_buf_release(req);
884 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400885 /*
886 * No tcon so can't do
887 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
888 */
Steve French7e682f72017-08-31 21:34:24 -0500889 if (rc == -EOPNOTSUPP) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000890 cifs_server_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500891 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500892 " older servers\n");
893 goto neg_exit;
894 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400895 goto neg_exit;
896
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000897 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500898 SMB3ANY_VERSION_STRING) == 0) {
899 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000900 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500901 "SMB2 dialect returned but not requested\n");
902 return -EIO;
903 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000904 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500905 "SMB2.1 dialect returned but not requested\n");
906 return -EIO;
907 }
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000908 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500909 SMBDEFAULT_VERSION_STRING) == 0) {
910 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000911 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500912 "SMB2 dialect returned but not requested\n");
913 return -EIO;
914 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
915 /* ops set to 3.0 by default for default so update */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000916 server->ops = &smb21_operations;
917 server->vals = &smb21_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800918 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000919 server->ops = &smb311_operations;
920 server->vals = &smb311_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800921 }
Steve French590d08d2017-09-19 11:43:47 -0500922 } else if (le16_to_cpu(rsp->DialectRevision) !=
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000923 server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500924 /* if requested single dialect ensure returned dialect matched */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000925 cifs_server_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500926 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500927 return -EIO;
928 }
929
Joe Perchesf96637b2013-05-04 22:12:25 -0500930 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400931
Steve Frenche4aa25e2012-10-01 12:26:22 -0500932 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500933 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500934 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500935 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500936 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500937 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500938 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
939 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600940 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
941 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400942 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000943 cifs_server_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500944 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400945 rc = -EIO;
946 goto neg_exit;
947 }
948 server->dialect = le16_to_cpu(rsp->DialectRevision);
949
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100950 /*
951 * Keep a copy of the hash after negprot. This hash will be
952 * the starting hash value for all sessions made from this
953 * server.
954 */
955 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
956 SMB2_PREAUTH_HASH_SIZE);
Steve French0fdfef92018-06-28 19:30:23 -0500957
Jeff Laytone598d1d82013-05-26 07:00:59 -0400958 /* SMB2 only has an extended negflavor */
959 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400960 /* set it to the maximum buffer size value we can send with 1 credit */
961 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
962 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400963 server->max_read = le32_to_cpu(rsp->MaxReadSize);
964 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400965 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500966 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
967 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
968 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400969 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400970 /* Internal types */
971 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400972
973 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000974 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500975 /*
976 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
977 * for us will be
978 * ses->sectype = RawNTLMSSP;
979 * but for time being this is our only auth choice so doesn't matter.
980 * We just found a server which sets blob length to zero expecting raw.
981 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700982 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500983 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700984 server->sec_ntlmssp = true;
985 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700986
Jeff Layton38d77c52013-05-26 07:01:00 -0400987 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400988 if (rc)
989 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500990 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500991 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500992 if (rc == 1)
993 rc = 0;
994 else if (rc == 0)
995 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400996 }
Steve French5100d8a2018-04-09 10:47:14 -0500997
Steve French5100d8a2018-04-09 10:47:14 -0500998 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
999 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10001000 rc = smb311_decode_neg_context(rsp, server,
1001 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -05001002 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001003 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
Steve French5100d8a2018-04-09 10:47:14 -05001004 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001005neg_exit:
1006 free_rsp_buf(resp_buftype, rsp);
1007 return rc;
1008}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001009
Steve Frenchff1c0382013-11-19 23:44:46 -06001010int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1011{
Long Li2796d302018-04-25 11:30:04 -07001012 int rc;
1013 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001014 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -06001015 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -05001016 u32 inbuflen; /* max of 4 dialects */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001017 struct TCP_Server_Info *server = tcon->ses->server;
Steve Frenchff1c0382013-11-19 23:44:46 -06001018
1019 cifs_dbg(FYI, "validate negotiate\n");
1020
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001021 /* In SMB3.11 preauth integrity supersedes validate negotiate */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001022 if (server->dialect == SMB311_PROT_ID)
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001023 return 0;
1024
Steve Frenchff1c0382013-11-19 23:44:46 -06001025 /*
1026 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -05001027 * can not sign it (ie are not known user). Even if signing is not
1028 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -06001029 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -05001030 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -06001031 */
Steve French0603c962017-09-20 19:57:18 -05001032 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -06001033 return 0; /* validation requires signing */
1034
Steve French0603c962017-09-20 19:57:18 -05001035 if (tcon->ses->user_name == NULL) {
1036 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1037 return 0; /* validation requires signing */
1038 }
1039
1040 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001041 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
Steve French0603c962017-09-20 19:57:18 -05001042
Long Li2796d302018-04-25 11:30:04 -07001043 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1044 if (!pneg_inbuf)
1045 return -ENOMEM;
1046
1047 pneg_inbuf->Capabilities =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001048 cpu_to_le32(server->vals->req_capabilities);
1049 memcpy(pneg_inbuf->Guid, server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +01001050 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -06001051
1052 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -07001053 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001054 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1055 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -07001056 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001057 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1058 else
Long Li2796d302018-04-25 11:30:04 -07001059 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001060
Steve French9764c022017-09-17 10:41:35 -05001061
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001062 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001063 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001064 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1065 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1066 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -05001067 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -07001068 inbuflen = sizeof(*pneg_inbuf) -
Steve Frenchd5c70762019-01-03 02:37:21 -06001069 (2 * sizeof(pneg_inbuf->Dialects[0]));
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001070 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001071 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001072 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1073 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1074 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -06001075 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1076 pneg_inbuf->DialectCount = cpu_to_le16(4);
Steve French9764c022017-09-17 10:41:35 -05001077 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -07001078 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -05001079 } else {
1080 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -07001081 pneg_inbuf->Dialects[0] =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001082 cpu_to_le16(server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -07001083 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -05001084 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -07001085 inbuflen = sizeof(*pneg_inbuf) -
1086 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -05001087 }
Steve Frenchff1c0382013-11-19 23:44:46 -06001088
1089 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1090 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001091 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1092 (char **)&pneg_rsp, &rsplen);
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001093 if (rc == -EOPNOTSUPP) {
1094 /*
1095 * Old Windows versions or Netapp SMB server can return
1096 * not supported error. Client should accept it.
1097 */
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001098 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
Colin Ian King21078202019-05-17 09:12:33 +01001099 rc = 0;
1100 goto out_free_inbuf;
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001101 } else if (rc != 0) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001102 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -07001103 rc = -EIO;
1104 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -06001105 }
1106
Long Li2796d302018-04-25 11:30:04 -07001107 rc = -EIO;
1108 if (rsplen != sizeof(*pneg_rsp)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001109 cifs_tcon_dbg(VFS, "invalid protocol negotiate response size: %d\n",
Steve French7db0a6e2017-05-03 21:12:20 -05001110 rsplen);
1111
1112 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -07001113 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1114 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001115 }
1116
1117 /* check validate negotiate info response matches what we got earlier */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001118 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -06001119 goto vneg_out;
1120
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001121 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
Steve Frenchff1c0382013-11-19 23:44:46 -06001122 goto vneg_out;
1123
1124 /* do not validate server guid because not saved at negprot time yet */
1125
1126 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001127 SMB2_LARGE_FILES) != server->capabilities)
Steve Frenchff1c0382013-11-19 23:44:46 -06001128 goto vneg_out;
1129
1130 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -07001131 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001132 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -07001133 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001134
1135vneg_out:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001136 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -07001137out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001138 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -07001139out_free_inbuf:
1140 kfree(pneg_inbuf);
1141 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -06001142}
1143
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301144enum securityEnum
1145smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1146{
1147 switch (requested) {
1148 case Kerberos:
1149 case RawNTLMSSP:
1150 return requested;
1151 case NTLMv2:
1152 return RawNTLMSSP;
1153 case Unspecified:
1154 if (server->sec_ntlmssp &&
1155 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1156 return RawNTLMSSP;
1157 if ((server->sec_kerberos || server->sec_mskerberos) &&
1158 (global_secflags & CIFSSEC_MAY_KRB5))
1159 return Kerberos;
1160 /* Fallthrough */
1161 default:
1162 return Unspecified;
1163 }
1164}
1165
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001166struct SMB2_sess_data {
1167 unsigned int xid;
1168 struct cifs_ses *ses;
1169 struct nls_table *nls_cp;
1170 void (*func)(struct SMB2_sess_data *);
1171 int result;
1172 u64 previous_session;
1173
1174 /* we will send the SMB in three pieces:
1175 * a fixed length beginning part, an optional
1176 * SPNEGO blob (which can be zero length), and a
1177 * last part which will include the strings
1178 * and rest of bcc area. This allows us to avoid
1179 * a large buffer 17K allocation
1180 */
1181 int buf0_type;
1182 struct kvec iov[2];
1183};
1184
1185static int
1186SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1187{
1188 int rc;
1189 struct cifs_ses *ses = sess_data->ses;
1190 struct smb2_sess_setup_req *req;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001191 struct TCP_Server_Info *server = cifs_ses_server(ses);
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001192 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001193
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001194 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1195 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001196 if (rc)
1197 return rc;
1198
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001199 if (sess_data->ses->binding) {
1200 req->sync_hdr.SessionId = sess_data->ses->Suid;
1201 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1202 req->PreviousSessionId = 0;
1203 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1204 } else {
1205 /* First session, not a reauthenticate */
1206 req->sync_hdr.SessionId = 0;
1207 /*
1208 * if reconnect, we need to send previous sess id
1209 * otherwise it is 0
1210 */
1211 req->PreviousSessionId = sess_data->previous_session;
1212 req->Flags = 0; /* MBZ */
1213 }
Steve Frenchd4090142018-06-13 17:05:58 -05001214
1215 /* enough to enable echos and oplocks and one max size write */
1216 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001217
1218 /* only one of SMB2 signing flags may be set in SMB2 request */
1219 if (server->sign)
1220 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1221 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1222 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1223 else
1224 req->SecurityMode = 0;
1225
Steve French8d330962019-07-25 18:13:10 -05001226#ifdef CONFIG_CIFS_DFS_UPCALL
1227 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1228#else
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001229 req->Capabilities = 0;
Steve French8d330962019-07-25 18:13:10 -05001230#endif /* DFS_UPCALL */
1231
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001232 req->Channel = 0; /* MBZ */
1233
1234 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001235 /* 1 for pad */
1236 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001237 /*
1238 * This variable will be used to clear the buffer
1239 * allocated above in case of any error in the calling function.
1240 */
1241 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1242
1243 return 0;
1244}
1245
1246static void
1247SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1248{
1249 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1250 sess_data->buf0_type = CIFS_NO_BUFFER;
1251}
1252
1253static int
1254SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1255{
1256 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001257 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001258 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001259 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001260
1261 /* Testing shows that buffer offset must be at location of Buffer[0] */
1262 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001263 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001264 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1265
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001266 memset(&rqst, 0, sizeof(struct smb_rqst));
1267 rqst.rq_iov = sess_data->iov;
1268 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001269
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001270 /* BB add code to build os and lm fields */
1271 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1272 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001273 &sess_data->buf0_type,
1274 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001275 cifs_small_buf_release(sess_data->iov[0].iov_base);
1276 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001277
1278 return rc;
1279}
1280
1281static int
1282SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1283{
1284 int rc = 0;
1285 struct cifs_ses *ses = sess_data->ses;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001286 struct TCP_Server_Info *server = cifs_ses_server(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001287
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001288 mutex_lock(&server->srv_mutex);
1289 if (server->ops->generate_signingkey) {
1290 rc = server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001291 if (rc) {
1292 cifs_dbg(FYI,
1293 "SMB3 session key generation failed\n");
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001294 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001295 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001296 }
1297 }
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001298 if (!server->session_estab) {
1299 server->sequence_number = 0x2;
1300 server->session_estab = true;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001301 }
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001302 mutex_unlock(&server->srv_mutex);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001303
1304 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001305 /* keep existing ses state if binding */
1306 if (!ses->binding) {
1307 spin_lock(&GlobalMid_Lock);
1308 ses->status = CifsGood;
1309 ses->need_reconnect = false;
1310 spin_unlock(&GlobalMid_Lock);
1311 }
1312
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001313 return rc;
1314}
1315
1316#ifdef CONFIG_CIFS_UPCALL
1317static void
1318SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1319{
1320 int rc;
1321 struct cifs_ses *ses = sess_data->ses;
1322 struct cifs_spnego_msg *msg;
1323 struct key *spnego_key = NULL;
1324 struct smb2_sess_setup_rsp *rsp = NULL;
1325
1326 rc = SMB2_sess_alloc_buffer(sess_data);
1327 if (rc)
1328 goto out;
1329
1330 spnego_key = cifs_get_spnego_key(ses);
1331 if (IS_ERR(spnego_key)) {
1332 rc = PTR_ERR(spnego_key);
1333 spnego_key = NULL;
1334 goto out;
1335 }
1336
1337 msg = spnego_key->payload.data[0];
1338 /*
1339 * check version field to make sure that cifs.upcall is
1340 * sending us a response in an expected form
1341 */
1342 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1343 cifs_dbg(VFS,
1344 "bad cifs.upcall version. Expected %d got %d",
1345 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1346 rc = -EKEYREJECTED;
1347 goto out_put_spnego_key;
1348 }
1349
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001350 /* keep session key if binding */
1351 if (!ses->binding) {
1352 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1353 GFP_KERNEL);
1354 if (!ses->auth_key.response) {
1355 cifs_dbg(VFS,
1356 "Kerberos can't allocate (%u bytes) memory",
1357 msg->sesskey_len);
1358 rc = -ENOMEM;
1359 goto out_put_spnego_key;
1360 }
1361 ses->auth_key.len = msg->sesskey_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001362 }
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001363
1364 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1365 sess_data->iov[1].iov_len = msg->secblob_len;
1366
1367 rc = SMB2_sess_sendreceive(sess_data);
1368 if (rc)
1369 goto out_put_spnego_key;
1370
1371 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001372 /* keep session id and flags if binding */
1373 if (!ses->binding) {
1374 ses->Suid = rsp->sync_hdr.SessionId;
1375 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1376 }
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001377
1378 rc = SMB2_sess_establish_session(sess_data);
1379out_put_spnego_key:
1380 key_invalidate(spnego_key);
1381 key_put(spnego_key);
1382out:
1383 sess_data->result = rc;
1384 sess_data->func = NULL;
1385 SMB2_sess_free_buffer(sess_data);
1386}
1387#else
1388static void
1389SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1390{
1391 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1392 sess_data->result = -EOPNOTSUPP;
1393 sess_data->func = NULL;
1394}
1395#endif
1396
Sachin Prabhu166cea42016-10-07 19:11:22 +01001397static void
1398SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1399
1400static void
1401SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1402{
1403 int rc;
1404 struct cifs_ses *ses = sess_data->ses;
1405 struct smb2_sess_setup_rsp *rsp = NULL;
1406 char *ntlmssp_blob = NULL;
1407 bool use_spnego = false; /* else use raw ntlmssp */
1408 u16 blob_length = 0;
1409
1410 /*
1411 * If memory allocation is successful, caller of this function
1412 * frees it.
1413 */
1414 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1415 if (!ses->ntlmssp) {
1416 rc = -ENOMEM;
1417 goto out_err;
1418 }
1419 ses->ntlmssp->sesskey_per_smbsess = true;
1420
1421 rc = SMB2_sess_alloc_buffer(sess_data);
1422 if (rc)
1423 goto out_err;
1424
1425 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1426 GFP_KERNEL);
1427 if (ntlmssp_blob == NULL) {
1428 rc = -ENOMEM;
1429 goto out;
1430 }
1431
1432 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1433 if (use_spnego) {
1434 /* BB eventually need to add this */
1435 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1436 rc = -EOPNOTSUPP;
1437 goto out;
1438 } else {
1439 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1440 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1441 }
1442 sess_data->iov[1].iov_base = ntlmssp_blob;
1443 sess_data->iov[1].iov_len = blob_length;
1444
1445 rc = SMB2_sess_sendreceive(sess_data);
1446 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1447
1448 /* If true, rc here is expected and not an error */
1449 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001450 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001451 rc = 0;
1452
1453 if (rc)
1454 goto out;
1455
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001456 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001457 le16_to_cpu(rsp->SecurityBufferOffset)) {
1458 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1459 le16_to_cpu(rsp->SecurityBufferOffset));
1460 rc = -EIO;
1461 goto out;
1462 }
1463 rc = decode_ntlmssp_challenge(rsp->Buffer,
1464 le16_to_cpu(rsp->SecurityBufferLength), ses);
1465 if (rc)
1466 goto out;
1467
1468 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1469
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001470 /* keep existing ses id and flags if binding */
1471 if (!ses->binding) {
1472 ses->Suid = rsp->sync_hdr.SessionId;
1473 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1474 }
Sachin Prabhu166cea42016-10-07 19:11:22 +01001475
1476out:
1477 kfree(ntlmssp_blob);
1478 SMB2_sess_free_buffer(sess_data);
1479 if (!rc) {
1480 sess_data->result = 0;
1481 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1482 return;
1483 }
1484out_err:
1485 kfree(ses->ntlmssp);
1486 ses->ntlmssp = NULL;
1487 sess_data->result = rc;
1488 sess_data->func = NULL;
1489}
1490
1491static void
1492SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1493{
1494 int rc;
1495 struct cifs_ses *ses = sess_data->ses;
1496 struct smb2_sess_setup_req *req;
1497 struct smb2_sess_setup_rsp *rsp = NULL;
1498 unsigned char *ntlmssp_blob = NULL;
1499 bool use_spnego = false; /* else use raw ntlmssp */
1500 u16 blob_length = 0;
1501
1502 rc = SMB2_sess_alloc_buffer(sess_data);
1503 if (rc)
1504 goto out;
1505
1506 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001507 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001508
1509 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1510 sess_data->nls_cp);
1511 if (rc) {
1512 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1513 goto out;
1514 }
1515
1516 if (use_spnego) {
1517 /* BB eventually need to add this */
1518 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1519 rc = -EOPNOTSUPP;
1520 goto out;
1521 }
1522 sess_data->iov[1].iov_base = ntlmssp_blob;
1523 sess_data->iov[1].iov_len = blob_length;
1524
1525 rc = SMB2_sess_sendreceive(sess_data);
1526 if (rc)
1527 goto out;
1528
1529 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1530
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001531 /* keep existing ses id and flags if binding */
1532 if (!ses->binding) {
1533 ses->Suid = rsp->sync_hdr.SessionId;
1534 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1535 }
Sachin Prabhu166cea42016-10-07 19:11:22 +01001536
1537 rc = SMB2_sess_establish_session(sess_data);
1538out:
1539 kfree(ntlmssp_blob);
1540 SMB2_sess_free_buffer(sess_data);
1541 kfree(ses->ntlmssp);
1542 ses->ntlmssp = NULL;
1543 sess_data->result = rc;
1544 sess_data->func = NULL;
1545}
1546
1547static int
1548SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1549{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301550 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001551
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001552 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301553 cifs_dbg(FYI, "sess setup type %d\n", type);
1554 if (type == Unspecified) {
1555 cifs_dbg(VFS,
1556 "Unable to select appropriate authentication method!");
1557 return -EINVAL;
1558 }
1559
1560 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001561 case Kerberos:
1562 sess_data->func = SMB2_auth_kerberos;
1563 break;
1564 case RawNTLMSSP:
1565 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1566 break;
1567 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301568 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001569 return -EOPNOTSUPP;
1570 }
1571
1572 return 0;
1573}
1574
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001575int
1576SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1577 const struct nls_table *nls_cp)
1578{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001579 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001580 struct TCP_Server_Info *server = cifs_ses_server(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001581 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001582
Joe Perchesf96637b2013-05-04 22:12:25 -05001583 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001584
Jeff Layton3534b852013-05-24 07:41:01 -04001585 if (!server) {
1586 WARN(1, "%s: server is NULL!\n", __func__);
1587 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001588 }
1589
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001590 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1591 if (!sess_data)
1592 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001593
1594 rc = SMB2_select_sec(ses, sess_data);
1595 if (rc)
1596 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001597 sess_data->xid = xid;
1598 sess_data->ses = ses;
1599 sess_data->buf0_type = CIFS_NO_BUFFER;
1600 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001601 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001602
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001603 /*
1604 * Initialize the session hash with the server one.
1605 */
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001606 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001607 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001608
Sachin Prabhu166cea42016-10-07 19:11:22 +01001609 while (sess_data->func)
1610 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001611
Steve Frenchc721c382017-09-19 18:40:03 -05001612 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001613 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001614 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001615out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001616 kfree(sess_data);
1617 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001618}
1619
1620int
1621SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1622{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001623 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001624 struct smb2_logoff_req *req; /* response is also trivial struct */
1625 int rc = 0;
1626 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001627 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001628 unsigned int total_len;
1629 struct kvec iov[1];
1630 struct kvec rsp_iov;
1631 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001632
Joe Perchesf96637b2013-05-04 22:12:25 -05001633 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001634
1635 if (ses && (ses->server))
1636 server = ses->server;
1637 else
1638 return -EIO;
1639
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001640 /* no need to send SMB logoff if uid already closed due to reconnect */
1641 if (ses->need_reconnect)
1642 goto smb2_session_already_dead;
1643
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001644 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001645 if (rc)
1646 return rc;
1647
1648 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001649 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001650
1651 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1652 flags |= CIFS_TRANSFORM_REQ;
1653 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001654 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001655
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001656 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001657
1658 iov[0].iov_base = (char *)req;
1659 iov[0].iov_len = total_len;
1660
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001661 memset(&rqst, 0, sizeof(struct smb_rqst));
1662 rqst.rq_iov = iov;
1663 rqst.rq_nvec = 1;
1664
1665 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001666 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001667 /*
1668 * No tcon so can't do
1669 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1670 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001671
1672smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001673 return rc;
1674}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001675
1676static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1677{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001678 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001679}
1680
1681#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1682
Steve Frenchde9f68df2013-11-15 11:26:24 -06001683/* These are similar values to what Windows uses */
1684static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1685{
1686 tcon->max_chunks = 256;
1687 tcon->max_bytes_chunk = 1048576;
1688 tcon->max_bytes_copy = 16777216;
1689}
1690
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001691int
1692SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1693 struct cifs_tcon *tcon, const struct nls_table *cp)
1694{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001695 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001696 struct smb2_tree_connect_req *req;
1697 struct smb2_tree_connect_rsp *rsp = NULL;
1698 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001699 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001700 int rc = 0;
1701 int resp_buftype;
1702 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001703 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001704 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001705 unsigned int total_len;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001706 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001707
Joe Perchesf96637b2013-05-04 22:12:25 -05001708 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001709
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001710 if (!server || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001711 return -EIO;
1712
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001713 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1714 if (unc_path == NULL)
1715 return -ENOMEM;
1716
1717 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1718 unc_path_len *= 2;
1719 if (unc_path_len < 2) {
1720 kfree(unc_path);
1721 return -EINVAL;
1722 }
1723
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001724 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001725 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001726 atomic_set(&tcon->num_remote_opens, 0);
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001727 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1728 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001729 if (rc) {
1730 kfree(unc_path);
1731 return rc;
1732 }
1733
Steve French5a77e752018-05-09 17:43:08 -05001734 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001735 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001736
1737 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001738 /* 1 for pad */
1739 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001740
1741 /* Testing shows that buffer offset must be at location of Buffer[0] */
1742 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001743 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001744 req->PathLength = cpu_to_le16(unc_path_len - 2);
1745 iov[1].iov_base = unc_path;
1746 iov[1].iov_len = unc_path_len;
1747
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001748 /*
1749 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1750 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
Steve French8c11a602019-03-22 22:31:17 -05001751 * (Samba servers don't always set the flag so also check if null user)
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001752 */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001753 if ((server->dialect == SMB311_PROT_ID) &&
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001754 !smb3_encryption_required(tcon) &&
Steve French8c11a602019-03-22 22:31:17 -05001755 !(ses->session_flags &
1756 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1757 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
Steve French6188f282018-03-13 02:29:36 -05001758 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1759
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001760 memset(&rqst, 0, sizeof(struct smb_rqst));
1761 rqst.rq_iov = iov;
1762 rqst.rq_nvec = 2;
1763
Steve French4fe75c42019-02-14 01:19:02 -06001764 /* Need 64 for max size write so ask for more in case not there yet */
1765 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1766
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001767 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001768 cifs_small_buf_release(req);
1769 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001770 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001771 if (rc != 0) {
1772 if (tcon) {
1773 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1774 tcon->need_reconnect = true;
1775 }
1776 goto tcon_error_exit;
1777 }
1778
Christophe JAILLETcd123002017-05-12 17:59:32 +02001779 switch (rsp->ShareType) {
1780 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001781 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001782 break;
1783 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001784 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001785 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001786 break;
1787 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001788 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001789 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001790 break;
1791 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001792 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001793 rc = -EOPNOTSUPP;
1794 goto tcon_error_exit;
1795 }
1796
1797 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001798 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001799 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1800 tcon->tidStatus = CifsGood;
1801 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001802 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001803 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001804
1805 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1806 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001807 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001808
1809 if (tcon->seal &&
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001810 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001811 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001812
Steve Frenchde9f68df2013-11-15 11:26:24 -06001813 init_copy_chunk_defaults(tcon);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001814 if (server->ops->validate_negotiate)
1815 rc = server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001816tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001817
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001818 free_rsp_buf(resp_buftype, rsp);
1819 kfree(unc_path);
1820 return rc;
1821
1822tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001823 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001824 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001825 }
1826 goto tcon_exit;
1827}
1828
1829int
1830SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1831{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001832 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001833 struct smb2_tree_disconnect_req *req; /* response is trivial */
1834 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001835 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001836 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001837 unsigned int total_len;
1838 struct kvec iov[1];
1839 struct kvec rsp_iov;
1840 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001841
Joe Perchesf96637b2013-05-04 22:12:25 -05001842 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001843
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001844 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001845 return -EIO;
1846
1847 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1848 return 0;
1849
Pavel Shilovskyd9191312019-12-10 11:44:52 -08001850 close_shroot_lease(&tcon->crfid);
Ronnie Sahlberg72e73c72019-11-07 17:00:38 +10001851
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001852 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1853 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001854 if (rc)
1855 return rc;
1856
Steve French5a77e752018-05-09 17:43:08 -05001857 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001858 flags |= CIFS_TRANSFORM_REQ;
1859
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001860 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001861
1862 iov[0].iov_base = (char *)req;
1863 iov[0].iov_len = total_len;
1864
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001865 memset(&rqst, 0, sizeof(struct smb_rqst));
1866 rqst.rq_iov = iov;
1867 rqst.rq_nvec = 1;
1868
1869 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001870 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001871 if (rc)
1872 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1873
1874 return rc;
1875}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001876
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001877
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001878static struct create_durable *
1879create_durable_buf(void)
1880{
1881 struct create_durable *buf;
1882
1883 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1884 if (!buf)
1885 return NULL;
1886
1887 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001888 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001889 buf->ccontext.DataLength = cpu_to_le32(16);
1890 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1891 (struct create_durable, Name));
1892 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001893 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001894 buf->Name[0] = 'D';
1895 buf->Name[1] = 'H';
1896 buf->Name[2] = 'n';
1897 buf->Name[3] = 'Q';
1898 return buf;
1899}
1900
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001901static struct create_durable *
1902create_reconnect_durable_buf(struct cifs_fid *fid)
1903{
1904 struct create_durable *buf;
1905
1906 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1907 if (!buf)
1908 return NULL;
1909
1910 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1911 (struct create_durable, Data));
1912 buf->ccontext.DataLength = cpu_to_le32(16);
1913 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1914 (struct create_durable, Name));
1915 buf->ccontext.NameLength = cpu_to_le16(4);
1916 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1917 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001918 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001919 buf->Name[0] = 'D';
1920 buf->Name[1] = 'H';
1921 buf->Name[2] = 'n';
1922 buf->Name[3] = 'C';
1923 return buf;
1924}
1925
Steve French89a5bfa2019-07-18 17:22:18 -05001926static void
1927parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1928{
1929 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1930
1931 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1932 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1933 buf->IndexNumber = pdisk_id->DiskFileId;
1934}
1935
1936void
1937smb2_parse_contexts(struct TCP_Server_Info *server,
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +10001938 struct smb2_create_rsp *rsp,
Steve French89a5bfa2019-07-18 17:22:18 -05001939 unsigned int *epoch, char *lease_key, __u8 *oplock,
1940 struct smb2_file_all_info *buf)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001941{
1942 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001943 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001944 unsigned int next;
1945 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001946 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001947
Steve French89a5bfa2019-07-18 17:22:18 -05001948 *oplock = 0;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001949 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001950 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001951 cc = (struct create_context *)data_offset;
Steve French89a5bfa2019-07-18 17:22:18 -05001952
1953 /* Initialize inode number to 0 in case no valid data in qfid context */
1954 if (buf)
1955 buf->IndexNumber = 0;
1956
Justin Maggarddeb7def2016-02-09 15:52:08 -08001957 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001958 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001959 if (le16_to_cpu(cc->NameLength) == 4 &&
Steve French89a5bfa2019-07-18 17:22:18 -05001960 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1961 *oplock = server->ops->parse_lease_buf(cc, epoch,
1962 lease_key);
1963 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1964 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1965 parse_query_id_ctxt(cc, buf);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001966
1967 next = le32_to_cpu(cc->Next);
1968 if (!next)
1969 break;
1970 remaining -= next;
1971 cc = (struct create_context *)((char *)cc + next);
1972 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001973
Steve French89a5bfa2019-07-18 17:22:18 -05001974 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1975 *oplock = rsp->OplockLevel;
1976
1977 return;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001978}
1979
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001980static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001981add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02001982 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001983{
1984 struct smb2_create_req *req = iov[0].iov_base;
1985 unsigned int num = *num_iovec;
1986
Stefano Brivio729c0c92018-07-05 15:10:02 +02001987 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001988 if (iov[num].iov_base == NULL)
1989 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001990 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001991 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1992 if (!req->CreateContextsOffset)
1993 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001994 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001995 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001996 le32_add_cpu(&req->CreateContextsLength,
1997 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001998 *num_iovec = num + 1;
1999 return 0;
2000}
2001
Steve Frenchb56eae42015-11-03 09:26:27 -06002002static struct create_durable_v2 *
Steve Frenchca567eb2019-03-29 16:31:07 -05002003create_durable_v2_buf(struct cifs_open_parms *oparms)
Steve Frenchb56eae42015-11-03 09:26:27 -06002004{
Steve Frenchca567eb2019-03-29 16:31:07 -05002005 struct cifs_fid *pfid = oparms->fid;
Steve Frenchb56eae42015-11-03 09:26:27 -06002006 struct create_durable_v2 *buf;
2007
2008 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2009 if (!buf)
2010 return NULL;
2011
2012 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2013 (struct create_durable_v2, dcontext));
2014 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2015 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2016 (struct create_durable_v2, Name));
2017 buf->ccontext.NameLength = cpu_to_le16(4);
2018
Steve Frenchca567eb2019-03-29 16:31:07 -05002019 /*
2020 * NB: Handle timeout defaults to 0, which allows server to choose
2021 * (most servers default to 120 seconds) and most clients default to 0.
2022 * This can be overridden at mount ("handletimeout=") if the user wants
2023 * a different persistent (or resilient) handle timeout for all opens
2024 * opens on a particular SMB3 mount.
2025 */
2026 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
Steve Frenchb56eae42015-11-03 09:26:27 -06002027 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05002028 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06002029 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2030
2031 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2032 buf->Name[0] = 'D';
2033 buf->Name[1] = 'H';
2034 buf->Name[2] = '2';
2035 buf->Name[3] = 'Q';
2036 return buf;
2037}
2038
2039static struct create_durable_handle_reconnect_v2 *
2040create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2041{
2042 struct create_durable_handle_reconnect_v2 *buf;
2043
2044 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2045 GFP_KERNEL);
2046 if (!buf)
2047 return NULL;
2048
2049 buf->ccontext.DataOffset =
2050 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2051 dcontext));
2052 buf->ccontext.DataLength =
2053 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2054 buf->ccontext.NameOffset =
2055 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2056 Name));
2057 buf->ccontext.NameLength = cpu_to_le16(4);
2058
2059 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2060 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2061 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2062 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2063
2064 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2065 buf->Name[0] = 'D';
2066 buf->Name[1] = 'H';
2067 buf->Name[2] = '2';
2068 buf->Name[3] = 'C';
2069 return buf;
2070}
2071
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002072static int
Steve Frenchb56eae42015-11-03 09:26:27 -06002073add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002074 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002075{
2076 struct smb2_create_req *req = iov[0].iov_base;
2077 unsigned int num = *num_iovec;
2078
Steve Frenchca567eb2019-03-29 16:31:07 -05002079 iov[num].iov_base = create_durable_v2_buf(oparms);
Steve Frenchb56eae42015-11-03 09:26:27 -06002080 if (iov[num].iov_base == NULL)
2081 return -ENOMEM;
2082 iov[num].iov_len = sizeof(struct create_durable_v2);
2083 if (!req->CreateContextsOffset)
2084 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002085 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002086 iov[1].iov_len);
2087 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002088 *num_iovec = num + 1;
2089 return 0;
2090}
2091
2092static int
2093add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2094 struct cifs_open_parms *oparms)
2095{
2096 struct smb2_create_req *req = iov[0].iov_base;
2097 unsigned int num = *num_iovec;
2098
2099 /* indicate that we don't need to relock the file */
2100 oparms->reconnect = false;
2101
2102 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2103 if (iov[num].iov_base == NULL)
2104 return -ENOMEM;
2105 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2106 if (!req->CreateContextsOffset)
2107 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002108 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002109 iov[1].iov_len);
2110 le32_add_cpu(&req->CreateContextsLength,
2111 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002112 *num_iovec = num + 1;
2113 return 0;
2114}
2115
2116static int
2117add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2118 struct cifs_open_parms *oparms, bool use_persistent)
2119{
2120 struct smb2_create_req *req = iov[0].iov_base;
2121 unsigned int num = *num_iovec;
2122
2123 if (use_persistent) {
2124 if (oparms->reconnect)
2125 return add_durable_reconnect_v2_context(iov, num_iovec,
2126 oparms);
2127 else
2128 return add_durable_v2_context(iov, num_iovec, oparms);
2129 }
2130
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002131 if (oparms->reconnect) {
2132 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2133 /* indicate that we don't need to relock the file */
2134 oparms->reconnect = false;
2135 } else
2136 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002137 if (iov[num].iov_base == NULL)
2138 return -ENOMEM;
2139 iov[num].iov_len = sizeof(struct create_durable);
2140 if (!req->CreateContextsOffset)
2141 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002142 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002143 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08002144 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002145 *num_iovec = num + 1;
2146 return 0;
2147}
2148
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002149/* See MS-SMB2 2.2.13.2.7 */
2150static struct crt_twarp_ctxt *
2151create_twarp_buf(__u64 timewarp)
2152{
2153 struct crt_twarp_ctxt *buf;
2154
2155 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2156 if (!buf)
2157 return NULL;
2158
2159 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2160 (struct crt_twarp_ctxt, Timestamp));
2161 buf->ccontext.DataLength = cpu_to_le32(8);
2162 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2163 (struct crt_twarp_ctxt, Name));
2164 buf->ccontext.NameLength = cpu_to_le16(4);
2165 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2166 buf->Name[0] = 'T';
2167 buf->Name[1] = 'W';
2168 buf->Name[2] = 'r';
2169 buf->Name[3] = 'p';
2170 buf->Timestamp = cpu_to_le64(timewarp);
2171 return buf;
2172}
2173
2174/* See MS-SMB2 2.2.13.2.7 */
2175static int
2176add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2177{
2178 struct smb2_create_req *req = iov[0].iov_base;
2179 unsigned int num = *num_iovec;
2180
2181 iov[num].iov_base = create_twarp_buf(timewarp);
2182 if (iov[num].iov_base == NULL)
2183 return -ENOMEM;
2184 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2185 if (!req->CreateContextsOffset)
2186 req->CreateContextsOffset = cpu_to_le32(
2187 sizeof(struct smb2_create_req) +
2188 iov[num - 1].iov_len);
2189 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2190 *num_iovec = num + 1;
2191 return 0;
2192}
2193
Steve Frenchfdef6652019-12-06 02:02:38 -06002194/* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2195static struct crt_sd_ctxt *
2196create_sd_buf(umode_t mode, unsigned int *len)
2197{
2198 struct crt_sd_ctxt *buf;
2199 struct cifs_ace *pace;
2200 unsigned int sdlen, acelen;
2201
2202 *len = roundup(sizeof(struct crt_sd_ctxt) + sizeof(struct cifs_ace), 8);
2203 buf = kzalloc(*len, GFP_KERNEL);
2204 if (buf == NULL)
2205 return buf;
2206
2207 sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) +
2208 sizeof(struct cifs_ace);
2209
2210 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2211 (struct crt_sd_ctxt, sd));
2212 buf->ccontext.DataLength = cpu_to_le32(sdlen);
2213 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2214 (struct crt_sd_ctxt, Name));
2215 buf->ccontext.NameLength = cpu_to_le16(4);
2216 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2217 buf->Name[0] = 'S';
2218 buf->Name[1] = 'e';
2219 buf->Name[2] = 'c';
2220 buf->Name[3] = 'D';
2221 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2222 /*
2223 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2224 * and "DP" ie the DACL is present
2225 */
2226 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2227
2228 /* offset owner, group and Sbz1 and SACL are all zero */
2229 buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd));
2230 buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2231
2232 /* create one ACE to hold the mode embedded in reserved special SID */
2233 pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf);
2234 acelen = setup_special_mode_ACE(pace, (__u64)mode);
2235 buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen);
2236 buf->acl.AceCount = cpu_to_le16(1);
2237 return buf;
2238}
2239
2240static int
2241add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
2242{
2243 struct smb2_create_req *req = iov[0].iov_base;
2244 unsigned int num = *num_iovec;
2245 unsigned int len = 0;
2246
2247 iov[num].iov_base = create_sd_buf(mode, &len);
2248 if (iov[num].iov_base == NULL)
2249 return -ENOMEM;
2250 iov[num].iov_len = len;
2251 if (!req->CreateContextsOffset)
2252 req->CreateContextsOffset = cpu_to_le32(
2253 sizeof(struct smb2_create_req) +
2254 iov[num - 1].iov_len);
2255 le32_add_cpu(&req->CreateContextsLength, len);
2256 *num_iovec = num + 1;
2257 return 0;
2258}
2259
Steve Frenchff2a09e2019-07-06 14:41:38 -05002260static struct crt_query_id_ctxt *
2261create_query_id_buf(void)
2262{
2263 struct crt_query_id_ctxt *buf;
2264
2265 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2266 if (!buf)
2267 return NULL;
2268
2269 buf->ccontext.DataOffset = cpu_to_le16(0);
2270 buf->ccontext.DataLength = cpu_to_le32(0);
2271 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2272 (struct crt_query_id_ctxt, Name));
2273 buf->ccontext.NameLength = cpu_to_le16(4);
2274 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2275 buf->Name[0] = 'Q';
2276 buf->Name[1] = 'F';
2277 buf->Name[2] = 'i';
2278 buf->Name[3] = 'd';
2279 return buf;
2280}
2281
2282/* See MS-SMB2 2.2.13.2.9 */
2283static int
2284add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2285{
2286 struct smb2_create_req *req = iov[0].iov_base;
2287 unsigned int num = *num_iovec;
2288
2289 iov[num].iov_base = create_query_id_buf();
2290 if (iov[num].iov_base == NULL)
2291 return -ENOMEM;
2292 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2293 if (!req->CreateContextsOffset)
2294 req->CreateContextsOffset = cpu_to_le32(
2295 sizeof(struct smb2_create_req) +
2296 iov[num - 1].iov_len);
2297 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2298 *num_iovec = num + 1;
2299 return 0;
2300}
2301
Aurelien Aptelf0712922017-02-22 14:47:17 +01002302static int
2303alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2304 const char *treename, const __le16 *path)
2305{
2306 int treename_len, path_len;
2307 struct nls_table *cp;
2308 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2309
2310 /*
2311 * skip leading "\\"
2312 */
2313 treename_len = strlen(treename);
2314 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2315 return -EINVAL;
2316
2317 treename += 2;
2318 treename_len -= 2;
2319
2320 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2321
2322 /*
2323 * make room for one path separator between the treename and
2324 * path
2325 */
2326 *out_len = treename_len + 1 + path_len;
2327
2328 /*
2329 * final path needs to be null-terminated UTF16 with a
2330 * size aligned to 8
2331 */
2332
2333 *out_size = roundup((*out_len+1)*2, 8);
2334 *out_path = kzalloc(*out_size, GFP_KERNEL);
2335 if (!*out_path)
2336 return -ENOMEM;
2337
2338 cp = load_nls_default();
2339 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2340 UniStrcat(*out_path, sep);
2341 UniStrcat(*out_path, path);
2342 unload_nls(cp);
2343
2344 return 0;
2345}
2346
Steve Frenchbea851b2018-06-14 21:56:32 -05002347int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2348 umode_t mode, struct cifs_tcon *tcon,
2349 const char *full_path,
2350 struct cifs_sb_info *cifs_sb)
2351{
2352 struct smb_rqst rqst;
2353 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002354 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002355 struct cifs_ses *ses = tcon->ses;
2356 struct kvec iov[3]; /* make sure at least one for each open context */
2357 struct kvec rsp_iov = {NULL, 0};
2358 int resp_buftype;
2359 int uni_path_len;
2360 __le16 *copy_path = NULL;
2361 int copy_size;
2362 int rc = 0;
2363 unsigned int n_iov = 2;
2364 __u32 file_attributes = 0;
2365 char *pc_buf = NULL;
2366 int flags = 0;
2367 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002368 __le16 *utf16_path = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002369
2370 cifs_dbg(FYI, "mkdir\n");
2371
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002372 /* resource #1: path allocation */
2373 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2374 if (!utf16_path)
2375 return -ENOMEM;
2376
YueHaibing29cbfa12018-12-18 02:51:51 +00002377 if (!ses || !(ses->server)) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002378 rc = -EIO;
2379 goto err_free_path;
2380 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002381
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002382 /* resource #2: request */
Steve Frenchbea851b2018-06-14 21:56:32 -05002383 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002384 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002385 goto err_free_path;
2386
Steve Frenchbea851b2018-06-14 21:56:32 -05002387
2388 if (smb3_encryption_required(tcon))
2389 flags |= CIFS_TRANSFORM_REQ;
2390
Steve Frenchbea851b2018-06-14 21:56:32 -05002391 req->ImpersonationLevel = IL_IMPERSONATION;
2392 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2393 /* File attributes ignored on open (used in create though) */
2394 req->FileAttributes = cpu_to_le32(file_attributes);
2395 req->ShareAccess = FILE_SHARE_ALL_LE;
2396 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2397 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2398
2399 iov[0].iov_base = (char *)req;
2400 /* -1 since last byte is buf[0] which is sent below (path) */
2401 iov[0].iov_len = total_len - 1;
2402
2403 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2404
2405 /* [MS-SMB2] 2.2.13 NameOffset:
2406 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2407 * the SMB2 header, the file name includes a prefix that will
2408 * be processed during DFS name normalization as specified in
2409 * section 3.3.5.9. Otherwise, the file name is relative to
2410 * the share that is identified by the TreeId in the SMB2
2411 * header.
2412 */
2413 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2414 int name_len;
2415
2416 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2417 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2418 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002419 tcon->treeName, utf16_path);
2420 if (rc)
2421 goto err_free_req;
2422
Steve Frenchbea851b2018-06-14 21:56:32 -05002423 req->NameLength = cpu_to_le16(name_len * 2);
2424 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002425 /* free before overwriting resource */
2426 kfree(utf16_path);
2427 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002428 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002429 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002430 /* MUST set path len (NameLength) to 0 opening root of share */
2431 req->NameLength = cpu_to_le16(uni_path_len - 2);
2432 if (uni_path_len % 8 != 0) {
2433 copy_size = roundup(uni_path_len, 8);
2434 copy_path = kzalloc(copy_size, GFP_KERNEL);
2435 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002436 rc = -ENOMEM;
2437 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002438 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002439 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002440 uni_path_len);
2441 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002442 /* free before overwriting resource */
2443 kfree(utf16_path);
2444 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002445 }
2446 }
2447
2448 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002449 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002450 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2451
2452 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002453 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002454 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002455 if (rc)
2456 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002457 pc_buf = iov[n_iov-1].iov_base;
2458 }
2459
2460
2461 memset(&rqst, 0, sizeof(struct smb_rqst));
2462 rqst.rq_iov = iov;
2463 rqst.rq_nvec = n_iov;
2464
Steve Frenchd2f15422019-09-22 00:55:46 -05002465 /* no need to inc num_remote_opens because we close it just below */
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002466 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2467 FILE_WRITE_ATTRIBUTES);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002468 /* resource #4: response buffer */
2469 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2470 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002471 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2472 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002473 CREATE_NOT_FILE,
2474 FILE_WRITE_ATTRIBUTES, rc);
2475 goto err_free_rsp_buf;
2476 }
2477
2478 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2479 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2480 ses->Suid, CREATE_NOT_FILE,
2481 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002482
2483 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2484
2485 /* Eventually save off posix specific response info and timestaps */
2486
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002487err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002488 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002489 kfree(pc_buf);
2490err_free_req:
2491 cifs_small_buf_release(req);
2492err_free_path:
2493 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002494 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002495}
Steve Frenchbea851b2018-06-14 21:56:32 -05002496
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002497int
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002498SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2499 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002500{
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002501 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002502 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002503 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002504 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002505 int copy_size;
2506 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002507 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002508 struct kvec *iov = rqst->rq_iov;
2509 __le16 *copy_path;
2510 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002511
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002512 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002513 if (rc)
2514 return rc;
2515
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002516 iov[0].iov_base = (char *)req;
2517 /* -1 since last byte is buf[0] which is sent below (path) */
2518 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002519
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002520 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002521 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002522 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2523 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002524
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002525 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002526 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002527 /* File attributes ignored on open (used in create though) */
2528 req->FileAttributes = cpu_to_le32(file_attributes);
2529 req->ShareAccess = FILE_SHARE_ALL_LE;
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002530
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002531 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2532 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002533 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002534
2535 /* [MS-SMB2] 2.2.13 NameOffset:
2536 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2537 * the SMB2 header, the file name includes a prefix that will
2538 * be processed during DFS name normalization as specified in
2539 * section 3.3.5.9. Otherwise, the file name is relative to
2540 * the share that is identified by the TreeId in the SMB2
2541 * header.
2542 */
2543 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2544 int name_len;
2545
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002546 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002547 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2548 &name_len,
2549 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002550 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002551 return rc;
2552 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002553 uni_path_len = copy_size;
2554 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002555 } else {
2556 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2557 /* MUST set path len (NameLength) to 0 opening root of share */
2558 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002559 copy_size = uni_path_len;
2560 if (copy_size % 8 != 0)
2561 copy_size = roundup(copy_size, 8);
2562 copy_path = kzalloc(copy_size, GFP_KERNEL);
2563 if (!copy_path)
2564 return -ENOMEM;
2565 memcpy((char *)copy_path, (const char *)path,
2566 uni_path_len);
2567 uni_path_len = copy_size;
2568 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002569 }
2570
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002571 iov[1].iov_len = uni_path_len;
2572 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002573
Steve French3e7a02d2019-09-11 21:46:20 -05002574 if ((!server->oplocks) || (tcon->no_lease))
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002575 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2576
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002577 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002578 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2579 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002580 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2581 (oparms->create_options & CREATE_NOT_FILE))
2582 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002583 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002584 rc = add_lease_context(server, iov, &n_iov,
2585 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002586 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002587 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002588 }
2589
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002590 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2591 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002592 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002593 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002594 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002595 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002596 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002597 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002598
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002599 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002600 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002601 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002602 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002603 }
2604
Steve Frenchce558b02018-05-31 19:16:54 -05002605 if (tcon->posix_extensions) {
2606 if (n_iov > 2) {
2607 struct create_context *ccontext =
2608 (struct create_context *)iov[n_iov-1].iov_base;
2609 ccontext->Next =
2610 cpu_to_le32(iov[n_iov-1].iov_len);
2611 }
2612
2613 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002614 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002615 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002616 }
Steve Frenchce558b02018-05-31 19:16:54 -05002617
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002618 if (tcon->snapshot_time) {
2619 cifs_dbg(FYI, "adding snapshot context\n");
2620 if (n_iov > 2) {
2621 struct create_context *ccontext =
2622 (struct create_context *)iov[n_iov-1].iov_base;
2623 ccontext->Next =
2624 cpu_to_le32(iov[n_iov-1].iov_len);
2625 }
2626
2627 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2628 if (rc)
2629 return rc;
2630 }
2631
Steve Frenchfdef6652019-12-06 02:02:38 -06002632 if ((oparms->disposition != FILE_OPEN) &&
Steve French231e2a02019-12-07 17:38:22 -06002633 (oparms->cifs_sb) &&
2634 (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
Steve Frenchd0959b02019-10-05 10:53:58 -05002635 (oparms->mode != ACL_NO_MODE)) {
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002636 if (n_iov > 2) {
2637 struct create_context *ccontext =
2638 (struct create_context *)iov[n_iov-1].iov_base;
2639 ccontext->Next =
2640 cpu_to_le32(iov[n_iov-1].iov_len);
2641 }
2642
Steve Frenchfdef6652019-12-06 02:02:38 -06002643 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2644 rc = add_sd_context(iov, &n_iov, oparms->mode);
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002645 if (rc)
2646 return rc;
2647 }
2648
Steve Frenchff2a09e2019-07-06 14:41:38 -05002649 if (n_iov > 2) {
2650 struct create_context *ccontext =
2651 (struct create_context *)iov[n_iov-1].iov_base;
2652 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2653 }
2654 add_query_id_context(iov, &n_iov);
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002655
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002656 rqst->rq_nvec = n_iov;
2657 return 0;
2658}
2659
2660/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2661 * All other vectors are freed by kfree().
2662 */
2663void
2664SMB2_open_free(struct smb_rqst *rqst)
2665{
2666 int i;
2667
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002668 if (rqst && rqst->rq_iov) {
2669 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2670 for (i = 1; i < rqst->rq_nvec; i++)
2671 if (rqst->rq_iov[i].iov_base != smb2_padding)
2672 kfree(rqst->rq_iov[i].iov_base);
2673 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002674}
2675
2676int
2677SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2678 __u8 *oplock, struct smb2_file_all_info *buf,
2679 struct kvec *err_iov, int *buftype)
2680{
2681 struct smb_rqst rqst;
2682 struct smb2_create_rsp *rsp = NULL;
2683 struct TCP_Server_Info *server;
2684 struct cifs_tcon *tcon = oparms->tcon;
2685 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002686 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002687 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002688 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002689 int rc = 0;
2690 int flags = 0;
2691
2692 cifs_dbg(FYI, "create/open\n");
2693 if (ses && (ses->server))
2694 server = ses->server;
2695 else
2696 return -EIO;
2697
2698 if (smb3_encryption_required(tcon))
2699 flags |= CIFS_TRANSFORM_REQ;
2700
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002701 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002702 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002703 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002704 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002705
2706 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2707 if (rc)
2708 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002709
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002710 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2711 oparms->create_options, oparms->desired_access);
2712
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002713 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002714 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002715 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002716
2717 if (rc != 0) {
2718 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002719 if (err_iov && rsp) {
2720 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002721 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002722 resp_buftype = CIFS_NO_BUFFER;
2723 rsp = NULL;
2724 }
Steve French28d59362018-05-30 21:42:34 -05002725 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2726 oparms->create_options, oparms->desired_access, rc);
Steve French7dcc82c2019-09-11 00:07:36 -05002727 if (rc == -EREMCHG) {
2728 printk_once(KERN_WARNING "server share %s deleted\n",
2729 tcon->treeName);
2730 tcon->need_reconnect = true;
2731 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002732 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002733 } else
2734 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2735 ses->Suid, oparms->create_options,
2736 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002737
Steve Frenchfae80442018-10-19 17:14:32 -05002738 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002739 oparms->fid->persistent_fid = rsp->PersistentFileId;
2740 oparms->fid->volatile_fid = rsp->VolatileFileId;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002741#ifdef CONFIG_CIFS_DEBUG2
2742 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2743#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002744
2745 if (buf) {
2746 memcpy(buf, &rsp->CreationTime, 32);
2747 buf->AllocationSize = rsp->AllocationSize;
2748 buf->EndOfFile = rsp->EndofFile;
2749 buf->Attributes = rsp->FileAttributes;
2750 buf->NumberOfLinks = cpu_to_le32(1);
2751 buf->DeletePending = 0;
2752 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002753
Steve French89a5bfa2019-07-18 17:22:18 -05002754
2755 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2756 oparms->fid->lease_key, oplock, buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002757creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002758 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002759 free_rsp_buf(resp_buftype, rsp);
2760 return rc;
2761}
2762
Steve French4a72daf2013-06-25 00:20:49 -05002763int
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002764SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2765 u64 persistent_fid, u64 volatile_fid, u32 opcode,
Steve French153322f2019-03-28 22:32:49 -05002766 bool is_fsctl, char *in_data, u32 indatalen,
2767 __u32 max_response_size)
Steve French4a72daf2013-06-25 00:20:49 -05002768{
2769 struct smb2_ioctl_req *req;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002770 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002771 unsigned int total_len;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002772 int rc;
Long Li2c87d6a2019-05-15 14:09:05 -07002773 char *in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002774
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -03002775 rc = smb2_ioctl_req_init(opcode, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002776 if (rc)
2777 return rc;
2778
Long Li2c87d6a2019-05-15 14:09:05 -07002779 if (indatalen) {
2780 /*
2781 * indatalen is usually small at a couple of bytes max, so
2782 * just allocate through generic pool
2783 */
YueHaibingd81f0972019-06-01 03:31:10 +00002784 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
Long Li2c87d6a2019-05-15 14:09:05 -07002785 if (!in_data_buf) {
2786 cifs_small_buf_release(req);
2787 return -ENOMEM;
2788 }
Long Li2c87d6a2019-05-15 14:09:05 -07002789 }
2790
Steve French4a72daf2013-06-25 00:20:49 -05002791 req->CtlCode = cpu_to_le32(opcode);
2792 req->PersistentFileId = persistent_fid;
2793 req->VolatileFileId = volatile_fid;
2794
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002795 iov[0].iov_base = (char *)req;
2796 /*
2797 * If no input data, the size of ioctl struct in
2798 * protocol spec still includes a 1 byte data buffer,
2799 * but if input data passed to ioctl, we do not
2800 * want to double count this, so we do not send
2801 * the dummy one byte of data in iovec[0] if sending
2802 * input data (in iovec[1]).
2803 */
Steve French4a72daf2013-06-25 00:20:49 -05002804 if (indatalen) {
2805 req->InputCount = cpu_to_le32(indatalen);
2806 /* do not set InputOffset if no input data */
2807 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002808 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002809 rqst->rq_nvec = 2;
2810 iov[0].iov_len = total_len - 1;
Long Li2c87d6a2019-05-15 14:09:05 -07002811 iov[1].iov_base = in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002812 iov[1].iov_len = indatalen;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002813 } else {
2814 rqst->rq_nvec = 1;
2815 iov[0].iov_len = total_len;
2816 }
Steve French4a72daf2013-06-25 00:20:49 -05002817
2818 req->OutputOffset = 0;
2819 req->OutputCount = 0; /* MBZ */
2820
2821 /*
Steve French153322f2019-03-28 22:32:49 -05002822 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2823 * We Could increase default MaxOutputResponse, but that could require
2824 * more credits. Windows typically sets this smaller, but for some
Steve French4a72daf2013-06-25 00:20:49 -05002825 * ioctls it may be useful to allow server to send more. No point
2826 * limiting what the server can send as long as fits in one credit
Steve French153322f2019-03-28 22:32:49 -05002827 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2828 * to increase this limit up in the future.
2829 * Note that for snapshot queries that servers like Azure expect that
2830 * the first query be minimal size (and just used to get the number/size
2831 * of previous versions) so response size must be specified as EXACTLY
2832 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2833 * of eight bytes. Currently that is the only case where we set max
2834 * response size smaller.
Steve French4a72daf2013-06-25 00:20:49 -05002835 */
Steve French153322f2019-03-28 22:32:49 -05002836 req->MaxOutputResponse = cpu_to_le32(max_response_size);
Steve French4a72daf2013-06-25 00:20:49 -05002837
2838 if (is_fsctl)
2839 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2840 else
2841 req->Flags = 0;
2842
Steve French4587eee2017-10-25 15:58:31 -05002843 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2844 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002845 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002846
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002847 return 0;
2848}
2849
2850void
2851SMB2_ioctl_free(struct smb_rqst *rqst)
2852{
Murphy Zhou6457c202019-05-23 12:12:43 +08002853 int i;
Long Li2c87d6a2019-05-15 14:09:05 -07002854 if (rqst && rqst->rq_iov) {
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002855 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Murphy Zhou6457c202019-05-23 12:12:43 +08002856 for (i = 1; i < rqst->rq_nvec; i++)
2857 if (rqst->rq_iov[i].iov_base != smb2_padding)
2858 kfree(rqst->rq_iov[i].iov_base);
Long Li2c87d6a2019-05-15 14:09:05 -07002859 }
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002860}
2861
Steve French153322f2019-03-28 22:32:49 -05002862
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002863/*
2864 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2865 */
2866int
2867SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2868 u64 volatile_fid, u32 opcode, bool is_fsctl,
Steve French153322f2019-03-28 22:32:49 -05002869 char *in_data, u32 indatalen, u32 max_out_data_len,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002870 char **out_data, u32 *plen /* returned data len */)
2871{
2872 struct smb_rqst rqst;
2873 struct smb2_ioctl_rsp *rsp = NULL;
2874 struct cifs_ses *ses;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002875 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002876 struct kvec rsp_iov = {NULL, 0};
2877 int resp_buftype = CIFS_NO_BUFFER;
2878 int rc = 0;
2879 int flags = 0;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002880 struct TCP_Server_Info *server;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002881
2882 cifs_dbg(FYI, "SMB2 IOCTL\n");
2883
2884 if (out_data != NULL)
2885 *out_data = NULL;
2886
2887 /* zero out returned data len, in case of error */
2888 if (plen)
2889 *plen = 0;
2890
2891 if (tcon)
2892 ses = tcon->ses;
2893 else
2894 return -EIO;
2895
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01002896 if (!ses)
2897 return -EIO;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002898 server = ses->server;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01002899 if (!server)
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002900 return -EIO;
2901
2902 if (smb3_encryption_required(tcon))
2903 flags |= CIFS_TRANSFORM_REQ;
2904
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002905 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002906 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002907 rqst.rq_iov = iov;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002908 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002909
Steve French153322f2019-03-28 22:32:49 -05002910 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2911 is_fsctl, in_data, indatalen, max_out_data_len);
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002912 if (rc)
2913 goto ioctl_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002914
2915 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002916 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002917 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002918
Steve Frencheccb4422018-05-17 21:16:55 -05002919 if (rc != 0)
2920 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2921 ses->Suid, 0, opcode, rc);
2922
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002923 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
Steve French8e353102015-03-26 19:47:02 -05002924 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002925 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002926 } else if (rc == -EINVAL) {
2927 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2928 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002929 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002930 goto ioctl_exit;
2931 }
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002932 } else if (rc == -E2BIG) {
2933 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2934 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2935 goto ioctl_exit;
2936 }
Steve French4a72daf2013-06-25 00:20:49 -05002937 }
2938
2939 /* check if caller wants to look at return data or just return rc */
2940 if ((plen == NULL) || (out_data == NULL))
2941 goto ioctl_exit;
2942
2943 *plen = le32_to_cpu(rsp->OutputCount);
2944
2945 /* We check for obvious errors in the output buffer length and offset */
2946 if (*plen == 0)
2947 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002948 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002949 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002950 *plen = 0;
2951 rc = -EIO;
2952 goto ioctl_exit;
2953 }
2954
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002955 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002956 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
Steve French4a72daf2013-06-25 00:20:49 -05002957 le32_to_cpu(rsp->OutputOffset));
2958 *plen = 0;
2959 rc = -EIO;
2960 goto ioctl_exit;
2961 }
2962
YueHaibingd034fee2018-09-10 01:33:06 +00002963 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2964 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05002965 if (*out_data == NULL) {
2966 rc = -ENOMEM;
2967 goto ioctl_exit;
2968 }
2969
Steve French4a72daf2013-06-25 00:20:49 -05002970ioctl_exit:
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002971 SMB2_ioctl_free(&rqst);
Steve French4a72daf2013-06-25 00:20:49 -05002972 free_rsp_buf(resp_buftype, rsp);
2973 return rc;
2974}
2975
Steve French64a5cfa2013-10-14 15:31:32 -05002976/*
2977 * Individual callers to ioctl worker function follow
2978 */
2979
2980int
2981SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2982 u64 persistent_fid, u64 volatile_fid)
2983{
2984 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002985 struct compress_ioctl fsctl_input;
2986 char *ret_data = NULL;
2987
2988 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002989 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002990
2991 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2992 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2993 (char *)&fsctl_input /* data input */,
Steve French153322f2019-03-28 22:32:49 -05002994 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2995 &ret_data /* out data */, NULL);
Steve French64a5cfa2013-10-14 15:31:32 -05002996
2997 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002998
2999 return rc;
3000}
3001
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003002int
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003003SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Steve French43f8a6a2019-12-02 21:46:54 -06003004 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003005{
3006 struct smb2_close_req *req;
3007 struct kvec *iov = rqst->rq_iov;
3008 unsigned int total_len;
3009 int rc;
3010
3011 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
3012 if (rc)
3013 return rc;
3014
3015 req->PersistentFileId = persistent_fid;
3016 req->VolatileFileId = volatile_fid;
Steve French43f8a6a2019-12-02 21:46:54 -06003017 if (query_attrs)
3018 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3019 else
3020 req->Flags = 0;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003021 iov[0].iov_base = (char *)req;
3022 iov[0].iov_len = total_len;
3023
3024 return 0;
3025}
3026
3027void
3028SMB2_close_free(struct smb_rqst *rqst)
3029{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003030 if (rqst && rqst->rq_iov)
3031 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003032}
3033
3034int
Steve French43f8a6a2019-12-02 21:46:54 -06003035__SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3036 u64 persistent_fid, u64 volatile_fid,
3037 struct smb2_file_network_open_info *pbuf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003038{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003039 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003040 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003041 struct cifs_ses *ses = tcon->ses;
3042 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003043 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01003044 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003045 int rc = 0;
Steve French9e8fae22019-12-02 17:55:41 -06003046 int flags = 0;
Steve French43f8a6a2019-12-02 21:46:54 -06003047 bool query_attrs = false;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003048
Joe Perchesf96637b2013-05-04 22:12:25 -05003049 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003050
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003051 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003052 return -EIO;
3053
Steve French5a77e752018-05-09 17:43:08 -05003054 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003055 flags |= CIFS_TRANSFORM_REQ;
3056
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003057 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003058 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003059 rqst.rq_iov = iov;
3060 rqst.rq_nvec = 1;
3061
Steve French43f8a6a2019-12-02 21:46:54 -06003062 /* check if need to ask server to return timestamps in close response */
3063 if (pbuf)
3064 query_attrs = true;
3065
Steve Frenchf90f9792019-09-03 18:35:42 -05003066 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Steve French43f8a6a2019-12-02 21:46:54 -06003067 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid,
3068 query_attrs);
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003069 if (rc)
3070 goto close_exit;
3071
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003072 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003073 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003074
3075 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09003076 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003077 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3078 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003079 goto close_exit;
Steve French43f8a6a2019-12-02 21:46:54 -06003080 } else {
Steve Frenchf90f9792019-09-03 18:35:42 -05003081 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3082 ses->Suid);
Steve French43f8a6a2019-12-02 21:46:54 -06003083 /*
3084 * Note that have to subtract 4 since struct network_open_info
3085 * has a final 4 byte pad that close response does not have
3086 */
3087 if (pbuf)
3088 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3089 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003090
Steve Frenchfae80442018-10-19 17:14:32 -05003091 atomic_dec(&tcon->num_remote_opens);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003092close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003093 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003094 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003095
3096 /* retry close in a worker thread if this one is interrupted */
3097 if (rc == -EINTR) {
Steve French9e8fae22019-12-02 17:55:41 -06003098 int tmp_rc;
3099
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003100 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3101 volatile_fid);
3102 if (tmp_rc)
3103 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3104 persistent_fid, tmp_rc);
3105 }
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003106 return rc;
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06003107}
3108
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003109int
Steve French43f8a6a2019-12-02 21:46:54 -06003110SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3111 u64 persistent_fid, u64 volatile_fid)
3112{
3113 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3114}
3115
3116int
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003117smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3118 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003119{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003120 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003121 char *end_of_smb = smb_len + (char *)iov->iov_base;
3122 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003123 char *end_of_buf = begin_of_buf + buffer_length;
3124
3125
3126 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003127 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3128 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003129 return -EINVAL;
3130 }
3131
3132 /* check if beyond RFC1001 maximum length */
3133 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003134 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3135 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003136 return -EINVAL;
3137 }
3138
3139 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003140 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003141 return -EINVAL;
3142 }
3143
3144 return 0;
3145}
3146
3147/*
3148 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3149 * Caller must free buffer.
3150 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003151int
3152smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3153 struct kvec *iov, unsigned int minbufsize,
3154 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003155{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003156 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003157 int rc;
3158
3159 if (!data)
3160 return -EINVAL;
3161
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003162 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003163 if (rc)
3164 return rc;
3165
3166 memcpy(data, begin_of_buf, buffer_length);
3167
3168 return 0;
3169}
3170
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003171int
3172SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3173 u64 persistent_fid, u64 volatile_fid,
3174 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003175 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003176{
3177 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003178 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003179 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003180 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003181
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003182 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3183 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003184 if (rc)
3185 return rc;
3186
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003187 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003188 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003189 req->PersistentFileId = persistent_fid;
3190 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003191 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02003192
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003193 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003194 if (input_len) {
3195 req->InputBufferLength = cpu_to_le32(input_len);
3196 /* total_len for smb query request never close to le16 max */
3197 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3198 memcpy(req->Buffer, input, input_len);
3199 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003200
3201 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003202 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003203 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003204 return 0;
3205}
3206
3207void
3208SMB2_query_info_free(struct smb_rqst *rqst)
3209{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003210 if (rqst && rqst->rq_iov)
3211 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003212}
3213
3214static int
3215query_info(const unsigned int xid, struct cifs_tcon *tcon,
3216 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3217 u32 additional_info, size_t output_len, size_t min_len, void **data,
3218 u32 *dlen)
3219{
3220 struct smb_rqst rqst;
3221 struct smb2_query_info_rsp *rsp = NULL;
3222 struct kvec iov[1];
3223 struct kvec rsp_iov;
3224 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01003225 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003226 struct cifs_ses *ses = tcon->ses;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003227 struct TCP_Server_Info *server;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003228 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003229 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003230
3231 cifs_dbg(FYI, "Query Info\n");
3232
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003233 if (!ses)
3234 return -EIO;
3235 server = ses->server;
3236 if (!server)
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003237 return -EIO;
3238
3239 if (smb3_encryption_required(tcon))
3240 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003241
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003242 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003243 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003244 rqst.rq_iov = iov;
3245 rqst.rq_nvec = 1;
3246
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003247 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3248 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003249 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003250 if (rc)
3251 goto qinf_exit;
3252
Steve Frenchd42043a2019-02-26 21:58:30 -06003253 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3254 ses->Suid, info_class, (__u32)info_type);
3255
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003256 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003257 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003258
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003259 if (rc) {
3260 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003261 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3262 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003263 goto qinf_exit;
3264 }
3265
Steve Frenchd42043a2019-02-26 21:58:30 -06003266 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3267 ses->Suid, info_class, (__u32)info_type);
3268
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003269 if (dlen) {
3270 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3271 if (!*data) {
3272 *data = kmalloc(*dlen, GFP_KERNEL);
3273 if (!*data) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003274 cifs_tcon_dbg(VFS,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003275 "Error %d allocating memory for acl\n",
3276 rc);
3277 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003278 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003279 goto qinf_exit;
3280 }
Colin Ian King73aaf922019-01-16 16:28:59 +00003281 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003282 }
3283 }
3284
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003285 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3286 le32_to_cpu(rsp->OutputBufferLength),
3287 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00003288 if (rc && allocated) {
3289 kfree(*data);
3290 *data = NULL;
3291 *dlen = 0;
3292 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003293
3294qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003295 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003296 free_rsp_buf(resp_buftype, rsp);
3297 return rc;
3298}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003299
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003300int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3301 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003302{
3303 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003304 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04003305 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003306 sizeof(struct smb2_file_all_info), (void **)&data,
3307 NULL);
3308}
3309
3310int
3311SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3312 u64 persistent_fid, u64 volatile_fid,
3313 void **data, u32 *plen)
3314{
3315 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3316 *plen = 0;
3317
3318 return query_info(xid, tcon, persistent_fid, volatile_fid,
3319 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05003320 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003321}
3322
3323int
3324SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3325 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3326{
3327 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003328 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003329 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003330 sizeof(struct smb2_file_internal_info),
3331 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003332}
3333
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003334/*
Steve Frenchc3498182019-09-15 22:38:52 -05003335 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3336 * See MS-SMB2 2.2.35 and 2.2.36
3337 */
3338
zhengbin388962e2019-09-23 15:06:18 +08003339static int
Steve Frenchc3498182019-09-15 22:38:52 -05003340SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3341 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid,
3342 u32 completion_filter, bool watch_tree)
3343{
3344 struct smb2_change_notify_req *req;
3345 struct kvec *iov = rqst->rq_iov;
3346 unsigned int total_len;
3347 int rc;
3348
3349 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len);
3350 if (rc)
3351 return rc;
3352
3353 req->PersistentFileId = persistent_fid;
3354 req->VolatileFileId = volatile_fid;
Steve French52870d52019-10-01 21:25:46 -05003355 req->OutputBufferLength =
3356 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
Steve Frenchc3498182019-09-15 22:38:52 -05003357 req->CompletionFilter = cpu_to_le32(completion_filter);
3358 if (watch_tree)
3359 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3360 else
3361 req->Flags = 0;
3362
3363 iov[0].iov_base = (char *)req;
3364 iov[0].iov_len = total_len;
3365
3366 return 0;
3367}
3368
3369int
3370SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3371 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3372 u32 completion_filter)
3373{
3374 struct cifs_ses *ses = tcon->ses;
3375 struct smb_rqst rqst;
3376 struct kvec iov[1];
3377 struct kvec rsp_iov = {NULL, 0};
3378 int resp_buftype = CIFS_NO_BUFFER;
3379 int flags = 0;
3380 int rc = 0;
3381
3382 cifs_dbg(FYI, "change notify\n");
3383 if (!ses || !(ses->server))
3384 return -EIO;
3385
3386 if (smb3_encryption_required(tcon))
3387 flags |= CIFS_TRANSFORM_REQ;
3388
3389 memset(&rqst, 0, sizeof(struct smb_rqst));
3390 memset(&iov, 0, sizeof(iov));
3391 rqst.rq_iov = iov;
3392 rqst.rq_nvec = 1;
3393
3394 rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid,
3395 completion_filter, watch_tree);
3396 if (rc)
3397 goto cnotify_exit;
3398
3399 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3400 (u8)watch_tree, completion_filter);
3401 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3402
3403 if (rc != 0) {
3404 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3405 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3406 (u8)watch_tree, completion_filter, rc);
3407 } else
3408 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3409 ses->Suid, (u8)watch_tree, completion_filter);
3410
3411 cnotify_exit:
3412 if (rqst.rq_iov)
3413 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3414 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3415 return rc;
3416}
3417
3418
3419
3420/*
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003421 * This is a no-op for now. We're not really interested in the reply, but
3422 * rather in the fact that the server sent one and that server->lstrp
3423 * gets updated.
3424 *
3425 * FIXME: maybe we should consider checking that the reply matches request?
3426 */
3427static void
3428smb2_echo_callback(struct mid_q_entry *mid)
3429{
3430 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003431 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003432 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003433
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003434 if (mid->mid_state == MID_RESPONSE_RECEIVED
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003435 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3436 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3437 credits.instance = server->reconnect_instance;
3438 }
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003439
3440 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003441 add_credits(server, &credits, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003442}
3443
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003444void smb2_reconnect_server(struct work_struct *work)
3445{
3446 struct TCP_Server_Info *server = container_of(work,
3447 struct TCP_Server_Info, reconnect.work);
3448 struct cifs_ses *ses;
3449 struct cifs_tcon *tcon, *tcon2;
3450 struct list_head tmp_list;
3451 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01003452 int rc;
3453 int resched = false;
3454
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003455
3456 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3457 mutex_lock(&server->reconnect_mutex);
3458
3459 INIT_LIST_HEAD(&tmp_list);
3460 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3461
3462 spin_lock(&cifs_tcp_ses_lock);
3463 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3464 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003465 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003466 tcon->tc_count++;
3467 list_add_tail(&tcon->rlist, &tmp_list);
3468 tcon_exist = true;
3469 }
3470 }
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003471 /*
3472 * IPC has the same lifetime as its session and uses its
3473 * refcount.
3474 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003475 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3476 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3477 tcon_exist = true;
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003478 ses->ses_count++;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003479 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003480 }
3481 /*
3482 * Get the reference to server struct to be sure that the last call of
3483 * cifs_put_tcon() in the loop below won't release the server pointer.
3484 */
3485 if (tcon_exist)
3486 server->srv_count++;
3487
3488 spin_unlock(&cifs_tcp_ses_lock);
3489
3490 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01003491 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3492 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003493 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01003494 else
3495 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003496 list_del_init(&tcon->rlist);
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003497 if (tcon->ipc)
3498 cifs_put_smb_ses(tcon->ses);
3499 else
3500 cifs_put_tcon(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003501 }
3502
3503 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01003504 if (resched)
3505 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003506 mutex_unlock(&server->reconnect_mutex);
3507
3508 /* now we can safely release srv struct */
3509 if (tcon_exist)
3510 cifs_put_tcp_session(server, 1);
3511}
3512
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003513int
3514SMB2_echo(struct TCP_Server_Info *server)
3515{
3516 struct smb2_echo_req *req;
3517 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003518 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003519 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003520 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003521 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003522
Joe Perchesf96637b2013-05-04 22:12:25 -05003523 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003524
Steve French4fcd1812016-06-22 20:12:05 -05003525 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003526 /* No need to send echo on newly established connections */
3527 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3528 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003529 }
3530
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003531 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003532 if (rc)
3533 return rc;
3534
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003535 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003536
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003537 iov[0].iov_len = total_len;
3538 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003539
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003540 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003541 server, CIFS_ECHO_OP, NULL);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003542 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003543 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003544
3545 cifs_small_buf_release(req);
3546 return rc;
3547}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003548
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003549void
3550SMB2_flush_free(struct smb_rqst *rqst)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003551{
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003552 if (rqst && rqst->rq_iov)
3553 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3554}
3555
3556int
3557SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3558 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3559{
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003560 struct smb2_flush_req *req;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003561 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003562 unsigned int total_len;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003563 int rc;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003564
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003565 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003566 if (rc)
3567 return rc;
3568
3569 req->PersistentFileId = persistent_fid;
3570 req->VolatileFileId = volatile_fid;
3571
3572 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003573 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003574
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003575 return 0;
3576}
3577
3578int
3579SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3580 u64 volatile_fid)
3581{
3582 struct cifs_ses *ses = tcon->ses;
3583 struct smb_rqst rqst;
3584 struct kvec iov[1];
3585 struct kvec rsp_iov = {NULL, 0};
3586 int resp_buftype = CIFS_NO_BUFFER;
3587 int flags = 0;
3588 int rc = 0;
3589
3590 cifs_dbg(FYI, "flush\n");
3591 if (!ses || !(ses->server))
3592 return -EIO;
3593
3594 if (smb3_encryption_required(tcon))
3595 flags |= CIFS_TRANSFORM_REQ;
3596
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003597 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003598 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003599 rqst.rq_iov = iov;
3600 rqst.rq_nvec = 1;
3601
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003602 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3603 if (rc)
3604 goto flush_exit;
3605
Steve Frenchf90f9792019-09-03 18:35:42 -05003606 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003607 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003608
Steve Frencheccb4422018-05-17 21:16:55 -05003609 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003610 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003611 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3612 rc);
Steve Frenchf90f9792019-09-03 18:35:42 -05003613 } else
3614 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3615 ses->Suid);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003616
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003617 flush_exit:
3618 SMB2_flush_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003619 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003620 return rc;
3621}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003622
3623/*
3624 * To form a chain of read requests, any read requests after the first should
3625 * have the end_of_chain boolean set to true.
3626 */
3627static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003628smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003629 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3630 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003631{
3632 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003633 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003634 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07003635 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003636
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003637 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3638 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003639 if (rc)
3640 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003641
3642 server = io_parms->tcon->ses->server;
3643 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003644 return -ECONNABORTED;
3645
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003646 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003647 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003648
3649 req->PersistentFileId = io_parms->persistent_fid;
3650 req->VolatileFileId = io_parms->volatile_fid;
3651 req->ReadChannelInfoOffset = 0; /* reserved */
3652 req->ReadChannelInfoLength = 0; /* reserved */
3653 req->Channel = 0; /* reserved */
3654 req->MinimumCount = 0;
3655 req->Length = cpu_to_le32(io_parms->length);
3656 req->Offset = cpu_to_le64(io_parms->offset);
Steve Frenchd323c2462019-02-25 00:52:43 -06003657
3658 trace_smb3_read_enter(0 /* xid */,
3659 io_parms->persistent_fid,
3660 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3661 io_parms->offset, io_parms->length);
Long Libd3dcc62017-11-22 17:38:47 -07003662#ifdef CONFIG_CIFS_SMB_DIRECT
3663 /*
3664 * If we want to do a RDMA write, fill in and append
3665 * smbd_buffer_descriptor_v1 to the end of read request
3666 */
Long Libb4c0412018-04-17 12:17:08 -07003667 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003668 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003669
Long Libd3dcc62017-11-22 17:38:47 -07003670 struct smbd_buffer_descriptor_v1 *v1;
3671 bool need_invalidate =
3672 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3673
3674 rdata->mr = smbd_register_mr(
3675 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003676 rdata->nr_pages, rdata->page_offset,
3677 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003678 if (!rdata->mr)
Long Lib7972092019-04-05 21:36:34 +00003679 return -EAGAIN;
Long Libd3dcc62017-11-22 17:38:47 -07003680
3681 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3682 if (need_invalidate)
3683 req->Channel = SMB2_CHANNEL_RDMA_V1;
3684 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003685 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003686 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003687 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003688 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003689 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3690 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3691 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003692
3693 *total_len += sizeof(*v1) - 1;
3694 }
3695#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003696 if (request_type & CHAINED_REQUEST) {
3697 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003698 /* next 8-byte aligned request */
3699 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3700 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003701 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003702 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003703 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003704 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003705 /*
3706 * Related requests use info from previous read request
3707 * in chain.
3708 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003709 shdr->SessionId = 0xFFFFFFFF;
3710 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003711 req->PersistentFileId = 0xFFFFFFFF;
3712 req->VolatileFileId = 0xFFFFFFFF;
3713 }
3714 }
3715 if (remaining_bytes > io_parms->length)
3716 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3717 else
3718 req->RemainingBytes = 0;
3719
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003720 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003721 return rc;
3722}
3723
3724static void
3725smb2_readv_callback(struct mid_q_entry *mid)
3726{
3727 struct cifs_readdata *rdata = mid->callback_data;
3728 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3729 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003730 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003731 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003732 struct cifs_credits credits = { .value = 0, .instance = 0 };
Steve French46f17d12019-09-04 23:07:52 -05003733 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3734 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07003735 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003736 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003737 .rq_npages = rdata->nr_pages,
3738 .rq_pagesz = rdata->pagesz,
3739 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003740
Joe Perchesf96637b2013-05-04 22:12:25 -05003741 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3742 __func__, mid->mid, mid->mid_state, rdata->result,
3743 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003744
3745 switch (mid->mid_state) {
3746 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003747 credits.value = le16_to_cpu(shdr->CreditRequest);
3748 credits.instance = server->reconnect_instance;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003749 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003750 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003751 int rc;
3752
Jeff Layton0b688cf2012-09-18 16:20:34 -07003753 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003754 if (rc)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003755 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003756 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003757 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003758 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003759 task_io_account_read(rdata->got_bytes);
3760 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003761 break;
3762 case MID_REQUEST_SUBMITTED:
3763 case MID_RETRY_NEEDED:
3764 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003765 if (server->sign && rdata->got_bytes)
3766 /* reset bytes number since we can not check a sign */
3767 rdata->got_bytes = 0;
3768 /* FIXME: should this be counted toward the initiating task? */
3769 task_io_account_read(rdata->got_bytes);
3770 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003771 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003772 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003773 credits.value = le16_to_cpu(shdr->CreditRequest);
3774 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003775 /* fall through */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003776 default:
Pavel Shilovsky6b15eb18c2019-01-18 15:46:14 -08003777 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003778 }
Long Libd3dcc62017-11-22 17:38:47 -07003779#ifdef CONFIG_CIFS_SMB_DIRECT
3780 /*
3781 * If this rdata has a memmory registered, the MR can be freed
3782 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3783 * because they have limited number and are used for future I/Os
3784 */
3785 if (rdata->mr) {
3786 smbd_deregister_mr(rdata->mr);
3787 rdata->mr = NULL;
3788 }
3789#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08003790 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003791 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003792 trace_smb3_read_err(0 /* xid */,
3793 rdata->cfile->fid.persistent_fid,
3794 tcon->tid, tcon->ses->Suid, rdata->offset,
3795 rdata->bytes, rdata->result);
3796 } else
3797 trace_smb3_read_done(0 /* xid */,
3798 rdata->cfile->fid.persistent_fid,
3799 tcon->tid, tcon->ses->Suid,
3800 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003801
3802 queue_work(cifsiod_wq, &rdata->work);
3803 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003804 add_credits(server, &credits, 0);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003805}
3806
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003807/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003808int
3809smb2_async_readv(struct cifs_readdata *rdata)
3810{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003811 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003812 char *buf;
3813 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003814 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003815 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003816 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003817 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003818 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003819
Joe Perchesf96637b2013-05-04 22:12:25 -05003820 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3821 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003822
3823 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3824 io_parms.offset = rdata->offset;
3825 io_parms.length = rdata->bytes;
3826 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3827 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3828 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003829
3830 server = io_parms.tcon->ses->server;
3831
Long Li2dabfd52017-11-07 01:54:53 -07003832 rc = smb2_new_read_req(
3833 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003834 if (rc)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003835 return rc;
3836
Steve French5a77e752018-05-09 17:43:08 -05003837 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003838 flags |= CIFS_TRANSFORM_REQ;
3839
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003840 rdata->iov[0].iov_base = buf;
3841 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003842
3843 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003844
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003845 if (rdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003846 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003847 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003848 shdr->CreditRequest =
3849 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003850
3851 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3852 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003853 goto async_readv_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003854
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003855 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003856 }
3857
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003858 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07003859 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003860 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003861 smb3_handle_read_data, rdata, flags,
3862 &rdata->credits);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003863 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003864 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003865 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003866 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3867 io_parms.tcon->tid,
3868 io_parms.tcon->ses->Suid,
3869 io_parms.offset, io_parms.length, rc);
3870 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003871
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003872async_readv_out:
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003873 cifs_small_buf_release(buf);
3874 return rc;
3875}
Pavel Shilovsky33319142012-09-18 16:20:29 -07003876
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003877int
3878SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3879 unsigned int *nbytes, char **buf, int *buf_type)
3880{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003881 struct smb_rqst rqst;
Colin Ian King1efd4fc2019-07-31 10:05:26 +01003882 int resp_buftype, rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003883 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003884 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003885 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003886 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003887 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003888 int flags = CIFS_LOG_ERROR;
3889 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003890
3891 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07003892 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003893 if (rc)
3894 return rc;
3895
Steve French5a77e752018-05-09 17:43:08 -05003896 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003897 flags |= CIFS_TRANSFORM_REQ;
3898
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003899 iov[0].iov_base = (char *)req;
3900 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003901
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003902 memset(&rqst, 0, sizeof(struct smb_rqst));
3903 rqst.rq_iov = iov;
3904 rqst.rq_nvec = 1;
3905
3906 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003907 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003908
3909 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003910 if (rc != -ENODATA) {
3911 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3912 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003913 trace_smb3_read_err(xid, req->PersistentFileId,
3914 io_parms->tcon->tid, ses->Suid,
3915 io_parms->offset, io_parms->length,
3916 rc);
Steve Frenchb0a42f22019-02-25 15:02:58 -06003917 } else
3918 trace_smb3_read_done(xid, req->PersistentFileId,
3919 io_parms->tcon->tid, ses->Suid,
3920 io_parms->offset, 0);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003921 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Ronnie Sahlberg05fd5c22019-04-23 16:39:45 +10003922 cifs_small_buf_release(req);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003923 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05003924 } else
3925 trace_smb3_read_done(xid, req->PersistentFileId,
3926 io_parms->tcon->tid, ses->Suid,
3927 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003928
ZhangXiaoxu088aaf12019-04-06 15:47:39 +08003929 cifs_small_buf_release(req);
3930
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003931 *nbytes = le32_to_cpu(rsp->DataLength);
3932 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3933 (*nbytes > io_parms->length)) {
3934 cifs_dbg(FYI, "bad length %d for count %d\n",
3935 *nbytes, io_parms->length);
3936 rc = -EIO;
3937 *nbytes = 0;
3938 }
3939
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003940 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003941 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003942 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003943 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003944 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003945 if (resp_buftype == CIFS_SMALL_BUFFER)
3946 *buf_type = CIFS_SMALL_BUFFER;
3947 else if (resp_buftype == CIFS_LARGE_BUFFER)
3948 *buf_type = CIFS_LARGE_BUFFER;
3949 }
3950 return rc;
3951}
3952
Pavel Shilovsky33319142012-09-18 16:20:29 -07003953/*
3954 * Check the mid_state and signature on received buffer (if any), and queue the
3955 * workqueue completion task.
3956 */
3957static void
3958smb2_writev_callback(struct mid_q_entry *mid)
3959{
3960 struct cifs_writedata *wdata = mid->callback_data;
3961 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003962 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003963 unsigned int written;
3964 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003965 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky33319142012-09-18 16:20:29 -07003966
3967 switch (mid->mid_state) {
3968 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003969 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3970 credits.instance = server->reconnect_instance;
3971 wdata->result = smb2_check_receive(mid, server, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003972 if (wdata->result != 0)
3973 break;
3974
3975 written = le32_to_cpu(rsp->DataLength);
3976 /*
3977 * Mask off high 16 bits when bytes written as returned
3978 * by the server is greater than bytes requested by the
3979 * client. OS/2 servers are known to set incorrect
3980 * CountHigh values.
3981 */
3982 if (written > wdata->bytes)
3983 written &= 0xFFFF;
3984
3985 if (written < wdata->bytes)
3986 wdata->result = -ENOSPC;
3987 else
3988 wdata->bytes = written;
3989 break;
3990 case MID_REQUEST_SUBMITTED:
3991 case MID_RETRY_NEEDED:
3992 wdata->result = -EAGAIN;
3993 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003994 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003995 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3996 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003997 /* fall through */
Pavel Shilovsky33319142012-09-18 16:20:29 -07003998 default:
3999 wdata->result = -EIO;
4000 break;
4001 }
Long Lidb223a52017-11-22 17:38:45 -07004002#ifdef CONFIG_CIFS_SMB_DIRECT
4003 /*
4004 * If this wdata has a memory registered, the MR can be freed
4005 * The number of MRs available is limited, it's important to recover
4006 * used MR as soon as I/O is finished. Hold MR longer in the later
4007 * I/O process can possibly result in I/O deadlock due to lack of MR
4008 * to send request on I/O retry
4009 */
4010 if (wdata->mr) {
4011 smbd_deregister_mr(wdata->mr);
4012 wdata->mr = NULL;
4013 }
4014#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004015 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07004016 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004017 trace_smb3_write_err(0 /* no xid */,
4018 wdata->cfile->fid.persistent_fid,
4019 tcon->tid, tcon->ses->Suid, wdata->offset,
4020 wdata->bytes, wdata->result);
4021 } else
4022 trace_smb3_write_done(0 /* no xid */,
4023 wdata->cfile->fid.persistent_fid,
4024 tcon->tid, tcon->ses->Suid,
4025 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004026
4027 queue_work(cifsiod_wq, &wdata->work);
4028 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004029 add_credits(server, &credits, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004030}
4031
4032/* smb2_async_writev - send an async write, and set up mid to handle result */
4033int
Steve French4a5c80d2014-02-07 20:45:12 -06004034smb2_async_writev(struct cifs_writedata *wdata,
4035 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07004036{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004037 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004038 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004039 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004040 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004041 struct TCP_Server_Info *server = tcon->ses->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004042 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004043 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004044 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004045
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004046 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08004047 if (rc)
4048 return rc;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004049
Steve French5a77e752018-05-09 17:43:08 -05004050 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004051 flags |= CIFS_TRANSFORM_REQ;
4052
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004053 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004054 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004055
4056 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4057 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4058 req->WriteChannelInfoOffset = 0;
4059 req->WriteChannelInfoLength = 0;
4060 req->Channel = 0;
4061 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004062 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004063 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07004064 req->RemainingBytes = 0;
Steve Frenchd323c2462019-02-25 00:52:43 -06004065
4066 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4067 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07004068#ifdef CONFIG_CIFS_SMB_DIRECT
4069 /*
4070 * If we want to do a server RDMA read, fill in and append
4071 * smbd_buffer_descriptor_v1 to the end of write request
4072 */
Long Libb4c0412018-04-17 12:17:08 -07004073 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07004074 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07004075
Long Lidb223a52017-11-22 17:38:45 -07004076 struct smbd_buffer_descriptor_v1 *v1;
4077 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4078
4079 wdata->mr = smbd_register_mr(
4080 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07004081 wdata->nr_pages, wdata->page_offset,
4082 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07004083 if (!wdata->mr) {
Long Lib7972092019-04-05 21:36:34 +00004084 rc = -EAGAIN;
Long Lidb223a52017-11-22 17:38:45 -07004085 goto async_writev_out;
4086 }
4087 req->Length = 0;
4088 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07004089 if (wdata->nr_pages > 1)
4090 req->RemainingBytes =
4091 cpu_to_le32(
4092 (wdata->nr_pages - 1) * wdata->pagesz -
4093 wdata->page_offset + wdata->tailsz
4094 );
4095 else
4096 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07004097 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4098 if (need_invalidate)
4099 req->Channel = SMB2_CHANNEL_RDMA_V1;
4100 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06004101 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07004102 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06004103 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07004104 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06004105 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4106 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4107 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07004108 }
4109#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004110 iov[0].iov_len = total_len - 1;
4111 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004112
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004113 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004114 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07004115 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07004116 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07004117 rqst.rq_npages = wdata->nr_pages;
4118 rqst.rq_pagesz = wdata->pagesz;
4119 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07004120#ifdef CONFIG_CIFS_SMB_DIRECT
4121 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004122 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07004123 rqst.rq_npages = 0;
4124 }
4125#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05004126 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4127 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004128
Long Lidb223a52017-11-22 17:38:45 -07004129#ifdef CONFIG_CIFS_SMB_DIRECT
4130 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4131 if (!wdata->mr)
4132 req->Length = cpu_to_le32(wdata->bytes);
4133#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07004134 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07004135#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07004136
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004137 if (wdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004138 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004139 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00004140 shdr->CreditRequest =
4141 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004142
4143 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4144 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004145 goto async_writev_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004146
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004147 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004148 }
4149
Pavel Shilovsky33319142012-09-18 16:20:29 -07004150 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08004151 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08004152 wdata, flags, &wdata->credits);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004153
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004154 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004155 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4156 tcon->tid, tcon->ses->Suid, wdata->offset,
4157 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06004158 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004159 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004160 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07004161
Pavel Shilovsky33319142012-09-18 16:20:29 -07004162async_writev_out:
4163 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004164 return rc;
4165}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004166
4167/*
4168 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4169 * The length field from io_parms must be at least 1 and indicates a number of
4170 * elements with data to write that begins with position 1 in iov array. All
4171 * data length is specified by count.
4172 */
4173int
4174SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4175 unsigned int *nbytes, struct kvec *iov, int n_vec)
4176{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004177 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004178 int rc = 0;
4179 struct smb2_write_req *req = NULL;
4180 struct smb2_write_rsp *rsp = NULL;
4181 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004182 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004183 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004184 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004185
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004186 *nbytes = 0;
4187
4188 if (n_vec < 1)
4189 return rc;
4190
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004191 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
4192 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004193 if (rc)
4194 return rc;
4195
4196 if (io_parms->tcon->ses->server == NULL)
4197 return -ECONNABORTED;
4198
Steve French5a77e752018-05-09 17:43:08 -05004199 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004200 flags |= CIFS_TRANSFORM_REQ;
4201
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004202 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004203
4204 req->PersistentFileId = io_parms->persistent_fid;
4205 req->VolatileFileId = io_parms->volatile_fid;
4206 req->WriteChannelInfoOffset = 0;
4207 req->WriteChannelInfoLength = 0;
4208 req->Channel = 0;
4209 req->Length = cpu_to_le32(io_parms->length);
4210 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004211 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004212 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004213 req->RemainingBytes = 0;
4214
Steve Frenchd323c2462019-02-25 00:52:43 -06004215 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4216 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4217 io_parms->offset, io_parms->length);
4218
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004219 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004220 /* 1 for Buffer */
4221 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004222
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004223 memset(&rqst, 0, sizeof(struct smb_rqst));
4224 rqst.rq_iov = iov;
4225 rqst.rq_nvec = n_vec + 1;
4226
4227 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004228 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004229 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004230
4231 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004232 trace_smb3_write_err(xid, req->PersistentFileId,
4233 io_parms->tcon->tid,
4234 io_parms->tcon->ses->Suid,
4235 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004236 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004237 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05004238 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004239 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05004240 trace_smb3_write_done(xid, req->PersistentFileId,
4241 io_parms->tcon->tid,
4242 io_parms->tcon->ses->Suid,
4243 io_parms->offset, *nbytes);
4244 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004245
ZhangXiaoxu6a3eb332019-04-06 15:47:38 +08004246 cifs_small_buf_release(req);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004247 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004248 return rc;
4249}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004250
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004251static unsigned int
4252num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
4253{
4254 int len;
4255 unsigned int entrycount = 0;
4256 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03004257 char *entryptr;
4258 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004259
4260 if (bufstart == NULL)
4261 return 0;
4262
Dan Carpenter56446f22018-09-06 12:48:22 +03004263 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004264
4265 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03004266 if (entryptr + next_offset < entryptr ||
4267 entryptr + next_offset > end_of_buf ||
4268 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004269 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004270 break;
4271 }
4272
Dan Carpenter56446f22018-09-06 12:48:22 +03004273 entryptr = entryptr + next_offset;
4274 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4275
4276 len = le32_to_cpu(dir_info->FileNameLength);
4277 if (entryptr + len < entryptr ||
4278 entryptr + len > end_of_buf ||
4279 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004280 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4281 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004282 break;
4283 }
4284
Dan Carpenter56446f22018-09-06 12:48:22 +03004285 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004286 entrycount++;
4287
Dan Carpenter56446f22018-09-06 12:48:22 +03004288 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004289 if (!next_offset)
4290 break;
4291 }
4292
4293 return entrycount;
4294}
4295
4296/*
4297 * Readdir/FindFirst
4298 */
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004299int SMB2_query_directory_init(const unsigned int xid,
4300 struct cifs_tcon *tcon, struct smb_rqst *rqst,
4301 u64 persistent_fid, u64 volatile_fid,
4302 int index, int info_level)
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004303{
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004304 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004305 struct smb2_query_directory_req *req;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004306 unsigned char *bufptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004307 __le16 asteriks = cpu_to_le16('*');
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004308 unsigned int output_size = CIFSMaxBufSize -
4309 MAX_SMB2_CREATE_RESPONSE_SIZE -
4310 MAX_SMB2_CLOSE_RESPONSE_SIZE;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004311 unsigned int total_len;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004312 struct kvec *iov = rqst->rq_iov;
4313 int len, rc;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004314
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004315 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4316 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004317 if (rc)
4318 return rc;
4319
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004320 switch (info_level) {
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004321 case SMB_FIND_FILE_DIRECTORY_INFO:
4322 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004323 break;
4324 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4325 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004326 break;
4327 default:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004328 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004329 info_level);
4330 return -EINVAL;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004331 }
4332
4333 req->FileIndex = cpu_to_le32(index);
4334 req->PersistentFileId = persistent_fid;
4335 req->VolatileFileId = volatile_fid;
4336
4337 len = 0x2;
4338 bufptr = req->Buffer;
4339 memcpy(bufptr, &asteriks, len);
4340
4341 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004342 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004343 req->FileNameLength = cpu_to_le16(len);
4344 /*
4345 * BB could be 30 bytes or so longer if we used SMB2 specific
4346 * buffer lengths, but this is safe and close enough.
4347 */
4348 output_size = min_t(unsigned int, output_size, server->maxBuf);
4349 output_size = min_t(unsigned int, output_size, 2 << 15);
4350 req->OutputBufferLength = cpu_to_le32(output_size);
4351
4352 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004353 /* 1 for Buffer */
4354 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004355
4356 iov[1].iov_base = (char *)(req->Buffer);
4357 iov[1].iov_len = len;
4358
Steve Frenchd323c2462019-02-25 00:52:43 -06004359 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4360 tcon->ses->Suid, index, output_size);
4361
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004362 return 0;
4363}
4364
4365void SMB2_query_directory_free(struct smb_rqst *rqst)
4366{
4367 if (rqst && rqst->rq_iov) {
4368 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4369 }
4370}
4371
4372int
4373SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4374 u64 persistent_fid, u64 volatile_fid, int index,
4375 struct cifs_search_info *srch_inf)
4376{
4377 struct smb_rqst rqst;
4378 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4379 struct smb2_query_directory_rsp *rsp = NULL;
4380 int resp_buftype = CIFS_NO_BUFFER;
4381 struct kvec rsp_iov;
4382 int rc = 0;
4383 struct TCP_Server_Info *server;
4384 struct cifs_ses *ses = tcon->ses;
4385 char *end_of_smb;
4386 size_t info_buf_size;
4387 int flags = 0;
4388
4389 if (ses && (ses->server))
4390 server = ses->server;
4391 else
4392 return -EIO;
4393
4394 if (smb3_encryption_required(tcon))
4395 flags |= CIFS_TRANSFORM_REQ;
4396
4397 memset(&rqst, 0, sizeof(struct smb_rqst));
4398 memset(&iov, 0, sizeof(iov));
4399 rqst.rq_iov = iov;
4400 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4401
4402 rc = SMB2_query_directory_init(xid, tcon, &rqst, persistent_fid,
4403 volatile_fid, index,
4404 srch_inf->info_level);
4405 if (rc)
4406 goto qdir_exit;
4407
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004408 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004409 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004410
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004411 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004412 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004413 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004414 trace_smb3_query_dir_done(xid, persistent_fid,
4415 tcon->tid, tcon->ses->Suid, index, 0);
Pavel Shilovsky52755802014-08-18 20:49:57 +04004416 srch_inf->endOfSearch = true;
4417 rc = 0;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004418 } else {
4419 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4420 tcon->ses->Suid, index, 0, rc);
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08004421 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004422 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004423 goto qdir_exit;
4424 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004425
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004426 switch (srch_inf->info_level) {
4427 case SMB_FIND_FILE_DIRECTORY_INFO:
4428 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4429 break;
4430 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4431 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4432 break;
4433 default:
4434 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4435 srch_inf->info_level);
4436 rc = -EINVAL;
4437 goto qdir_exit;
4438 }
4439
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004440 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4441 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4442 info_buf_size);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004443 if (rc) {
4444 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4445 tcon->ses->Suid, index, 0, rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004446 goto qdir_exit;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004447 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004448
4449 srch_inf->unicode = true;
4450
4451 if (srch_inf->ntwrk_buf_start) {
4452 if (srch_inf->smallBuf)
4453 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4454 else
4455 cifs_buf_release(srch_inf->ntwrk_buf_start);
4456 }
4457 srch_inf->ntwrk_buf_start = (char *)rsp;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004458 srch_inf->srch_entries_start = srch_inf->last_entry =
4459 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4460 end_of_smb = rsp_iov.iov_len + (char *)rsp;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004461 srch_inf->entries_in_buffer =
4462 num_entries(srch_inf->srch_entries_start, end_of_smb,
4463 &srch_inf->last_entry, info_buf_size);
4464 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05004465 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4466 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4467 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004468 if (resp_buftype == CIFS_LARGE_BUFFER)
4469 srch_inf->smallBuf = false;
4470 else if (resp_buftype == CIFS_SMALL_BUFFER)
4471 srch_inf->smallBuf = true;
4472 else
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004473 cifs_tcon_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004474
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004475 resp_buftype = CIFS_NO_BUFFER;
4476
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004477 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4478 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004479
4480qdir_exit:
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004481 SMB2_query_directory_free(&rqst);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004482 free_rsp_buf(resp_buftype, rsp);
4483 return rc;
4484}
4485
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004486int
4487SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004488 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004489 u8 info_type, u32 additional_info,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004490 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004491{
4492 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004493 struct kvec *iov = rqst->rq_iov;
4494 unsigned int i, total_len;
4495 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004496
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004497 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004498 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004499 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004500
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004501 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004502 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004503 req->FileInfoClass = info_class;
4504 req->PersistentFileId = persistent_fid;
4505 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004506 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004507
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004508 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004509 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004510 req->BufferLength = cpu_to_le32(*size);
4511
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004512 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004513 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004514
4515 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004516 /* 1 for Buffer */
4517 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004518
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004519 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004520 le32_add_cpu(&req->BufferLength, size[i]);
4521 iov[i].iov_base = (char *)data[i];
4522 iov[i].iov_len = size[i];
4523 }
4524
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004525 return 0;
4526}
4527
4528void
4529SMB2_set_info_free(struct smb_rqst *rqst)
4530{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10004531 if (rqst && rqst->rq_iov)
4532 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004533}
4534
4535static int
4536send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4537 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4538 u8 info_type, u32 additional_info, unsigned int num,
4539 void **data, unsigned int *size)
4540{
4541 struct smb_rqst rqst;
4542 struct smb2_set_info_rsp *rsp = NULL;
4543 struct kvec *iov;
4544 struct kvec rsp_iov;
4545 int rc = 0;
4546 int resp_buftype;
4547 struct cifs_ses *ses = tcon->ses;
4548 int flags = 0;
4549
4550 if (!ses || !(ses->server))
4551 return -EIO;
4552
4553 if (!num)
4554 return -EINVAL;
4555
4556 if (smb3_encryption_required(tcon))
4557 flags |= CIFS_TRANSFORM_REQ;
4558
4559 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4560 if (!iov)
4561 return -ENOMEM;
4562
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004563 memset(&rqst, 0, sizeof(struct smb_rqst));
4564 rqst.rq_iov = iov;
4565 rqst.rq_nvec = num;
4566
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004567 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4568 info_class, info_type, additional_info,
4569 data, size);
4570 if (rc) {
4571 kfree(iov);
4572 return rc;
4573 }
4574
4575
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004576 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004577 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004578 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004579 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004580
Steve Frencheccb4422018-05-17 21:16:55 -05004581 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004582 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004583 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4584 ses->Suid, info_class, (__u32)info_type, rc);
4585 }
Steve French7d3fb242013-11-18 09:56:28 -06004586
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004587 free_rsp_buf(resp_buftype, rsp);
4588 kfree(iov);
4589 return rc;
4590}
4591
4592int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004593SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004594 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004595{
4596 struct smb2_file_eof_info info;
4597 void *data;
4598 unsigned int size;
4599
4600 info.EndOfFile = *eof;
4601
4602 data = &info;
4603 size = sizeof(struct smb2_file_eof_info);
4604
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004605 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004606 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4607 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004608}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004609
4610int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004611SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4612 u64 persistent_fid, u64 volatile_fid,
4613 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4614{
4615 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4616 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4617 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004618}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004619
4620int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004621SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4622 u64 persistent_fid, u64 volatile_fid,
4623 struct smb2_file_full_ea_info *buf, int len)
4624{
4625 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4626 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4627 0, 1, (void **)&buf, &len);
4628}
4629
4630int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004631SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4632 const u64 persistent_fid, const u64 volatile_fid,
4633 __u8 oplock_level)
4634{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004635 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004636 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10004637 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004638 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004639 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004640 unsigned int total_len;
4641 struct kvec iov[1];
4642 struct kvec rsp_iov;
4643 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004644
Joe Perchesf96637b2013-05-04 22:12:25 -05004645 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004646 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4647 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004648 if (rc)
4649 return rc;
4650
Steve French5a77e752018-05-09 17:43:08 -05004651 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004652 flags |= CIFS_TRANSFORM_REQ;
4653
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004654 req->VolatileFid = volatile_fid;
4655 req->PersistentFid = persistent_fid;
4656 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004657 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004658
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004659 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004660
4661 iov[0].iov_base = (char *)req;
4662 iov[0].iov_len = total_len;
4663
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004664 memset(&rqst, 0, sizeof(struct smb_rqst));
4665 rqst.rq_iov = iov;
4666 rqst.rq_nvec = 1;
4667
4668 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004669 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004670
4671 if (rc) {
4672 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004673 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004674 }
4675
4676 return rc;
4677}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004678
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004679void
4680smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4681 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004682{
4683 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4684 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4685 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05304686 kst->f_bfree = kst->f_bavail =
4687 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004688 return;
4689}
4690
Steve French2d304212018-06-24 23:28:12 -05004691static void
4692copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4693 struct kstatfs *kst)
4694{
4695 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4696 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4697 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4698 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4699 kst->f_bavail = kst->f_bfree;
4700 else
4701 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4702 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4703 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4704 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4705 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4706
4707 return;
4708}
Steve French2d304212018-06-24 23:28:12 -05004709
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004710static int
4711build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4712 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4713{
4714 int rc;
4715 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004716 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004717
Joe Perchesf96637b2013-05-04 22:12:25 -05004718 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004719
4720 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4721 return -EIO;
4722
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004723 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4724 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004725 if (rc)
4726 return rc;
4727
4728 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4729 req->FileInfoClass = level;
4730 req->PersistentFileId = persistent_fid;
4731 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004732 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004733 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004734 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004735 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004736 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004737
4738 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004739 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004740 return 0;
4741}
4742
Steve French2d304212018-06-24 23:28:12 -05004743int
4744SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4745 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4746{
4747 struct smb_rqst rqst;
4748 struct smb2_query_info_rsp *rsp = NULL;
4749 struct kvec iov;
4750 struct kvec rsp_iov;
4751 int rc = 0;
4752 int resp_buftype;
4753 struct cifs_ses *ses = tcon->ses;
4754 FILE_SYSTEM_POSIX_INFO *info = NULL;
4755 int flags = 0;
4756
4757 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4758 sizeof(FILE_SYSTEM_POSIX_INFO),
4759 persistent_fid, volatile_fid);
4760 if (rc)
4761 return rc;
4762
4763 if (smb3_encryption_required(tcon))
4764 flags |= CIFS_TRANSFORM_REQ;
4765
4766 memset(&rqst, 0, sizeof(struct smb_rqst));
4767 rqst.rq_iov = &iov;
4768 rqst.rq_nvec = 1;
4769
4770 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4771 cifs_small_buf_release(iov.iov_base);
4772 if (rc) {
4773 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4774 goto posix_qfsinf_exit;
4775 }
4776 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4777
4778 info = (FILE_SYSTEM_POSIX_INFO *)(
4779 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004780 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4781 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4782 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05004783 if (!rc)
4784 copy_posix_fs_info_to_kstatfs(info, fsdata);
4785
4786posix_qfsinf_exit:
4787 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4788 return rc;
4789}
Steve French2d304212018-06-24 23:28:12 -05004790
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004791int
4792SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4793 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4794{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004795 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004796 struct smb2_query_info_rsp *rsp = NULL;
4797 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004798 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004799 int rc = 0;
4800 int resp_buftype;
4801 struct cifs_ses *ses = tcon->ses;
4802 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004803 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004804
4805 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4806 sizeof(struct smb2_fs_full_size_info),
4807 persistent_fid, volatile_fid);
4808 if (rc)
4809 return rc;
4810
Steve French5a77e752018-05-09 17:43:08 -05004811 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004812 flags |= CIFS_TRANSFORM_REQ;
4813
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004814 memset(&rqst, 0, sizeof(struct smb_rqst));
4815 rqst.rq_iov = &iov;
4816 rqst.rq_nvec = 1;
4817
4818 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004819 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004820 if (rc) {
4821 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05004822 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004823 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004824 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004825
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004826 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004827 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004828 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4829 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4830 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004831 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004832 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004833
Steve French34f62642013-10-09 02:07:00 -05004834qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004835 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004836 return rc;
4837}
4838
4839int
4840SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05004841 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05004842{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004843 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05004844 struct smb2_query_info_rsp *rsp = NULL;
4845 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004846 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05004847 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05004848 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05004849 struct cifs_ses *ses = tcon->ses;
4850 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004851 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05004852
Steven French21671142013-10-09 13:36:35 -05004853 if (level == FS_DEVICE_INFORMATION) {
4854 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4855 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4856 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4857 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4858 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004859 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4860 max_len = sizeof(struct smb3_fs_ss_info);
4861 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05004862 } else if (level == FS_VOLUME_INFORMATION) {
4863 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4864 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05004865 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004866 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05004867 return -EINVAL;
4868 }
4869
4870 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05004871 persistent_fid, volatile_fid);
4872 if (rc)
4873 return rc;
4874
Steve French5a77e752018-05-09 17:43:08 -05004875 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004876 flags |= CIFS_TRANSFORM_REQ;
4877
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004878 memset(&rqst, 0, sizeof(struct smb_rqst));
4879 rqst.rq_iov = &iov;
4880 rqst.rq_nvec = 1;
4881
4882 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004883 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004884 if (rc) {
4885 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4886 goto qfsattr_exit;
4887 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004888 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05004889
4890 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4891 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004892 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05004893 if (rc)
4894 goto qfsattr_exit;
4895
4896 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004897 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004898 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05004899 rsp_len, max_len));
4900 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004901 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004902 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004903 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4904 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004905 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004906 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4907 tcon->perf_sector_size =
4908 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05004909 } else if (level == FS_VOLUME_INFORMATION) {
4910 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4911 (offset + (char *)rsp);
4912 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4913 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004914 }
Steve French34f62642013-10-09 02:07:00 -05004915
4916qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004917 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004918 return rc;
4919}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004920
4921int
4922smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4923 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4924 const __u32 num_lock, struct smb2_lock_element *buf)
4925{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004926 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004927 int rc = 0;
4928 struct smb2_lock_req *req = NULL;
4929 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004930 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004931 int resp_buf_type;
4932 unsigned int count;
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004933 int flags = CIFS_NO_RSP_BUF;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004934 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004935
Joe Perchesf96637b2013-05-04 22:12:25 -05004936 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004937
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004938 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004939 if (rc)
4940 return rc;
4941
Steve French5a77e752018-05-09 17:43:08 -05004942 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004943 flags |= CIFS_TRANSFORM_REQ;
4944
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004945 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004946 req->LockCount = cpu_to_le16(num_lock);
4947
4948 req->PersistentFileId = persist_fid;
4949 req->VolatileFileId = volatile_fid;
4950
4951 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004952
4953 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004954 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004955 iov[1].iov_base = (char *)buf;
4956 iov[1].iov_len = count;
4957
4958 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004959
4960 memset(&rqst, 0, sizeof(struct smb_rqst));
4961 rqst.rq_iov = iov;
4962 rqst.rq_nvec = 2;
4963
4964 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004965 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004966 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004967 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004968 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004969 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004970 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4971 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004972 }
4973
4974 return rc;
4975}
4976
4977int
4978SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4979 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4980 const __u64 length, const __u64 offset, const __u32 lock_flags,
4981 const bool wait)
4982{
4983 struct smb2_lock_element lock;
4984
4985 lock.Offset = cpu_to_le64(offset);
4986 lock.Length = cpu_to_le64(length);
4987 lock.Flags = cpu_to_le32(lock_flags);
4988 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4989 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4990
4991 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4992}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004993
4994int
4995SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4996 __u8 *lease_key, const __le32 lease_state)
4997{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004998 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004999 int rc;
5000 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005001 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005002 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005003 unsigned int total_len;
5004 struct kvec iov[1];
5005 struct kvec rsp_iov;
5006 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05005007 __u64 *please_key_high;
5008 __u64 *please_key_low;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005009
Joe Perchesf96637b2013-05-04 22:12:25 -05005010 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005011 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
5012 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005013 if (rc)
5014 return rc;
5015
Steve French5a77e752018-05-09 17:43:08 -05005016 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005017 flags |= CIFS_TRANSFORM_REQ;
5018
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005019 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005020 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005021 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005022
5023 memcpy(req->LeaseKey, lease_key, 16);
5024 req->LeaseState = lease_state;
5025
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10005026 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005027
5028 iov[0].iov_base = (char *)req;
5029 iov[0].iov_len = total_len;
5030
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005031 memset(&rqst, 0, sizeof(struct smb_rqst));
5032 rqst.rq_iov = iov;
5033 rqst.rq_nvec = 1;
5034
5035 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005036 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005037
Aurelien Apteld339adc2019-01-31 13:46:07 +01005038 please_key_low = (__u64 *)lease_key;
5039 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005040 if (rc) {
5041 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05005042 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5043 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05005044 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05005045 } else
5046 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5047 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005048
5049 return rc;
5050}