blob: 05149862aea42e6a33736fe77856d8a822af4480 [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
Pavel Shilovskye99c63e2019-07-22 11:34:59 -0700255 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300256 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400257
258 if (tcon->tidStatus == CifsExiting) {
259 /*
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
263 */
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
268 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400269 return -ENODEV;
270 }
271 }
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
274 return -EIO;
275
276 ses = tcon->ses;
277 server = ses->server;
278
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200279 retries = server->nr_targets;
280
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400281 /*
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200282 * Give demultiplex thread up to 10 seconds to each target available for
283 * reconnect -- should be greater than cifs socket timeout which is 7
284 * seconds.
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400285 */
286 while (server->tcpStatus == CifsNeedReconnect) {
287 /*
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
290 */
291 switch (smb2_command) {
292 /*
293 * BB Should we keep oplock break and add flush to exceptions?
294 */
295 case SMB2_TREE_DISCONNECT:
296 case SMB2_CANCEL:
297 case SMB2_CLOSE:
298 case SMB2_OPLOCK_BREAK:
299 return -EAGAIN;
300 }
301
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
304 10 * HZ);
305 if (rc < 0) {
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
308 return -ERESTARTSYS;
309 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400310
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
313 break;
314
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200315 if (--retries)
316 continue;
317
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400318 /*
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
321 * back on-line
322 */
323 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400325 return -EHOSTDOWN;
326 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200327 retries = server->nr_targets;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400328 }
329
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300331 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400332
333 nls_codepage = load_nls_default();
334
335 /*
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
338 */
339 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200340
341 /*
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
345 */
346 if (server->tcpStatus == CifsNeedReconnect) {
347 rc = -EHOSTDOWN;
348 mutex_unlock(&tcon->ses->session_mutex);
349 goto out;
350 }
351
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400352 rc = cifs_negotiate_protocol(0, tcon->ses);
353 if (!rc && tcon->ses->need_reconnect)
354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
355
356 if (rc || !tcon->need_reconnect) {
357 mutex_unlock(&tcon->ses->session_mutex);
358 goto out;
359 }
360
361 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800362 if (tcon->use_persistent)
363 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500364
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200365 rc = __smb2_reconnect(nls_codepage, tcon);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400366 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500367
Joe Perchesf96637b2013-05-04 22:12:25 -0500368 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500369 if (rc) {
370 /* If sess reconnected but tcon didn't, something strange ... */
371 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400372 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500373 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800374
375 if (smb2_command != SMB2_INTERNAL_CMD)
376 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
377
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400378 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400379out:
380 /*
381 * Check if handle based operation so we know whether we can continue
382 * or not without returning to caller to reset file handle.
383 */
384 /*
385 * BB Is flush done by server on drop of tcp session? Should we special
386 * case it and skip above?
387 */
388 switch (smb2_command) {
389 case SMB2_FLUSH:
390 case SMB2_READ:
391 case SMB2_WRITE:
392 case SMB2_LOCK:
393 case SMB2_IOCTL:
394 case SMB2_QUERY_DIRECTORY:
395 case SMB2_CHANGE_NOTIFY:
396 case SMB2_QUERY_INFO:
397 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800398 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400399 }
400 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400401 return rc;
402}
403
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700404static void
405fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
406 unsigned int *total_len)
407{
408 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
409 /* lookup word count ie StructureSize from table */
410 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
411
412 /*
413 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
414 * largest operations (Create)
415 */
416 memset(buf, 0, 256);
417
418 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
419 spdu->StructureSize2 = cpu_to_le16(parmsize);
420
421 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
422}
423
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100424/*
425 * Allocate and return pointer to an SMB request hdr, and set basic
426 * SMB information in the SMB header. If the return code is zero, this
427 * function must have filled in request_buf pointer.
428 */
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800429static int
430smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
431 void **request_buf, unsigned int *total_len)
432{
433 int rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800434
435 rc = smb2_reconnect(smb2_command, tcon);
436 if (rc)
437 return rc;
438
439 /* BB eventually switch this to SMB2 specific small buf size */
Stefano Briviof46ecbd2018-07-05 11:46:42 +0200440 if (smb2_command == SMB2_SET_INFO)
441 *request_buf = cifs_buf_get();
442 else
443 *request_buf = cifs_small_buf_get();
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800444 if (*request_buf == NULL) {
445 /* BB should we add a retry in here if not a writepage? */
446 return -ENOMEM;
447 }
448
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100449 fill_small_buf(smb2_command, tcon,
450 (struct smb2_sync_hdr *)(*request_buf),
451 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400452
453 if (tcon != NULL) {
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400454 uint16_t com_code = le16_to_cpu(smb2_command);
455 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400456 cifs_stats_inc(&tcon->num_smbs_sent);
457 }
458
459 return rc;
460}
461
Steve Frenchd7bef4c2019-04-18 11:03:58 -0500462/* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500463
464static void
465build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
466{
467 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
468 pneg_ctxt->DataLength = cpu_to_le16(38);
469 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
470 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
471 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
472 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
473}
474
475static void
Steve French26ea8882019-04-26 20:36:08 -0700476build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
477{
478 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
479 pneg_ctxt->DataLength =
480 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
481 - sizeof(struct smb2_neg_context));
482 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
483 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
484 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
485 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
486}
487
488static void
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500489build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
490{
491 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French9ac63ec2019-06-07 08:59:40 -0500492 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
493 pneg_ctxt->CipherCount = cpu_to_le16(2);
494 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
495 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500496}
497
Steve French96d3cca2019-06-25 04:39:51 -0500498static unsigned int
499build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
500{
501 struct nls_table *cp = load_nls_default();
502
503 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
504
505 /* copy up to max of first 100 bytes of server name to NetName field */
Steve Frenchdf58fae2019-08-05 17:07:26 -0500506 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
Steve French96d3cca2019-06-25 04:39:51 -0500507 /* context size is DataLength + minimal smb2_neg_context */
508 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
509 sizeof(struct smb2_neg_context), 8) * 8;
510}
511
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500512static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500513build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
514{
515 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
516 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
Steve French0d481322019-02-24 17:56:33 -0600517 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
518 pneg_ctxt->Name[0] = 0x93;
519 pneg_ctxt->Name[1] = 0xAD;
520 pneg_ctxt->Name[2] = 0x25;
521 pneg_ctxt->Name[3] = 0x50;
522 pneg_ctxt->Name[4] = 0x9C;
523 pneg_ctxt->Name[5] = 0xB4;
524 pneg_ctxt->Name[6] = 0x11;
525 pneg_ctxt->Name[7] = 0xE7;
526 pneg_ctxt->Name[8] = 0xB4;
527 pneg_ctxt->Name[9] = 0x23;
528 pneg_ctxt->Name[10] = 0x83;
529 pneg_ctxt->Name[11] = 0xDE;
530 pneg_ctxt->Name[12] = 0x96;
531 pneg_ctxt->Name[13] = 0x8B;
532 pneg_ctxt->Name[14] = 0xCD;
533 pneg_ctxt->Name[15] = 0x7C;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500534}
535
536static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100537assemble_neg_contexts(struct smb2_negotiate_req *req,
Steve French9fe5ff12019-06-24 20:39:04 -0500538 struct TCP_Server_Info *server, unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500539{
Steve Frenchd5c70762019-01-03 02:37:21 -0600540 char *pneg_ctxt = (char *)req;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500541 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500542
Steve Frenchd5c70762019-01-03 02:37:21 -0600543 if (*total_len > 200) {
544 /* In case length corrupted don't want to overrun smb buffer */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000545 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
Steve Frenchd5c70762019-01-03 02:37:21 -0600546 return;
547 }
548
549 /*
550 * round up total_len of fixed part of SMB3 negotiate request to 8
551 * byte boundary before adding negotiate contexts
552 */
553 *total_len = roundup(*total_len, 8);
554
555 pneg_ctxt = (*total_len) + (char *)req;
556 req->NegotiateContextOffset = cpu_to_le32(*total_len);
557
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500558 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500559 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
560 *total_len += ctxt_len;
561 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100562
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500563 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500564 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
565 *total_len += ctxt_len;
566 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100567
Steve French9fe5ff12019-06-24 20:39:04 -0500568 if (server->compress_algorithm) {
569 build_compression_ctxt((struct smb2_compression_capabilities_context *)
Steve French26ea8882019-04-26 20:36:08 -0700570 pneg_ctxt);
Steve French9fe5ff12019-06-24 20:39:04 -0500571 ctxt_len = DIV_ROUND_UP(
572 sizeof(struct smb2_compression_capabilities_context),
573 8) * 8;
574 *total_len += ctxt_len;
575 pneg_ctxt += ctxt_len;
Steve French96d3cca2019-06-25 04:39:51 -0500576 req->NegotiateContextCount = cpu_to_le16(5);
Steve French9fe5ff12019-06-24 20:39:04 -0500577 } else
Steve French96d3cca2019-06-25 04:39:51 -0500578 req->NegotiateContextCount = cpu_to_le16(4);
579
580 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
581 server->hostname);
582 *total_len += ctxt_len;
583 pneg_ctxt += ctxt_len;
584
Steve Frenchfcef0db2018-05-19 20:45:27 -0500585 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
586 *total_len += sizeof(struct smb2_posix_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500587}
Steve French5100d8a2018-04-09 10:47:14 -0500588
589static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
590{
591 unsigned int len = le16_to_cpu(ctxt->DataLength);
592
593 /* If invalid preauth context warn but use what we requested, SHA-512 */
594 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
595 printk_once(KERN_WARNING "server sent bad preauth context\n");
596 return;
597 }
598 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
599 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
600 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
601 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
602}
603
Steve French26ea8882019-04-26 20:36:08 -0700604static void decode_compress_ctx(struct TCP_Server_Info *server,
605 struct smb2_compression_capabilities_context *ctxt)
606{
607 unsigned int len = le16_to_cpu(ctxt->DataLength);
608
609 /* sizeof compress context is a one element compression capbility struct */
610 if (len < 10) {
611 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
612 return;
613 }
614 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
615 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
616 return;
617 }
618 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
619 printk_once(KERN_WARNING "unknown compression algorithm\n");
620 return;
621 }
622 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
623}
624
Steve French5100d8a2018-04-09 10:47:14 -0500625static int decode_encrypt_ctx(struct TCP_Server_Info *server,
626 struct smb2_encryption_neg_context *ctxt)
627{
628 unsigned int len = le16_to_cpu(ctxt->DataLength);
629
630 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
631 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
632 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
633 return -EINVAL;
634 }
635
636 if (le16_to_cpu(ctxt->CipherCount) != 1) {
637 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
638 return -EINVAL;
639 }
640 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
641 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
642 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
643 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
644 return -EINVAL;
645 }
646 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500647 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500648 return 0;
649}
650
651static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000652 struct TCP_Server_Info *server,
653 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500654{
655 struct smb2_neg_context *pctx;
656 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
657 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500658 unsigned int len_of_ctxts, i;
659 int rc = 0;
660
661 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
662 if (len_of_smb <= offset) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000663 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
Steve French5100d8a2018-04-09 10:47:14 -0500664 return -EINVAL;
665 }
666
667 len_of_ctxts = len_of_smb - offset;
668
669 for (i = 0; i < ctxt_cnt; i++) {
670 int clen;
671 /* check that offset is not beyond end of SMB */
672 if (len_of_ctxts == 0)
673 break;
674
675 if (len_of_ctxts < sizeof(struct smb2_neg_context))
676 break;
677
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000678 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500679 clen = le16_to_cpu(pctx->DataLength);
680 if (clen > len_of_ctxts)
681 break;
682
683 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
684 decode_preauth_context(
685 (struct smb2_preauth_neg_context *)pctx);
686 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
687 rc = decode_encrypt_ctx(server,
688 (struct smb2_encryption_neg_context *)pctx);
Steve French26ea8882019-04-26 20:36:08 -0700689 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
690 decode_compress_ctx(server,
691 (struct smb2_compression_capabilities_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500692 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
693 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500694 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000695 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
Steve French5100d8a2018-04-09 10:47:14 -0500696 le16_to_cpu(pctx->ContextType));
697
698 if (rc)
699 break;
700 /* offsets must be 8 byte aligned */
701 clen = (clen + 7) & ~0x7;
702 offset += clen + sizeof(struct smb2_neg_context);
703 len_of_ctxts -= clen;
704 }
705 return rc;
706}
707
Steve Frenchce558b02018-05-31 19:16:54 -0500708static struct create_posix *
709create_posix_buf(umode_t mode)
710{
711 struct create_posix *buf;
712
713 buf = kzalloc(sizeof(struct create_posix),
714 GFP_KERNEL);
715 if (!buf)
716 return NULL;
717
718 buf->ccontext.DataOffset =
719 cpu_to_le16(offsetof(struct create_posix, Mode));
720 buf->ccontext.DataLength = cpu_to_le32(4);
721 buf->ccontext.NameOffset =
722 cpu_to_le16(offsetof(struct create_posix, Name));
723 buf->ccontext.NameLength = cpu_to_le16(16);
724
725 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
726 buf->Name[0] = 0x93;
727 buf->Name[1] = 0xAD;
728 buf->Name[2] = 0x25;
729 buf->Name[3] = 0x50;
730 buf->Name[4] = 0x9C;
731 buf->Name[5] = 0xB4;
732 buf->Name[6] = 0x11;
733 buf->Name[7] = 0xE7;
734 buf->Name[8] = 0xB4;
735 buf->Name[9] = 0x23;
736 buf->Name[10] = 0x83;
737 buf->Name[11] = 0xDE;
738 buf->Name[12] = 0x96;
739 buf->Name[13] = 0x8B;
740 buf->Name[14] = 0xCD;
741 buf->Name[15] = 0x7C;
742 buf->Mode = cpu_to_le32(mode);
743 cifs_dbg(FYI, "mode on posix create 0%o", mode);
744 return buf;
745}
746
747static int
748add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
749{
750 struct smb2_create_req *req = iov[0].iov_base;
751 unsigned int num = *num_iovec;
752
753 iov[num].iov_base = create_posix_buf(mode);
Steve Frenchd0959b02019-10-05 10:53:58 -0500754 if (mode == ACL_NO_MODE)
755 cifs_dbg(FYI, "illegal mode\n");
Steve Frenchce558b02018-05-31 19:16:54 -0500756 if (iov[num].iov_base == NULL)
757 return -ENOMEM;
758 iov[num].iov_len = sizeof(struct create_posix);
759 if (!req->CreateContextsOffset)
760 req->CreateContextsOffset = cpu_to_le32(
761 sizeof(struct smb2_create_req) +
762 iov[num - 1].iov_len);
763 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
764 *num_iovec = num + 1;
765 return 0;
766}
767
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500768
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400769/*
770 *
771 * SMB2 Worker functions follow:
772 *
773 * The general structure of the worker functions is:
774 * 1) Call smb2_init (assembles SMB2 header)
775 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
776 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
777 * 4) Decode SMB2 command specific fields in the fixed length area
778 * 5) Decode variable length data area (if any for this SMB2 command type)
779 * 6) Call free smb buffer
780 * 7) return
781 *
782 */
783
784int
785SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
786{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000787 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400788 struct smb2_negotiate_req *req;
789 struct smb2_negotiate_rsp *rsp;
790 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700791 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400792 int rc = 0;
793 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400794 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400795 int blob_offset, blob_length;
796 char *security_blob;
797 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100798 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400799
Joe Perchesf96637b2013-05-04 22:12:25 -0500800 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400801
Jeff Layton3534b852013-05-24 07:41:01 -0400802 if (!server) {
803 WARN(1, "%s: server is NULL!\n", __func__);
804 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400805 }
806
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100807 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400808 if (rc)
809 return rc;
810
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100811 req->sync_hdr.SessionId = 0;
Steve French0fdfef92018-06-28 19:30:23 -0500812
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100813 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
814 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400815
Steve French9764c022017-09-17 10:41:35 -0500816 if (strcmp(ses->server->vals->version_string,
817 SMB3ANY_VERSION_STRING) == 0) {
818 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
819 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
820 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100821 total_len += 4;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000822 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500823 SMBDEFAULT_VERSION_STRING) == 0) {
824 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
825 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
826 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600827 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
828 req->DialectCount = cpu_to_le16(4);
829 total_len += 8;
Steve French9764c022017-09-17 10:41:35 -0500830 } else {
831 /* otherwise send specific dialect */
832 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
833 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100834 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500835 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400836
837 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400838 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500839 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400840 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500841 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400842 else
843 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400844
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000845 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400846
Steve French3c5f9be12014-05-13 13:37:45 -0700847 /* ClientGUID must be zero for SMB2.02 dialect */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000848 if (server->vals->protocol_id == SMB20_PROT_ID)
Steve French3c5f9be12014-05-13 13:37:45 -0700849 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500850 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700851 memcpy(req->ClientGUID, server->client_guid,
852 SMB2_CLIENT_GUID_SIZE);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000853 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
854 (strcmp(server->vals->version_string,
Steve Frenchd5c70762019-01-03 02:37:21 -0600855 SMBDEFAULT_VERSION_STRING) == 0))
Steve French9fe5ff12019-06-24 20:39:04 -0500856 assemble_neg_contexts(req, server, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500857 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400858 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100859 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400860
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000861 memset(&rqst, 0, sizeof(struct smb_rqst));
862 rqst.rq_iov = iov;
863 rqst.rq_nvec = 1;
864
865 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700866 cifs_small_buf_release(req);
867 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400868 /*
869 * No tcon so can't do
870 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
871 */
Steve French7e682f72017-08-31 21:34:24 -0500872 if (rc == -EOPNOTSUPP) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000873 cifs_server_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500874 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500875 " older servers\n");
876 goto neg_exit;
877 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400878 goto neg_exit;
879
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000880 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500881 SMB3ANY_VERSION_STRING) == 0) {
882 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000883 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500884 "SMB2 dialect returned but not requested\n");
885 return -EIO;
886 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000887 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500888 "SMB2.1 dialect returned but not requested\n");
889 return -EIO;
890 }
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000891 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500892 SMBDEFAULT_VERSION_STRING) == 0) {
893 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000894 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500895 "SMB2 dialect returned but not requested\n");
896 return -EIO;
897 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
898 /* ops set to 3.0 by default for default so update */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000899 server->ops = &smb21_operations;
900 server->vals = &smb21_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800901 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000902 server->ops = &smb311_operations;
903 server->vals = &smb311_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800904 }
Steve French590d08d2017-09-19 11:43:47 -0500905 } else if (le16_to_cpu(rsp->DialectRevision) !=
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000906 server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500907 /* if requested single dialect ensure returned dialect matched */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000908 cifs_server_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500909 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500910 return -EIO;
911 }
912
Joe Perchesf96637b2013-05-04 22:12:25 -0500913 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400914
Steve Frenche4aa25e2012-10-01 12:26:22 -0500915 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500916 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500917 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500918 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500919 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500920 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500921 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
922 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600923 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
924 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400925 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000926 cifs_server_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500927 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400928 rc = -EIO;
929 goto neg_exit;
930 }
931 server->dialect = le16_to_cpu(rsp->DialectRevision);
932
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100933 /*
934 * Keep a copy of the hash after negprot. This hash will be
935 * the starting hash value for all sessions made from this
936 * server.
937 */
938 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
939 SMB2_PREAUTH_HASH_SIZE);
Steve French0fdfef92018-06-28 19:30:23 -0500940
Jeff Laytone598d1d82013-05-26 07:00:59 -0400941 /* SMB2 only has an extended negflavor */
942 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400943 /* set it to the maximum buffer size value we can send with 1 credit */
944 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
945 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400946 server->max_read = le32_to_cpu(rsp->MaxReadSize);
947 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400948 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500949 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
950 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
951 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400952 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400953 /* Internal types */
954 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400955
956 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000957 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500958 /*
959 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
960 * for us will be
961 * ses->sectype = RawNTLMSSP;
962 * but for time being this is our only auth choice so doesn't matter.
963 * We just found a server which sets blob length to zero expecting raw.
964 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700965 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500966 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700967 server->sec_ntlmssp = true;
968 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700969
Jeff Layton38d77c52013-05-26 07:01:00 -0400970 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400971 if (rc)
972 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500973 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500974 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500975 if (rc == 1)
976 rc = 0;
977 else if (rc == 0)
978 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400979 }
Steve French5100d8a2018-04-09 10:47:14 -0500980
Steve French5100d8a2018-04-09 10:47:14 -0500981 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
982 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000983 rc = smb311_decode_neg_context(rsp, server,
984 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -0500985 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000986 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
Steve French5100d8a2018-04-09 10:47:14 -0500987 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400988neg_exit:
989 free_rsp_buf(resp_buftype, rsp);
990 return rc;
991}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400992
Steve Frenchff1c0382013-11-19 23:44:46 -0600993int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
994{
Long Li2796d302018-04-25 11:30:04 -0700995 int rc;
996 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200997 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600998 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500999 u32 inbuflen; /* max of 4 dialects */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001000 struct TCP_Server_Info *server = tcon->ses->server;
Steve Frenchff1c0382013-11-19 23:44:46 -06001001
1002 cifs_dbg(FYI, "validate negotiate\n");
1003
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001004 /* In SMB3.11 preauth integrity supersedes validate negotiate */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001005 if (server->dialect == SMB311_PROT_ID)
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001006 return 0;
1007
Steve Frenchff1c0382013-11-19 23:44:46 -06001008 /*
1009 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -05001010 * can not sign it (ie are not known user). Even if signing is not
1011 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -06001012 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -05001013 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -06001014 */
Steve French0603c962017-09-20 19:57:18 -05001015 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -06001016 return 0; /* validation requires signing */
1017
Steve French0603c962017-09-20 19:57:18 -05001018 if (tcon->ses->user_name == NULL) {
1019 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1020 return 0; /* validation requires signing */
1021 }
1022
1023 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001024 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
Steve French0603c962017-09-20 19:57:18 -05001025
Long Li2796d302018-04-25 11:30:04 -07001026 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1027 if (!pneg_inbuf)
1028 return -ENOMEM;
1029
1030 pneg_inbuf->Capabilities =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001031 cpu_to_le32(server->vals->req_capabilities);
1032 memcpy(pneg_inbuf->Guid, server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +01001033 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -06001034
1035 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -07001036 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001037 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1038 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -07001039 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001040 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1041 else
Long Li2796d302018-04-25 11:30:04 -07001042 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001043
Steve French9764c022017-09-17 10:41:35 -05001044
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001045 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001046 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001047 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1048 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1049 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -05001050 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -07001051 inbuflen = sizeof(*pneg_inbuf) -
Steve Frenchd5c70762019-01-03 02:37:21 -06001052 (2 * sizeof(pneg_inbuf->Dialects[0]));
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001053 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001054 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001055 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1056 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1057 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -06001058 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1059 pneg_inbuf->DialectCount = cpu_to_le16(4);
Steve French9764c022017-09-17 10:41:35 -05001060 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -07001061 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -05001062 } else {
1063 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -07001064 pneg_inbuf->Dialects[0] =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001065 cpu_to_le16(server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -07001066 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -05001067 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -07001068 inbuflen = sizeof(*pneg_inbuf) -
1069 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -05001070 }
Steve Frenchff1c0382013-11-19 23:44:46 -06001071
1072 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1073 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001074 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1075 (char **)&pneg_rsp, &rsplen);
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001076 if (rc == -EOPNOTSUPP) {
1077 /*
1078 * Old Windows versions or Netapp SMB server can return
1079 * not supported error. Client should accept it.
1080 */
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001081 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
Colin Ian King21078202019-05-17 09:12:33 +01001082 rc = 0;
1083 goto out_free_inbuf;
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001084 } else if (rc != 0) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001085 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -07001086 rc = -EIO;
1087 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -06001088 }
1089
Long Li2796d302018-04-25 11:30:04 -07001090 rc = -EIO;
1091 if (rsplen != sizeof(*pneg_rsp)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001092 cifs_tcon_dbg(VFS, "invalid protocol negotiate response size: %d\n",
Steve French7db0a6e2017-05-03 21:12:20 -05001093 rsplen);
1094
1095 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -07001096 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1097 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001098 }
1099
1100 /* check validate negotiate info response matches what we got earlier */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001101 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -06001102 goto vneg_out;
1103
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001104 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
Steve Frenchff1c0382013-11-19 23:44:46 -06001105 goto vneg_out;
1106
1107 /* do not validate server guid because not saved at negprot time yet */
1108
1109 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001110 SMB2_LARGE_FILES) != server->capabilities)
Steve Frenchff1c0382013-11-19 23:44:46 -06001111 goto vneg_out;
1112
1113 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -07001114 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001115 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -07001116 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001117
1118vneg_out:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001119 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -07001120out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001121 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -07001122out_free_inbuf:
1123 kfree(pneg_inbuf);
1124 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -06001125}
1126
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301127enum securityEnum
1128smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1129{
1130 switch (requested) {
1131 case Kerberos:
1132 case RawNTLMSSP:
1133 return requested;
1134 case NTLMv2:
1135 return RawNTLMSSP;
1136 case Unspecified:
1137 if (server->sec_ntlmssp &&
1138 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1139 return RawNTLMSSP;
1140 if ((server->sec_kerberos || server->sec_mskerberos) &&
1141 (global_secflags & CIFSSEC_MAY_KRB5))
1142 return Kerberos;
1143 /* Fallthrough */
1144 default:
1145 return Unspecified;
1146 }
1147}
1148
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001149struct SMB2_sess_data {
1150 unsigned int xid;
1151 struct cifs_ses *ses;
1152 struct nls_table *nls_cp;
1153 void (*func)(struct SMB2_sess_data *);
1154 int result;
1155 u64 previous_session;
1156
1157 /* we will send the SMB in three pieces:
1158 * a fixed length beginning part, an optional
1159 * SPNEGO blob (which can be zero length), and a
1160 * last part which will include the strings
1161 * and rest of bcc area. This allows us to avoid
1162 * a large buffer 17K allocation
1163 */
1164 int buf0_type;
1165 struct kvec iov[2];
1166};
1167
1168static int
1169SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1170{
1171 int rc;
1172 struct cifs_ses *ses = sess_data->ses;
1173 struct smb2_sess_setup_req *req;
1174 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001175 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001176
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001177 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1178 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001179 if (rc)
1180 return rc;
1181
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001182 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001183 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001184
1185 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1186 req->PreviousSessionId = sess_data->previous_session;
1187
1188 req->Flags = 0; /* MBZ */
Steve Frenchd4090142018-06-13 17:05:58 -05001189
1190 /* enough to enable echos and oplocks and one max size write */
1191 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001192
1193 /* only one of SMB2 signing flags may be set in SMB2 request */
1194 if (server->sign)
1195 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1196 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1197 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1198 else
1199 req->SecurityMode = 0;
1200
Steve French8d330962019-07-25 18:13:10 -05001201#ifdef CONFIG_CIFS_DFS_UPCALL
1202 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1203#else
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001204 req->Capabilities = 0;
Steve French8d330962019-07-25 18:13:10 -05001205#endif /* DFS_UPCALL */
1206
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001207 req->Channel = 0; /* MBZ */
1208
1209 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001210 /* 1 for pad */
1211 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001212 /*
1213 * This variable will be used to clear the buffer
1214 * allocated above in case of any error in the calling function.
1215 */
1216 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1217
1218 return 0;
1219}
1220
1221static void
1222SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1223{
1224 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1225 sess_data->buf0_type = CIFS_NO_BUFFER;
1226}
1227
1228static int
1229SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1230{
1231 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001232 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001233 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001234 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001235
1236 /* Testing shows that buffer offset must be at location of Buffer[0] */
1237 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001238 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001239 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1240
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001241 memset(&rqst, 0, sizeof(struct smb_rqst));
1242 rqst.rq_iov = sess_data->iov;
1243 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001244
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001245 /* BB add code to build os and lm fields */
1246 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1247 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001248 &sess_data->buf0_type,
1249 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001250 cifs_small_buf_release(sess_data->iov[0].iov_base);
1251 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001252
1253 return rc;
1254}
1255
1256static int
1257SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1258{
1259 int rc = 0;
1260 struct cifs_ses *ses = sess_data->ses;
1261
1262 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001263 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001264 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001265 if (rc) {
1266 cifs_dbg(FYI,
1267 "SMB3 session key generation failed\n");
1268 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001269 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001270 }
1271 }
1272 if (!ses->server->session_estab) {
1273 ses->server->sequence_number = 0x2;
1274 ses->server->session_estab = true;
1275 }
1276 mutex_unlock(&ses->server->srv_mutex);
1277
1278 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1279 spin_lock(&GlobalMid_Lock);
1280 ses->status = CifsGood;
1281 ses->need_reconnect = false;
1282 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001283 return rc;
1284}
1285
1286#ifdef CONFIG_CIFS_UPCALL
1287static void
1288SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1289{
1290 int rc;
1291 struct cifs_ses *ses = sess_data->ses;
1292 struct cifs_spnego_msg *msg;
1293 struct key *spnego_key = NULL;
1294 struct smb2_sess_setup_rsp *rsp = NULL;
1295
1296 rc = SMB2_sess_alloc_buffer(sess_data);
1297 if (rc)
1298 goto out;
1299
1300 spnego_key = cifs_get_spnego_key(ses);
1301 if (IS_ERR(spnego_key)) {
1302 rc = PTR_ERR(spnego_key);
1303 spnego_key = NULL;
1304 goto out;
1305 }
1306
1307 msg = spnego_key->payload.data[0];
1308 /*
1309 * check version field to make sure that cifs.upcall is
1310 * sending us a response in an expected form
1311 */
1312 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1313 cifs_dbg(VFS,
1314 "bad cifs.upcall version. Expected %d got %d",
1315 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1316 rc = -EKEYREJECTED;
1317 goto out_put_spnego_key;
1318 }
1319
1320 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1321 GFP_KERNEL);
1322 if (!ses->auth_key.response) {
1323 cifs_dbg(VFS,
1324 "Kerberos can't allocate (%u bytes) memory",
1325 msg->sesskey_len);
1326 rc = -ENOMEM;
1327 goto out_put_spnego_key;
1328 }
1329 ses->auth_key.len = msg->sesskey_len;
1330
1331 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1332 sess_data->iov[1].iov_len = msg->secblob_len;
1333
1334 rc = SMB2_sess_sendreceive(sess_data);
1335 if (rc)
1336 goto out_put_spnego_key;
1337
1338 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001339 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001340
1341 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001342
1343 rc = SMB2_sess_establish_session(sess_data);
1344out_put_spnego_key:
1345 key_invalidate(spnego_key);
1346 key_put(spnego_key);
1347out:
1348 sess_data->result = rc;
1349 sess_data->func = NULL;
1350 SMB2_sess_free_buffer(sess_data);
1351}
1352#else
1353static void
1354SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1355{
1356 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1357 sess_data->result = -EOPNOTSUPP;
1358 sess_data->func = NULL;
1359}
1360#endif
1361
Sachin Prabhu166cea42016-10-07 19:11:22 +01001362static void
1363SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1364
1365static void
1366SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1367{
1368 int rc;
1369 struct cifs_ses *ses = sess_data->ses;
1370 struct smb2_sess_setup_rsp *rsp = NULL;
1371 char *ntlmssp_blob = NULL;
1372 bool use_spnego = false; /* else use raw ntlmssp */
1373 u16 blob_length = 0;
1374
1375 /*
1376 * If memory allocation is successful, caller of this function
1377 * frees it.
1378 */
1379 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1380 if (!ses->ntlmssp) {
1381 rc = -ENOMEM;
1382 goto out_err;
1383 }
1384 ses->ntlmssp->sesskey_per_smbsess = true;
1385
1386 rc = SMB2_sess_alloc_buffer(sess_data);
1387 if (rc)
1388 goto out_err;
1389
1390 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1391 GFP_KERNEL);
1392 if (ntlmssp_blob == NULL) {
1393 rc = -ENOMEM;
1394 goto out;
1395 }
1396
1397 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1398 if (use_spnego) {
1399 /* BB eventually need to add this */
1400 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1401 rc = -EOPNOTSUPP;
1402 goto out;
1403 } else {
1404 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1405 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1406 }
1407 sess_data->iov[1].iov_base = ntlmssp_blob;
1408 sess_data->iov[1].iov_len = blob_length;
1409
1410 rc = SMB2_sess_sendreceive(sess_data);
1411 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1412
1413 /* If true, rc here is expected and not an error */
1414 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001415 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001416 rc = 0;
1417
1418 if (rc)
1419 goto out;
1420
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001421 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001422 le16_to_cpu(rsp->SecurityBufferOffset)) {
1423 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1424 le16_to_cpu(rsp->SecurityBufferOffset));
1425 rc = -EIO;
1426 goto out;
1427 }
1428 rc = decode_ntlmssp_challenge(rsp->Buffer,
1429 le16_to_cpu(rsp->SecurityBufferLength), ses);
1430 if (rc)
1431 goto out;
1432
1433 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1434
1435
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001436 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001437 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001438
1439out:
1440 kfree(ntlmssp_blob);
1441 SMB2_sess_free_buffer(sess_data);
1442 if (!rc) {
1443 sess_data->result = 0;
1444 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1445 return;
1446 }
1447out_err:
1448 kfree(ses->ntlmssp);
1449 ses->ntlmssp = NULL;
1450 sess_data->result = rc;
1451 sess_data->func = NULL;
1452}
1453
1454static void
1455SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1456{
1457 int rc;
1458 struct cifs_ses *ses = sess_data->ses;
1459 struct smb2_sess_setup_req *req;
1460 struct smb2_sess_setup_rsp *rsp = NULL;
1461 unsigned char *ntlmssp_blob = NULL;
1462 bool use_spnego = false; /* else use raw ntlmssp */
1463 u16 blob_length = 0;
1464
1465 rc = SMB2_sess_alloc_buffer(sess_data);
1466 if (rc)
1467 goto out;
1468
1469 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001470 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001471
1472 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1473 sess_data->nls_cp);
1474 if (rc) {
1475 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1476 goto out;
1477 }
1478
1479 if (use_spnego) {
1480 /* BB eventually need to add this */
1481 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1482 rc = -EOPNOTSUPP;
1483 goto out;
1484 }
1485 sess_data->iov[1].iov_base = ntlmssp_blob;
1486 sess_data->iov[1].iov_len = blob_length;
1487
1488 rc = SMB2_sess_sendreceive(sess_data);
1489 if (rc)
1490 goto out;
1491
1492 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1493
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001494 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001495 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001496
1497 rc = SMB2_sess_establish_session(sess_data);
1498out:
1499 kfree(ntlmssp_blob);
1500 SMB2_sess_free_buffer(sess_data);
1501 kfree(ses->ntlmssp);
1502 ses->ntlmssp = NULL;
1503 sess_data->result = rc;
1504 sess_data->func = NULL;
1505}
1506
1507static int
1508SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1509{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301510 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001511
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301512 type = smb2_select_sectype(ses->server, ses->sectype);
1513 cifs_dbg(FYI, "sess setup type %d\n", type);
1514 if (type == Unspecified) {
1515 cifs_dbg(VFS,
1516 "Unable to select appropriate authentication method!");
1517 return -EINVAL;
1518 }
1519
1520 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001521 case Kerberos:
1522 sess_data->func = SMB2_auth_kerberos;
1523 break;
1524 case RawNTLMSSP:
1525 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1526 break;
1527 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301528 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001529 return -EOPNOTSUPP;
1530 }
1531
1532 return 0;
1533}
1534
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001535int
1536SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1537 const struct nls_table *nls_cp)
1538{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001539 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001540 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001541 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001542
Joe Perchesf96637b2013-05-04 22:12:25 -05001543 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001544
Jeff Layton3534b852013-05-24 07:41:01 -04001545 if (!server) {
1546 WARN(1, "%s: server is NULL!\n", __func__);
1547 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001548 }
1549
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001550 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1551 if (!sess_data)
1552 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001553
1554 rc = SMB2_select_sec(ses, sess_data);
1555 if (rc)
1556 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001557 sess_data->xid = xid;
1558 sess_data->ses = ses;
1559 sess_data->buf0_type = CIFS_NO_BUFFER;
1560 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001561 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001562
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001563 /*
1564 * Initialize the session hash with the server one.
1565 */
1566 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1567 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001568
Sachin Prabhu166cea42016-10-07 19:11:22 +01001569 while (sess_data->func)
1570 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001571
Steve Frenchc721c382017-09-19 18:40:03 -05001572 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001573 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001574 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001575out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001576 kfree(sess_data);
1577 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001578}
1579
1580int
1581SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1582{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001583 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001584 struct smb2_logoff_req *req; /* response is also trivial struct */
1585 int rc = 0;
1586 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001587 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001588 unsigned int total_len;
1589 struct kvec iov[1];
1590 struct kvec rsp_iov;
1591 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001592
Joe Perchesf96637b2013-05-04 22:12:25 -05001593 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001594
1595 if (ses && (ses->server))
1596 server = ses->server;
1597 else
1598 return -EIO;
1599
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001600 /* no need to send SMB logoff if uid already closed due to reconnect */
1601 if (ses->need_reconnect)
1602 goto smb2_session_already_dead;
1603
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001604 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001605 if (rc)
1606 return rc;
1607
1608 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001609 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001610
1611 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1612 flags |= CIFS_TRANSFORM_REQ;
1613 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001614 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001615
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001616 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001617
1618 iov[0].iov_base = (char *)req;
1619 iov[0].iov_len = total_len;
1620
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001621 memset(&rqst, 0, sizeof(struct smb_rqst));
1622 rqst.rq_iov = iov;
1623 rqst.rq_nvec = 1;
1624
1625 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001626 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001627 /*
1628 * No tcon so can't do
1629 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1630 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001631
1632smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001633 return rc;
1634}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001635
1636static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1637{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001638 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001639}
1640
1641#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1642
Steve Frenchde9f68df2013-11-15 11:26:24 -06001643/* These are similar values to what Windows uses */
1644static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1645{
1646 tcon->max_chunks = 256;
1647 tcon->max_bytes_chunk = 1048576;
1648 tcon->max_bytes_copy = 16777216;
1649}
1650
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001651int
1652SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1653 struct cifs_tcon *tcon, const struct nls_table *cp)
1654{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001655 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001656 struct smb2_tree_connect_req *req;
1657 struct smb2_tree_connect_rsp *rsp = NULL;
1658 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001659 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001660 int rc = 0;
1661 int resp_buftype;
1662 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001663 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001664 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001665 unsigned int total_len;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001666 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001667
Joe Perchesf96637b2013-05-04 22:12:25 -05001668 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001669
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001670 if (!server || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001671 return -EIO;
1672
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001673 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1674 if (unc_path == NULL)
1675 return -ENOMEM;
1676
1677 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1678 unc_path_len *= 2;
1679 if (unc_path_len < 2) {
1680 kfree(unc_path);
1681 return -EINVAL;
1682 }
1683
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001684 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001685 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001686 atomic_set(&tcon->num_remote_opens, 0);
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001687 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1688 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001689 if (rc) {
1690 kfree(unc_path);
1691 return rc;
1692 }
1693
Steve French5a77e752018-05-09 17:43:08 -05001694 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001695 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001696
1697 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001698 /* 1 for pad */
1699 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001700
1701 /* Testing shows that buffer offset must be at location of Buffer[0] */
1702 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001703 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001704 req->PathLength = cpu_to_le16(unc_path_len - 2);
1705 iov[1].iov_base = unc_path;
1706 iov[1].iov_len = unc_path_len;
1707
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001708 /*
1709 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1710 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
Steve French8c11a602019-03-22 22:31:17 -05001711 * (Samba servers don't always set the flag so also check if null user)
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001712 */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001713 if ((server->dialect == SMB311_PROT_ID) &&
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001714 !smb3_encryption_required(tcon) &&
Steve French8c11a602019-03-22 22:31:17 -05001715 !(ses->session_flags &
1716 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1717 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
Steve French6188f282018-03-13 02:29:36 -05001718 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1719
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001720 memset(&rqst, 0, sizeof(struct smb_rqst));
1721 rqst.rq_iov = iov;
1722 rqst.rq_nvec = 2;
1723
Steve French4fe75c42019-02-14 01:19:02 -06001724 /* Need 64 for max size write so ask for more in case not there yet */
1725 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1726
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001727 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001728 cifs_small_buf_release(req);
1729 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001730 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001731 if (rc != 0) {
1732 if (tcon) {
1733 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1734 tcon->need_reconnect = true;
1735 }
1736 goto tcon_error_exit;
1737 }
1738
Christophe JAILLETcd123002017-05-12 17:59:32 +02001739 switch (rsp->ShareType) {
1740 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001741 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001742 break;
1743 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001744 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001745 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001746 break;
1747 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001748 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001749 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001750 break;
1751 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001752 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001753 rc = -EOPNOTSUPP;
1754 goto tcon_error_exit;
1755 }
1756
1757 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001758 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001759 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1760 tcon->tidStatus = CifsGood;
1761 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001762 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001763 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001764
1765 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1766 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001767 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001768
1769 if (tcon->seal &&
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001770 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001771 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001772
Steve Frenchde9f68df2013-11-15 11:26:24 -06001773 init_copy_chunk_defaults(tcon);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001774 if (server->ops->validate_negotiate)
1775 rc = server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001776tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001777
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001778 free_rsp_buf(resp_buftype, rsp);
1779 kfree(unc_path);
1780 return rc;
1781
1782tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001783 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001784 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001785 }
1786 goto tcon_exit;
1787}
1788
1789int
1790SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1791{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001792 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001793 struct smb2_tree_disconnect_req *req; /* response is trivial */
1794 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001795 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001796 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001797 unsigned int total_len;
1798 struct kvec iov[1];
1799 struct kvec rsp_iov;
1800 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001801
Joe Perchesf96637b2013-05-04 22:12:25 -05001802 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001803
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001804 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001805 return -EIO;
1806
1807 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1808 return 0;
1809
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001810 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1811 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001812 if (rc)
1813 return rc;
1814
Steve French5a77e752018-05-09 17:43:08 -05001815 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001816 flags |= CIFS_TRANSFORM_REQ;
1817
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001818 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001819
1820 iov[0].iov_base = (char *)req;
1821 iov[0].iov_len = total_len;
1822
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001823 memset(&rqst, 0, sizeof(struct smb_rqst));
1824 rqst.rq_iov = iov;
1825 rqst.rq_nvec = 1;
1826
1827 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001828 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001829 if (rc)
1830 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1831
1832 return rc;
1833}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001834
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001835
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001836static struct create_durable *
1837create_durable_buf(void)
1838{
1839 struct create_durable *buf;
1840
1841 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1842 if (!buf)
1843 return NULL;
1844
1845 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001846 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001847 buf->ccontext.DataLength = cpu_to_le32(16);
1848 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1849 (struct create_durable, Name));
1850 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001851 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001852 buf->Name[0] = 'D';
1853 buf->Name[1] = 'H';
1854 buf->Name[2] = 'n';
1855 buf->Name[3] = 'Q';
1856 return buf;
1857}
1858
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001859static struct create_durable *
1860create_reconnect_durable_buf(struct cifs_fid *fid)
1861{
1862 struct create_durable *buf;
1863
1864 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1865 if (!buf)
1866 return NULL;
1867
1868 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1869 (struct create_durable, Data));
1870 buf->ccontext.DataLength = cpu_to_le32(16);
1871 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1872 (struct create_durable, Name));
1873 buf->ccontext.NameLength = cpu_to_le16(4);
1874 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1875 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001876 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001877 buf->Name[0] = 'D';
1878 buf->Name[1] = 'H';
1879 buf->Name[2] = 'n';
1880 buf->Name[3] = 'C';
1881 return buf;
1882}
1883
Steve French89a5bfa2019-07-18 17:22:18 -05001884static void
1885parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1886{
1887 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1888
1889 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1890 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1891 buf->IndexNumber = pdisk_id->DiskFileId;
1892}
1893
1894void
1895smb2_parse_contexts(struct TCP_Server_Info *server,
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +10001896 struct smb2_create_rsp *rsp,
Steve French89a5bfa2019-07-18 17:22:18 -05001897 unsigned int *epoch, char *lease_key, __u8 *oplock,
1898 struct smb2_file_all_info *buf)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001899{
1900 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001901 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001902 unsigned int next;
1903 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001904 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001905
Steve French89a5bfa2019-07-18 17:22:18 -05001906 *oplock = 0;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001907 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001908 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001909 cc = (struct create_context *)data_offset;
Steve French89a5bfa2019-07-18 17:22:18 -05001910
1911 /* Initialize inode number to 0 in case no valid data in qfid context */
1912 if (buf)
1913 buf->IndexNumber = 0;
1914
Justin Maggarddeb7def2016-02-09 15:52:08 -08001915 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001916 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001917 if (le16_to_cpu(cc->NameLength) == 4 &&
Steve French89a5bfa2019-07-18 17:22:18 -05001918 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1919 *oplock = server->ops->parse_lease_buf(cc, epoch,
1920 lease_key);
1921 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1922 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1923 parse_query_id_ctxt(cc, buf);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001924
1925 next = le32_to_cpu(cc->Next);
1926 if (!next)
1927 break;
1928 remaining -= next;
1929 cc = (struct create_context *)((char *)cc + next);
1930 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001931
Steve French89a5bfa2019-07-18 17:22:18 -05001932 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1933 *oplock = rsp->OplockLevel;
1934
1935 return;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001936}
1937
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001938static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001939add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02001940 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001941{
1942 struct smb2_create_req *req = iov[0].iov_base;
1943 unsigned int num = *num_iovec;
1944
Stefano Brivio729c0c92018-07-05 15:10:02 +02001945 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001946 if (iov[num].iov_base == NULL)
1947 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001948 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001949 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1950 if (!req->CreateContextsOffset)
1951 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001952 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001953 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001954 le32_add_cpu(&req->CreateContextsLength,
1955 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001956 *num_iovec = num + 1;
1957 return 0;
1958}
1959
Steve Frenchb56eae42015-11-03 09:26:27 -06001960static struct create_durable_v2 *
Steve Frenchca567eb2019-03-29 16:31:07 -05001961create_durable_v2_buf(struct cifs_open_parms *oparms)
Steve Frenchb56eae42015-11-03 09:26:27 -06001962{
Steve Frenchca567eb2019-03-29 16:31:07 -05001963 struct cifs_fid *pfid = oparms->fid;
Steve Frenchb56eae42015-11-03 09:26:27 -06001964 struct create_durable_v2 *buf;
1965
1966 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1967 if (!buf)
1968 return NULL;
1969
1970 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1971 (struct create_durable_v2, dcontext));
1972 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1973 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1974 (struct create_durable_v2, Name));
1975 buf->ccontext.NameLength = cpu_to_le16(4);
1976
Steve Frenchca567eb2019-03-29 16:31:07 -05001977 /*
1978 * NB: Handle timeout defaults to 0, which allows server to choose
1979 * (most servers default to 120 seconds) and most clients default to 0.
1980 * This can be overridden at mount ("handletimeout=") if the user wants
1981 * a different persistent (or resilient) handle timeout for all opens
1982 * opens on a particular SMB3 mount.
1983 */
1984 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
Steve Frenchb56eae42015-11-03 09:26:27 -06001985 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001986 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001987 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1988
1989 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1990 buf->Name[0] = 'D';
1991 buf->Name[1] = 'H';
1992 buf->Name[2] = '2';
1993 buf->Name[3] = 'Q';
1994 return buf;
1995}
1996
1997static struct create_durable_handle_reconnect_v2 *
1998create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1999{
2000 struct create_durable_handle_reconnect_v2 *buf;
2001
2002 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2003 GFP_KERNEL);
2004 if (!buf)
2005 return NULL;
2006
2007 buf->ccontext.DataOffset =
2008 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2009 dcontext));
2010 buf->ccontext.DataLength =
2011 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2012 buf->ccontext.NameOffset =
2013 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2014 Name));
2015 buf->ccontext.NameLength = cpu_to_le16(4);
2016
2017 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2018 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2019 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2020 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2021
2022 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2023 buf->Name[0] = 'D';
2024 buf->Name[1] = 'H';
2025 buf->Name[2] = '2';
2026 buf->Name[3] = 'C';
2027 return buf;
2028}
2029
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002030static int
Steve Frenchb56eae42015-11-03 09:26:27 -06002031add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002032 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002033{
2034 struct smb2_create_req *req = iov[0].iov_base;
2035 unsigned int num = *num_iovec;
2036
Steve Frenchca567eb2019-03-29 16:31:07 -05002037 iov[num].iov_base = create_durable_v2_buf(oparms);
Steve Frenchb56eae42015-11-03 09:26:27 -06002038 if (iov[num].iov_base == NULL)
2039 return -ENOMEM;
2040 iov[num].iov_len = sizeof(struct create_durable_v2);
2041 if (!req->CreateContextsOffset)
2042 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002043 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002044 iov[1].iov_len);
2045 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002046 *num_iovec = num + 1;
2047 return 0;
2048}
2049
2050static int
2051add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2052 struct cifs_open_parms *oparms)
2053{
2054 struct smb2_create_req *req = iov[0].iov_base;
2055 unsigned int num = *num_iovec;
2056
2057 /* indicate that we don't need to relock the file */
2058 oparms->reconnect = false;
2059
2060 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2061 if (iov[num].iov_base == NULL)
2062 return -ENOMEM;
2063 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2064 if (!req->CreateContextsOffset)
2065 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002066 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002067 iov[1].iov_len);
2068 le32_add_cpu(&req->CreateContextsLength,
2069 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002070 *num_iovec = num + 1;
2071 return 0;
2072}
2073
2074static int
2075add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2076 struct cifs_open_parms *oparms, bool use_persistent)
2077{
2078 struct smb2_create_req *req = iov[0].iov_base;
2079 unsigned int num = *num_iovec;
2080
2081 if (use_persistent) {
2082 if (oparms->reconnect)
2083 return add_durable_reconnect_v2_context(iov, num_iovec,
2084 oparms);
2085 else
2086 return add_durable_v2_context(iov, num_iovec, oparms);
2087 }
2088
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002089 if (oparms->reconnect) {
2090 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2091 /* indicate that we don't need to relock the file */
2092 oparms->reconnect = false;
2093 } else
2094 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002095 if (iov[num].iov_base == NULL)
2096 return -ENOMEM;
2097 iov[num].iov_len = sizeof(struct create_durable);
2098 if (!req->CreateContextsOffset)
2099 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002100 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002101 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08002102 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002103 *num_iovec = num + 1;
2104 return 0;
2105}
2106
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002107/* See MS-SMB2 2.2.13.2.7 */
2108static struct crt_twarp_ctxt *
2109create_twarp_buf(__u64 timewarp)
2110{
2111 struct crt_twarp_ctxt *buf;
2112
2113 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2114 if (!buf)
2115 return NULL;
2116
2117 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2118 (struct crt_twarp_ctxt, Timestamp));
2119 buf->ccontext.DataLength = cpu_to_le32(8);
2120 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2121 (struct crt_twarp_ctxt, Name));
2122 buf->ccontext.NameLength = cpu_to_le16(4);
2123 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2124 buf->Name[0] = 'T';
2125 buf->Name[1] = 'W';
2126 buf->Name[2] = 'r';
2127 buf->Name[3] = 'p';
2128 buf->Timestamp = cpu_to_le64(timewarp);
2129 return buf;
2130}
2131
2132/* See MS-SMB2 2.2.13.2.7 */
2133static int
2134add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2135{
2136 struct smb2_create_req *req = iov[0].iov_base;
2137 unsigned int num = *num_iovec;
2138
2139 iov[num].iov_base = create_twarp_buf(timewarp);
2140 if (iov[num].iov_base == NULL)
2141 return -ENOMEM;
2142 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2143 if (!req->CreateContextsOffset)
2144 req->CreateContextsOffset = cpu_to_le32(
2145 sizeof(struct smb2_create_req) +
2146 iov[num - 1].iov_len);
2147 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2148 *num_iovec = num + 1;
2149 return 0;
2150}
2151
Steve Frenchff2a09e2019-07-06 14:41:38 -05002152static struct crt_query_id_ctxt *
2153create_query_id_buf(void)
2154{
2155 struct crt_query_id_ctxt *buf;
2156
2157 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2158 if (!buf)
2159 return NULL;
2160
2161 buf->ccontext.DataOffset = cpu_to_le16(0);
2162 buf->ccontext.DataLength = cpu_to_le32(0);
2163 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2164 (struct crt_query_id_ctxt, Name));
2165 buf->ccontext.NameLength = cpu_to_le16(4);
2166 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2167 buf->Name[0] = 'Q';
2168 buf->Name[1] = 'F';
2169 buf->Name[2] = 'i';
2170 buf->Name[3] = 'd';
2171 return buf;
2172}
2173
2174/* See MS-SMB2 2.2.13.2.9 */
2175static int
2176add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2177{
2178 struct smb2_create_req *req = iov[0].iov_base;
2179 unsigned int num = *num_iovec;
2180
2181 iov[num].iov_base = create_query_id_buf();
2182 if (iov[num].iov_base == NULL)
2183 return -ENOMEM;
2184 iov[num].iov_len = sizeof(struct crt_query_id_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_query_id_ctxt));
2190 *num_iovec = num + 1;
2191 return 0;
2192}
2193
Aurelien Aptelf0712922017-02-22 14:47:17 +01002194static int
2195alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2196 const char *treename, const __le16 *path)
2197{
2198 int treename_len, path_len;
2199 struct nls_table *cp;
2200 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2201
2202 /*
2203 * skip leading "\\"
2204 */
2205 treename_len = strlen(treename);
2206 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2207 return -EINVAL;
2208
2209 treename += 2;
2210 treename_len -= 2;
2211
2212 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2213
2214 /*
2215 * make room for one path separator between the treename and
2216 * path
2217 */
2218 *out_len = treename_len + 1 + path_len;
2219
2220 /*
2221 * final path needs to be null-terminated UTF16 with a
2222 * size aligned to 8
2223 */
2224
2225 *out_size = roundup((*out_len+1)*2, 8);
2226 *out_path = kzalloc(*out_size, GFP_KERNEL);
2227 if (!*out_path)
2228 return -ENOMEM;
2229
2230 cp = load_nls_default();
2231 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2232 UniStrcat(*out_path, sep);
2233 UniStrcat(*out_path, path);
2234 unload_nls(cp);
2235
2236 return 0;
2237}
2238
Steve Frenchbea851b2018-06-14 21:56:32 -05002239int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2240 umode_t mode, struct cifs_tcon *tcon,
2241 const char *full_path,
2242 struct cifs_sb_info *cifs_sb)
2243{
2244 struct smb_rqst rqst;
2245 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002246 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002247 struct cifs_ses *ses = tcon->ses;
2248 struct kvec iov[3]; /* make sure at least one for each open context */
2249 struct kvec rsp_iov = {NULL, 0};
2250 int resp_buftype;
2251 int uni_path_len;
2252 __le16 *copy_path = NULL;
2253 int copy_size;
2254 int rc = 0;
2255 unsigned int n_iov = 2;
2256 __u32 file_attributes = 0;
2257 char *pc_buf = NULL;
2258 int flags = 0;
2259 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002260 __le16 *utf16_path = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002261
2262 cifs_dbg(FYI, "mkdir\n");
2263
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002264 /* resource #1: path allocation */
2265 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2266 if (!utf16_path)
2267 return -ENOMEM;
2268
YueHaibing29cbfa12018-12-18 02:51:51 +00002269 if (!ses || !(ses->server)) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002270 rc = -EIO;
2271 goto err_free_path;
2272 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002273
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002274 /* resource #2: request */
Steve Frenchbea851b2018-06-14 21:56:32 -05002275 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002276 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002277 goto err_free_path;
2278
Steve Frenchbea851b2018-06-14 21:56:32 -05002279
2280 if (smb3_encryption_required(tcon))
2281 flags |= CIFS_TRANSFORM_REQ;
2282
Steve Frenchbea851b2018-06-14 21:56:32 -05002283 req->ImpersonationLevel = IL_IMPERSONATION;
2284 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2285 /* File attributes ignored on open (used in create though) */
2286 req->FileAttributes = cpu_to_le32(file_attributes);
2287 req->ShareAccess = FILE_SHARE_ALL_LE;
2288 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2289 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2290
2291 iov[0].iov_base = (char *)req;
2292 /* -1 since last byte is buf[0] which is sent below (path) */
2293 iov[0].iov_len = total_len - 1;
2294
2295 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2296
2297 /* [MS-SMB2] 2.2.13 NameOffset:
2298 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2299 * the SMB2 header, the file name includes a prefix that will
2300 * be processed during DFS name normalization as specified in
2301 * section 3.3.5.9. Otherwise, the file name is relative to
2302 * the share that is identified by the TreeId in the SMB2
2303 * header.
2304 */
2305 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2306 int name_len;
2307
2308 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2309 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2310 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002311 tcon->treeName, utf16_path);
2312 if (rc)
2313 goto err_free_req;
2314
Steve Frenchbea851b2018-06-14 21:56:32 -05002315 req->NameLength = cpu_to_le16(name_len * 2);
2316 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002317 /* free before overwriting resource */
2318 kfree(utf16_path);
2319 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002320 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002321 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002322 /* MUST set path len (NameLength) to 0 opening root of share */
2323 req->NameLength = cpu_to_le16(uni_path_len - 2);
2324 if (uni_path_len % 8 != 0) {
2325 copy_size = roundup(uni_path_len, 8);
2326 copy_path = kzalloc(copy_size, GFP_KERNEL);
2327 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002328 rc = -ENOMEM;
2329 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002330 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002331 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002332 uni_path_len);
2333 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002334 /* free before overwriting resource */
2335 kfree(utf16_path);
2336 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002337 }
2338 }
2339
2340 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002341 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002342 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2343
2344 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002345 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002346 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002347 if (rc)
2348 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002349 pc_buf = iov[n_iov-1].iov_base;
2350 }
2351
2352
2353 memset(&rqst, 0, sizeof(struct smb_rqst));
2354 rqst.rq_iov = iov;
2355 rqst.rq_nvec = n_iov;
2356
Steve Frenchd2f15422019-09-22 00:55:46 -05002357 /* no need to inc num_remote_opens because we close it just below */
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002358 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2359 FILE_WRITE_ATTRIBUTES);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002360 /* resource #4: response buffer */
2361 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2362 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002363 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2364 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002365 CREATE_NOT_FILE,
2366 FILE_WRITE_ATTRIBUTES, rc);
2367 goto err_free_rsp_buf;
2368 }
2369
2370 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2371 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2372 ses->Suid, CREATE_NOT_FILE,
2373 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002374
2375 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2376
2377 /* Eventually save off posix specific response info and timestaps */
2378
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002379err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002380 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002381 kfree(pc_buf);
2382err_free_req:
2383 cifs_small_buf_release(req);
2384err_free_path:
2385 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002386 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002387}
Steve Frenchbea851b2018-06-14 21:56:32 -05002388
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002389int
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002390SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2391 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002392{
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002393 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002394 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002395 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002396 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002397 int copy_size;
2398 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002399 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002400 struct kvec *iov = rqst->rq_iov;
2401 __le16 *copy_path;
2402 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002403
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002404 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002405 if (rc)
2406 return rc;
2407
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002408 iov[0].iov_base = (char *)req;
2409 /* -1 since last byte is buf[0] which is sent below (path) */
2410 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002411
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002412 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002413 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002414 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2415 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002416
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002417 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002418 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002419 /* File attributes ignored on open (used in create though) */
2420 req->FileAttributes = cpu_to_le32(file_attributes);
2421 req->ShareAccess = FILE_SHARE_ALL_LE;
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002422
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002423 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2424 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002425 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002426
2427 /* [MS-SMB2] 2.2.13 NameOffset:
2428 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2429 * the SMB2 header, the file name includes a prefix that will
2430 * be processed during DFS name normalization as specified in
2431 * section 3.3.5.9. Otherwise, the file name is relative to
2432 * the share that is identified by the TreeId in the SMB2
2433 * header.
2434 */
2435 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2436 int name_len;
2437
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002438 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002439 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2440 &name_len,
2441 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002442 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002443 return rc;
2444 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002445 uni_path_len = copy_size;
2446 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002447 } else {
2448 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2449 /* MUST set path len (NameLength) to 0 opening root of share */
2450 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002451 copy_size = uni_path_len;
2452 if (copy_size % 8 != 0)
2453 copy_size = roundup(copy_size, 8);
2454 copy_path = kzalloc(copy_size, GFP_KERNEL);
2455 if (!copy_path)
2456 return -ENOMEM;
2457 memcpy((char *)copy_path, (const char *)path,
2458 uni_path_len);
2459 uni_path_len = copy_size;
2460 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002461 }
2462
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002463 iov[1].iov_len = uni_path_len;
2464 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002465
Steve French3e7a02d2019-09-11 21:46:20 -05002466 if ((!server->oplocks) || (tcon->no_lease))
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002467 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2468
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002469 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002470 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2471 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002472 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2473 (oparms->create_options & CREATE_NOT_FILE))
2474 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002475 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002476 rc = add_lease_context(server, iov, &n_iov,
2477 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002478 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002479 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002480 }
2481
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002482 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2483 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002484 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002485 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002486 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002487 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002488 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002489 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002490
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002491 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002492 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002493 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002494 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002495 }
2496
Steve Frenchce558b02018-05-31 19:16:54 -05002497 if (tcon->posix_extensions) {
2498 if (n_iov > 2) {
2499 struct create_context *ccontext =
2500 (struct create_context *)iov[n_iov-1].iov_base;
2501 ccontext->Next =
2502 cpu_to_le32(iov[n_iov-1].iov_len);
2503 }
2504
2505 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002506 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002507 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002508 }
Steve Frenchce558b02018-05-31 19:16:54 -05002509
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002510 if (tcon->snapshot_time) {
2511 cifs_dbg(FYI, "adding snapshot context\n");
2512 if (n_iov > 2) {
2513 struct create_context *ccontext =
2514 (struct create_context *)iov[n_iov-1].iov_base;
2515 ccontext->Next =
2516 cpu_to_le32(iov[n_iov-1].iov_len);
2517 }
2518
2519 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2520 if (rc)
2521 return rc;
2522 }
2523
Steve Frenchd0959b02019-10-05 10:53:58 -05002524 if ((oparms->disposition == FILE_CREATE) &&
2525 (oparms->mode != ACL_NO_MODE)) {
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002526 if (n_iov > 2) {
2527 struct create_context *ccontext =
2528 (struct create_context *)iov[n_iov-1].iov_base;
2529 ccontext->Next =
2530 cpu_to_le32(iov[n_iov-1].iov_len);
2531 }
2532
2533 /* rc = add_sd_context(iov, &n_iov, oparms->mode); */
2534 if (rc)
2535 return rc;
2536 }
2537
Steve Frenchff2a09e2019-07-06 14:41:38 -05002538 if (n_iov > 2) {
2539 struct create_context *ccontext =
2540 (struct create_context *)iov[n_iov-1].iov_base;
2541 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2542 }
2543 add_query_id_context(iov, &n_iov);
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002544
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002545 rqst->rq_nvec = n_iov;
2546 return 0;
2547}
2548
2549/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2550 * All other vectors are freed by kfree().
2551 */
2552void
2553SMB2_open_free(struct smb_rqst *rqst)
2554{
2555 int i;
2556
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002557 if (rqst && rqst->rq_iov) {
2558 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2559 for (i = 1; i < rqst->rq_nvec; i++)
2560 if (rqst->rq_iov[i].iov_base != smb2_padding)
2561 kfree(rqst->rq_iov[i].iov_base);
2562 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002563}
2564
2565int
2566SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2567 __u8 *oplock, struct smb2_file_all_info *buf,
2568 struct kvec *err_iov, int *buftype)
2569{
2570 struct smb_rqst rqst;
2571 struct smb2_create_rsp *rsp = NULL;
2572 struct TCP_Server_Info *server;
2573 struct cifs_tcon *tcon = oparms->tcon;
2574 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002575 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002576 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002577 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002578 int rc = 0;
2579 int flags = 0;
2580
2581 cifs_dbg(FYI, "create/open\n");
2582 if (ses && (ses->server))
2583 server = ses->server;
2584 else
2585 return -EIO;
2586
2587 if (smb3_encryption_required(tcon))
2588 flags |= CIFS_TRANSFORM_REQ;
2589
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002590 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002591 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002592 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002593 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002594
2595 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2596 if (rc)
2597 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002598
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002599 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2600 oparms->create_options, oparms->desired_access);
2601
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002602 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002603 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002604 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002605
2606 if (rc != 0) {
2607 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002608 if (err_iov && rsp) {
2609 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002610 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002611 resp_buftype = CIFS_NO_BUFFER;
2612 rsp = NULL;
2613 }
Steve French28d59362018-05-30 21:42:34 -05002614 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2615 oparms->create_options, oparms->desired_access, rc);
Steve French7dcc82c2019-09-11 00:07:36 -05002616 if (rc == -EREMCHG) {
2617 printk_once(KERN_WARNING "server share %s deleted\n",
2618 tcon->treeName);
2619 tcon->need_reconnect = true;
2620 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002621 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002622 } else
2623 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2624 ses->Suid, oparms->create_options,
2625 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002626
Steve Frenchfae80442018-10-19 17:14:32 -05002627 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002628 oparms->fid->persistent_fid = rsp->PersistentFileId;
2629 oparms->fid->volatile_fid = rsp->VolatileFileId;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002630#ifdef CONFIG_CIFS_DEBUG2
2631 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2632#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002633
2634 if (buf) {
2635 memcpy(buf, &rsp->CreationTime, 32);
2636 buf->AllocationSize = rsp->AllocationSize;
2637 buf->EndOfFile = rsp->EndofFile;
2638 buf->Attributes = rsp->FileAttributes;
2639 buf->NumberOfLinks = cpu_to_le32(1);
2640 buf->DeletePending = 0;
2641 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002642
Steve French89a5bfa2019-07-18 17:22:18 -05002643
2644 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2645 oparms->fid->lease_key, oplock, buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002646creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002647 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002648 free_rsp_buf(resp_buftype, rsp);
2649 return rc;
2650}
2651
Steve French4a72daf2013-06-25 00:20:49 -05002652int
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002653SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2654 u64 persistent_fid, u64 volatile_fid, u32 opcode,
Steve French153322f2019-03-28 22:32:49 -05002655 bool is_fsctl, char *in_data, u32 indatalen,
2656 __u32 max_response_size)
Steve French4a72daf2013-06-25 00:20:49 -05002657{
2658 struct smb2_ioctl_req *req;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002659 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002660 unsigned int total_len;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002661 int rc;
Long Li2c87d6a2019-05-15 14:09:05 -07002662 char *in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002663
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002664 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002665 if (rc)
2666 return rc;
2667
Long Li2c87d6a2019-05-15 14:09:05 -07002668 if (indatalen) {
2669 /*
2670 * indatalen is usually small at a couple of bytes max, so
2671 * just allocate through generic pool
2672 */
YueHaibingd81f0972019-06-01 03:31:10 +00002673 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
Long Li2c87d6a2019-05-15 14:09:05 -07002674 if (!in_data_buf) {
2675 cifs_small_buf_release(req);
2676 return -ENOMEM;
2677 }
Long Li2c87d6a2019-05-15 14:09:05 -07002678 }
2679
Steve French4a72daf2013-06-25 00:20:49 -05002680 req->CtlCode = cpu_to_le32(opcode);
2681 req->PersistentFileId = persistent_fid;
2682 req->VolatileFileId = volatile_fid;
2683
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002684 iov[0].iov_base = (char *)req;
2685 /*
2686 * If no input data, the size of ioctl struct in
2687 * protocol spec still includes a 1 byte data buffer,
2688 * but if input data passed to ioctl, we do not
2689 * want to double count this, so we do not send
2690 * the dummy one byte of data in iovec[0] if sending
2691 * input data (in iovec[1]).
2692 */
Steve French4a72daf2013-06-25 00:20:49 -05002693 if (indatalen) {
2694 req->InputCount = cpu_to_le32(indatalen);
2695 /* do not set InputOffset if no input data */
2696 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002697 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002698 rqst->rq_nvec = 2;
2699 iov[0].iov_len = total_len - 1;
Long Li2c87d6a2019-05-15 14:09:05 -07002700 iov[1].iov_base = in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002701 iov[1].iov_len = indatalen;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002702 } else {
2703 rqst->rq_nvec = 1;
2704 iov[0].iov_len = total_len;
2705 }
Steve French4a72daf2013-06-25 00:20:49 -05002706
2707 req->OutputOffset = 0;
2708 req->OutputCount = 0; /* MBZ */
2709
2710 /*
Steve French153322f2019-03-28 22:32:49 -05002711 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2712 * We Could increase default MaxOutputResponse, but that could require
2713 * more credits. Windows typically sets this smaller, but for some
Steve French4a72daf2013-06-25 00:20:49 -05002714 * ioctls it may be useful to allow server to send more. No point
2715 * limiting what the server can send as long as fits in one credit
Steve French153322f2019-03-28 22:32:49 -05002716 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2717 * to increase this limit up in the future.
2718 * Note that for snapshot queries that servers like Azure expect that
2719 * the first query be minimal size (and just used to get the number/size
2720 * of previous versions) so response size must be specified as EXACTLY
2721 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2722 * of eight bytes. Currently that is the only case where we set max
2723 * response size smaller.
Steve French4a72daf2013-06-25 00:20:49 -05002724 */
Steve French153322f2019-03-28 22:32:49 -05002725 req->MaxOutputResponse = cpu_to_le32(max_response_size);
Steve French4a72daf2013-06-25 00:20:49 -05002726
2727 if (is_fsctl)
2728 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2729 else
2730 req->Flags = 0;
2731
Steve French4587eee2017-10-25 15:58:31 -05002732 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2733 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002734 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002735
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002736 return 0;
2737}
2738
2739void
2740SMB2_ioctl_free(struct smb_rqst *rqst)
2741{
Murphy Zhou6457c202019-05-23 12:12:43 +08002742 int i;
Long Li2c87d6a2019-05-15 14:09:05 -07002743 if (rqst && rqst->rq_iov) {
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002744 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Murphy Zhou6457c202019-05-23 12:12:43 +08002745 for (i = 1; i < rqst->rq_nvec; i++)
2746 if (rqst->rq_iov[i].iov_base != smb2_padding)
2747 kfree(rqst->rq_iov[i].iov_base);
Long Li2c87d6a2019-05-15 14:09:05 -07002748 }
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002749}
2750
Steve French153322f2019-03-28 22:32:49 -05002751
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002752/*
2753 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2754 */
2755int
2756SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2757 u64 volatile_fid, u32 opcode, bool is_fsctl,
Steve French153322f2019-03-28 22:32:49 -05002758 char *in_data, u32 indatalen, u32 max_out_data_len,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002759 char **out_data, u32 *plen /* returned data len */)
2760{
2761 struct smb_rqst rqst;
2762 struct smb2_ioctl_rsp *rsp = NULL;
2763 struct cifs_ses *ses;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002764 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002765 struct kvec rsp_iov = {NULL, 0};
2766 int resp_buftype = CIFS_NO_BUFFER;
2767 int rc = 0;
2768 int flags = 0;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002769 struct TCP_Server_Info *server;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002770
2771 cifs_dbg(FYI, "SMB2 IOCTL\n");
2772
2773 if (out_data != NULL)
2774 *out_data = NULL;
2775
2776 /* zero out returned data len, in case of error */
2777 if (plen)
2778 *plen = 0;
2779
2780 if (tcon)
2781 ses = tcon->ses;
2782 else
2783 return -EIO;
2784
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01002785 if (!ses)
2786 return -EIO;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002787 server = ses->server;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01002788 if (!server)
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002789 return -EIO;
2790
2791 if (smb3_encryption_required(tcon))
2792 flags |= CIFS_TRANSFORM_REQ;
2793
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002794 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002795 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002796 rqst.rq_iov = iov;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002797 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002798
Steve French153322f2019-03-28 22:32:49 -05002799 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2800 is_fsctl, in_data, indatalen, max_out_data_len);
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002801 if (rc)
2802 goto ioctl_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002803
2804 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002805 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002806 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002807
Steve Frencheccb4422018-05-17 21:16:55 -05002808 if (rc != 0)
2809 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2810 ses->Suid, 0, opcode, rc);
2811
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002812 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
Steve French8e353102015-03-26 19:47:02 -05002813 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002814 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002815 } else if (rc == -EINVAL) {
2816 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2817 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002818 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002819 goto ioctl_exit;
2820 }
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002821 } else if (rc == -E2BIG) {
2822 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2823 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2824 goto ioctl_exit;
2825 }
Steve French4a72daf2013-06-25 00:20:49 -05002826 }
2827
2828 /* check if caller wants to look at return data or just return rc */
2829 if ((plen == NULL) || (out_data == NULL))
2830 goto ioctl_exit;
2831
2832 *plen = le32_to_cpu(rsp->OutputCount);
2833
2834 /* We check for obvious errors in the output buffer length and offset */
2835 if (*plen == 0)
2836 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002837 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002838 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002839 *plen = 0;
2840 rc = -EIO;
2841 goto ioctl_exit;
2842 }
2843
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002844 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002845 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
Steve French4a72daf2013-06-25 00:20:49 -05002846 le32_to_cpu(rsp->OutputOffset));
2847 *plen = 0;
2848 rc = -EIO;
2849 goto ioctl_exit;
2850 }
2851
YueHaibingd034fee2018-09-10 01:33:06 +00002852 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2853 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05002854 if (*out_data == NULL) {
2855 rc = -ENOMEM;
2856 goto ioctl_exit;
2857 }
2858
Steve French4a72daf2013-06-25 00:20:49 -05002859ioctl_exit:
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002860 SMB2_ioctl_free(&rqst);
Steve French4a72daf2013-06-25 00:20:49 -05002861 free_rsp_buf(resp_buftype, rsp);
2862 return rc;
2863}
2864
Steve French64a5cfa2013-10-14 15:31:32 -05002865/*
2866 * Individual callers to ioctl worker function follow
2867 */
2868
2869int
2870SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2871 u64 persistent_fid, u64 volatile_fid)
2872{
2873 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002874 struct compress_ioctl fsctl_input;
2875 char *ret_data = NULL;
2876
2877 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002878 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002879
2880 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2881 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2882 (char *)&fsctl_input /* data input */,
Steve French153322f2019-03-28 22:32:49 -05002883 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2884 &ret_data /* out data */, NULL);
Steve French64a5cfa2013-10-14 15:31:32 -05002885
2886 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002887
2888 return rc;
2889}
2890
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002891int
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002892SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2893 u64 persistent_fid, u64 volatile_fid)
2894{
2895 struct smb2_close_req *req;
2896 struct kvec *iov = rqst->rq_iov;
2897 unsigned int total_len;
2898 int rc;
2899
2900 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2901 if (rc)
2902 return rc;
2903
2904 req->PersistentFileId = persistent_fid;
2905 req->VolatileFileId = volatile_fid;
2906 iov[0].iov_base = (char *)req;
2907 iov[0].iov_len = total_len;
2908
2909 return 0;
2910}
2911
2912void
2913SMB2_close_free(struct smb_rqst *rqst)
2914{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002915 if (rqst && rqst->rq_iov)
2916 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002917}
2918
2919int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002920SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2921 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002922{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002923 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002924 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002925 struct cifs_ses *ses = tcon->ses;
2926 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002927 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01002928 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002929 int rc = 0;
2930
Joe Perchesf96637b2013-05-04 22:12:25 -05002931 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002932
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002933 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002934 return -EIO;
2935
Steve French5a77e752018-05-09 17:43:08 -05002936 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002937 flags |= CIFS_TRANSFORM_REQ;
2938
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002939 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002940 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002941 rqst.rq_iov = iov;
2942 rqst.rq_nvec = 1;
2943
Steve Frenchf90f9792019-09-03 18:35:42 -05002944 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002945 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2946 if (rc)
2947 goto close_exit;
2948
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002949 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002950 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002951
2952 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002953 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002954 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2955 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002956 goto close_exit;
Steve Frenchf90f9792019-09-03 18:35:42 -05002957 } else
2958 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
2959 ses->Suid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002960
Steve Frenchfae80442018-10-19 17:14:32 -05002961 atomic_dec(&tcon->num_remote_opens);
2962
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002963 /* BB FIXME - decode close response, update inode for caching */
2964
2965close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002966 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002967 free_rsp_buf(resp_buftype, rsp);
2968 return rc;
2969}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002970
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002971int
2972SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2973 u64 persistent_fid, u64 volatile_fid)
2974{
2975 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2976}
2977
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002978int
2979smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2980 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002981{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002982 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002983 char *end_of_smb = smb_len + (char *)iov->iov_base;
2984 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002985 char *end_of_buf = begin_of_buf + buffer_length;
2986
2987
2988 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002989 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2990 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002991 return -EINVAL;
2992 }
2993
2994 /* check if beyond RFC1001 maximum length */
2995 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002996 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2997 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002998 return -EINVAL;
2999 }
3000
3001 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003002 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003003 return -EINVAL;
3004 }
3005
3006 return 0;
3007}
3008
3009/*
3010 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3011 * Caller must free buffer.
3012 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003013int
3014smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3015 struct kvec *iov, unsigned int minbufsize,
3016 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003017{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003018 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003019 int rc;
3020
3021 if (!data)
3022 return -EINVAL;
3023
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003024 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003025 if (rc)
3026 return rc;
3027
3028 memcpy(data, begin_of_buf, buffer_length);
3029
3030 return 0;
3031}
3032
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003033int
3034SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3035 u64 persistent_fid, u64 volatile_fid,
3036 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003037 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003038{
3039 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003040 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003041 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003042 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003043
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003044 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3045 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003046 if (rc)
3047 return rc;
3048
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003049 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003050 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003051 req->PersistentFileId = persistent_fid;
3052 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003053 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02003054
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003055 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003056 if (input_len) {
3057 req->InputBufferLength = cpu_to_le32(input_len);
3058 /* total_len for smb query request never close to le16 max */
3059 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3060 memcpy(req->Buffer, input, input_len);
3061 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003062
3063 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003064 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003065 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003066 return 0;
3067}
3068
3069void
3070SMB2_query_info_free(struct smb_rqst *rqst)
3071{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003072 if (rqst && rqst->rq_iov)
3073 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003074}
3075
3076static int
3077query_info(const unsigned int xid, struct cifs_tcon *tcon,
3078 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3079 u32 additional_info, size_t output_len, size_t min_len, void **data,
3080 u32 *dlen)
3081{
3082 struct smb_rqst rqst;
3083 struct smb2_query_info_rsp *rsp = NULL;
3084 struct kvec iov[1];
3085 struct kvec rsp_iov;
3086 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01003087 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003088 struct cifs_ses *ses = tcon->ses;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003089 struct TCP_Server_Info *server;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003090 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003091 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003092
3093 cifs_dbg(FYI, "Query Info\n");
3094
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003095 if (!ses)
3096 return -EIO;
3097 server = ses->server;
3098 if (!server)
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003099 return -EIO;
3100
3101 if (smb3_encryption_required(tcon))
3102 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003103
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003104 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003105 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003106 rqst.rq_iov = iov;
3107 rqst.rq_nvec = 1;
3108
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003109 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3110 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003111 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003112 if (rc)
3113 goto qinf_exit;
3114
Steve Frenchd42043a2019-02-26 21:58:30 -06003115 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3116 ses->Suid, info_class, (__u32)info_type);
3117
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003118 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003119 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003120
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003121 if (rc) {
3122 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003123 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3124 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003125 goto qinf_exit;
3126 }
3127
Steve Frenchd42043a2019-02-26 21:58:30 -06003128 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3129 ses->Suid, info_class, (__u32)info_type);
3130
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003131 if (dlen) {
3132 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3133 if (!*data) {
3134 *data = kmalloc(*dlen, GFP_KERNEL);
3135 if (!*data) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003136 cifs_tcon_dbg(VFS,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003137 "Error %d allocating memory for acl\n",
3138 rc);
3139 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003140 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003141 goto qinf_exit;
3142 }
Colin Ian King73aaf922019-01-16 16:28:59 +00003143 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003144 }
3145 }
3146
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003147 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3148 le32_to_cpu(rsp->OutputBufferLength),
3149 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00003150 if (rc && allocated) {
3151 kfree(*data);
3152 *data = NULL;
3153 *dlen = 0;
3154 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003155
3156qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003157 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003158 free_rsp_buf(resp_buftype, rsp);
3159 return rc;
3160}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003161
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003162int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3163 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003164{
3165 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003166 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04003167 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003168 sizeof(struct smb2_file_all_info), (void **)&data,
3169 NULL);
3170}
3171
3172int
3173SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3174 u64 persistent_fid, u64 volatile_fid,
3175 void **data, u32 *plen)
3176{
3177 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3178 *plen = 0;
3179
3180 return query_info(xid, tcon, persistent_fid, volatile_fid,
3181 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05003182 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003183}
3184
3185int
3186SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3187 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3188{
3189 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003190 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003191 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003192 sizeof(struct smb2_file_internal_info),
3193 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003194}
3195
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003196/*
Steve Frenchc3498182019-09-15 22:38:52 -05003197 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3198 * See MS-SMB2 2.2.35 and 2.2.36
3199 */
3200
zhengbin388962e2019-09-23 15:06:18 +08003201static int
Steve Frenchc3498182019-09-15 22:38:52 -05003202SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3203 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid,
3204 u32 completion_filter, bool watch_tree)
3205{
3206 struct smb2_change_notify_req *req;
3207 struct kvec *iov = rqst->rq_iov;
3208 unsigned int total_len;
3209 int rc;
3210
3211 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len);
3212 if (rc)
3213 return rc;
3214
3215 req->PersistentFileId = persistent_fid;
3216 req->VolatileFileId = volatile_fid;
Steve French52870d52019-10-01 21:25:46 -05003217 req->OutputBufferLength =
3218 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
Steve Frenchc3498182019-09-15 22:38:52 -05003219 req->CompletionFilter = cpu_to_le32(completion_filter);
3220 if (watch_tree)
3221 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3222 else
3223 req->Flags = 0;
3224
3225 iov[0].iov_base = (char *)req;
3226 iov[0].iov_len = total_len;
3227
3228 return 0;
3229}
3230
3231int
3232SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3233 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3234 u32 completion_filter)
3235{
3236 struct cifs_ses *ses = tcon->ses;
3237 struct smb_rqst rqst;
3238 struct kvec iov[1];
3239 struct kvec rsp_iov = {NULL, 0};
3240 int resp_buftype = CIFS_NO_BUFFER;
3241 int flags = 0;
3242 int rc = 0;
3243
3244 cifs_dbg(FYI, "change notify\n");
3245 if (!ses || !(ses->server))
3246 return -EIO;
3247
3248 if (smb3_encryption_required(tcon))
3249 flags |= CIFS_TRANSFORM_REQ;
3250
3251 memset(&rqst, 0, sizeof(struct smb_rqst));
3252 memset(&iov, 0, sizeof(iov));
3253 rqst.rq_iov = iov;
3254 rqst.rq_nvec = 1;
3255
3256 rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid,
3257 completion_filter, watch_tree);
3258 if (rc)
3259 goto cnotify_exit;
3260
3261 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3262 (u8)watch_tree, completion_filter);
3263 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3264
3265 if (rc != 0) {
3266 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3267 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3268 (u8)watch_tree, completion_filter, rc);
3269 } else
3270 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3271 ses->Suid, (u8)watch_tree, completion_filter);
3272
3273 cnotify_exit:
3274 if (rqst.rq_iov)
3275 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3276 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3277 return rc;
3278}
3279
3280
3281
3282/*
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003283 * This is a no-op for now. We're not really interested in the reply, but
3284 * rather in the fact that the server sent one and that server->lstrp
3285 * gets updated.
3286 *
3287 * FIXME: maybe we should consider checking that the reply matches request?
3288 */
3289static void
3290smb2_echo_callback(struct mid_q_entry *mid)
3291{
3292 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003293 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003294 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003295
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003296 if (mid->mid_state == MID_RESPONSE_RECEIVED
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003297 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3298 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3299 credits.instance = server->reconnect_instance;
3300 }
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003301
3302 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003303 add_credits(server, &credits, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003304}
3305
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003306void smb2_reconnect_server(struct work_struct *work)
3307{
3308 struct TCP_Server_Info *server = container_of(work,
3309 struct TCP_Server_Info, reconnect.work);
3310 struct cifs_ses *ses;
3311 struct cifs_tcon *tcon, *tcon2;
3312 struct list_head tmp_list;
3313 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01003314 int rc;
3315 int resched = false;
3316
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003317
3318 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3319 mutex_lock(&server->reconnect_mutex);
3320
3321 INIT_LIST_HEAD(&tmp_list);
3322 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3323
3324 spin_lock(&cifs_tcp_ses_lock);
3325 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3326 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003327 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003328 tcon->tc_count++;
3329 list_add_tail(&tcon->rlist, &tmp_list);
3330 tcon_exist = true;
3331 }
3332 }
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003333 /*
3334 * IPC has the same lifetime as its session and uses its
3335 * refcount.
3336 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003337 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3338 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3339 tcon_exist = true;
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003340 ses->ses_count++;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003341 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003342 }
3343 /*
3344 * Get the reference to server struct to be sure that the last call of
3345 * cifs_put_tcon() in the loop below won't release the server pointer.
3346 */
3347 if (tcon_exist)
3348 server->srv_count++;
3349
3350 spin_unlock(&cifs_tcp_ses_lock);
3351
3352 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01003353 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3354 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003355 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01003356 else
3357 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003358 list_del_init(&tcon->rlist);
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003359 if (tcon->ipc)
3360 cifs_put_smb_ses(tcon->ses);
3361 else
3362 cifs_put_tcon(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003363 }
3364
3365 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01003366 if (resched)
3367 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003368 mutex_unlock(&server->reconnect_mutex);
3369
3370 /* now we can safely release srv struct */
3371 if (tcon_exist)
3372 cifs_put_tcp_session(server, 1);
3373}
3374
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003375int
3376SMB2_echo(struct TCP_Server_Info *server)
3377{
3378 struct smb2_echo_req *req;
3379 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003380 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003381 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003382 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003383 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003384
Joe Perchesf96637b2013-05-04 22:12:25 -05003385 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003386
Steve French4fcd1812016-06-22 20:12:05 -05003387 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003388 /* No need to send echo on newly established connections */
3389 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3390 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003391 }
3392
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003393 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003394 if (rc)
3395 return rc;
3396
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003397 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003398
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003399 iov[0].iov_len = total_len;
3400 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003401
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003402 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003403 server, CIFS_ECHO_OP, NULL);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003404 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003405 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003406
3407 cifs_small_buf_release(req);
3408 return rc;
3409}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003410
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003411void
3412SMB2_flush_free(struct smb_rqst *rqst)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003413{
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003414 if (rqst && rqst->rq_iov)
3415 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3416}
3417
3418int
3419SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3420 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3421{
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003422 struct smb2_flush_req *req;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003423 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003424 unsigned int total_len;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003425 int rc;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003426
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003427 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003428 if (rc)
3429 return rc;
3430
3431 req->PersistentFileId = persistent_fid;
3432 req->VolatileFileId = volatile_fid;
3433
3434 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003435 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003436
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003437 return 0;
3438}
3439
3440int
3441SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3442 u64 volatile_fid)
3443{
3444 struct cifs_ses *ses = tcon->ses;
3445 struct smb_rqst rqst;
3446 struct kvec iov[1];
3447 struct kvec rsp_iov = {NULL, 0};
3448 int resp_buftype = CIFS_NO_BUFFER;
3449 int flags = 0;
3450 int rc = 0;
3451
3452 cifs_dbg(FYI, "flush\n");
3453 if (!ses || !(ses->server))
3454 return -EIO;
3455
3456 if (smb3_encryption_required(tcon))
3457 flags |= CIFS_TRANSFORM_REQ;
3458
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003459 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003460 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003461 rqst.rq_iov = iov;
3462 rqst.rq_nvec = 1;
3463
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003464 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3465 if (rc)
3466 goto flush_exit;
3467
Steve Frenchf90f9792019-09-03 18:35:42 -05003468 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003469 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003470
Steve Frencheccb4422018-05-17 21:16:55 -05003471 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003472 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003473 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3474 rc);
Steve Frenchf90f9792019-09-03 18:35:42 -05003475 } else
3476 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3477 ses->Suid);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003478
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003479 flush_exit:
3480 SMB2_flush_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003481 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003482 return rc;
3483}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003484
3485/*
3486 * To form a chain of read requests, any read requests after the first should
3487 * have the end_of_chain boolean set to true.
3488 */
3489static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003490smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003491 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3492 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003493{
3494 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003495 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003496 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07003497 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003498
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003499 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3500 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003501 if (rc)
3502 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003503
3504 server = io_parms->tcon->ses->server;
3505 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003506 return -ECONNABORTED;
3507
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003508 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003509 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003510
3511 req->PersistentFileId = io_parms->persistent_fid;
3512 req->VolatileFileId = io_parms->volatile_fid;
3513 req->ReadChannelInfoOffset = 0; /* reserved */
3514 req->ReadChannelInfoLength = 0; /* reserved */
3515 req->Channel = 0; /* reserved */
3516 req->MinimumCount = 0;
3517 req->Length = cpu_to_le32(io_parms->length);
3518 req->Offset = cpu_to_le64(io_parms->offset);
Steve Frenchd323c2462019-02-25 00:52:43 -06003519
3520 trace_smb3_read_enter(0 /* xid */,
3521 io_parms->persistent_fid,
3522 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3523 io_parms->offset, io_parms->length);
Long Libd3dcc62017-11-22 17:38:47 -07003524#ifdef CONFIG_CIFS_SMB_DIRECT
3525 /*
3526 * If we want to do a RDMA write, fill in and append
3527 * smbd_buffer_descriptor_v1 to the end of read request
3528 */
Long Libb4c0412018-04-17 12:17:08 -07003529 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003530 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003531
Long Libd3dcc62017-11-22 17:38:47 -07003532 struct smbd_buffer_descriptor_v1 *v1;
3533 bool need_invalidate =
3534 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3535
3536 rdata->mr = smbd_register_mr(
3537 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003538 rdata->nr_pages, rdata->page_offset,
3539 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003540 if (!rdata->mr)
Long Lib7972092019-04-05 21:36:34 +00003541 return -EAGAIN;
Long Libd3dcc62017-11-22 17:38:47 -07003542
3543 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3544 if (need_invalidate)
3545 req->Channel = SMB2_CHANNEL_RDMA_V1;
3546 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003547 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003548 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003549 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003550 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003551 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3552 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3553 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003554
3555 *total_len += sizeof(*v1) - 1;
3556 }
3557#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003558 if (request_type & CHAINED_REQUEST) {
3559 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003560 /* next 8-byte aligned request */
3561 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3562 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003563 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003564 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003565 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003566 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003567 /*
3568 * Related requests use info from previous read request
3569 * in chain.
3570 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003571 shdr->SessionId = 0xFFFFFFFF;
3572 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003573 req->PersistentFileId = 0xFFFFFFFF;
3574 req->VolatileFileId = 0xFFFFFFFF;
3575 }
3576 }
3577 if (remaining_bytes > io_parms->length)
3578 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3579 else
3580 req->RemainingBytes = 0;
3581
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003582 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003583 return rc;
3584}
3585
3586static void
3587smb2_readv_callback(struct mid_q_entry *mid)
3588{
3589 struct cifs_readdata *rdata = mid->callback_data;
3590 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3591 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003592 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003593 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003594 struct cifs_credits credits = { .value = 0, .instance = 0 };
Steve French46f17d12019-09-04 23:07:52 -05003595 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3596 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07003597 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003598 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003599 .rq_npages = rdata->nr_pages,
3600 .rq_pagesz = rdata->pagesz,
3601 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003602
Joe Perchesf96637b2013-05-04 22:12:25 -05003603 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3604 __func__, mid->mid, mid->mid_state, rdata->result,
3605 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003606
3607 switch (mid->mid_state) {
3608 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003609 credits.value = le16_to_cpu(shdr->CreditRequest);
3610 credits.instance = server->reconnect_instance;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003611 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003612 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003613 int rc;
3614
Jeff Layton0b688cf2012-09-18 16:20:34 -07003615 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003616 if (rc)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003617 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003618 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003619 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003620 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003621 task_io_account_read(rdata->got_bytes);
3622 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003623 break;
3624 case MID_REQUEST_SUBMITTED:
3625 case MID_RETRY_NEEDED:
3626 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003627 if (server->sign && rdata->got_bytes)
3628 /* reset bytes number since we can not check a sign */
3629 rdata->got_bytes = 0;
3630 /* FIXME: should this be counted toward the initiating task? */
3631 task_io_account_read(rdata->got_bytes);
3632 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003633 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003634 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003635 credits.value = le16_to_cpu(shdr->CreditRequest);
3636 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003637 /* fall through */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003638 default:
Pavel Shilovsky6b15eb12019-01-18 15:46:14 -08003639 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003640 }
Long Libd3dcc62017-11-22 17:38:47 -07003641#ifdef CONFIG_CIFS_SMB_DIRECT
3642 /*
3643 * If this rdata has a memmory registered, the MR can be freed
3644 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3645 * because they have limited number and are used for future I/Os
3646 */
3647 if (rdata->mr) {
3648 smbd_deregister_mr(rdata->mr);
3649 rdata->mr = NULL;
3650 }
3651#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08003652 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003653 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003654 trace_smb3_read_err(0 /* xid */,
3655 rdata->cfile->fid.persistent_fid,
3656 tcon->tid, tcon->ses->Suid, rdata->offset,
3657 rdata->bytes, rdata->result);
3658 } else
3659 trace_smb3_read_done(0 /* xid */,
3660 rdata->cfile->fid.persistent_fid,
3661 tcon->tid, tcon->ses->Suid,
3662 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003663
3664 queue_work(cifsiod_wq, &rdata->work);
3665 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003666 add_credits(server, &credits, 0);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003667}
3668
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003669/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003670int
3671smb2_async_readv(struct cifs_readdata *rdata)
3672{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003673 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003674 char *buf;
3675 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003676 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003677 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003678 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003679 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003680 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003681
Joe Perchesf96637b2013-05-04 22:12:25 -05003682 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3683 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003684
3685 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3686 io_parms.offset = rdata->offset;
3687 io_parms.length = rdata->bytes;
3688 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3689 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3690 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003691
3692 server = io_parms.tcon->ses->server;
3693
Long Li2dabfd52017-11-07 01:54:53 -07003694 rc = smb2_new_read_req(
3695 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003696 if (rc)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003697 return rc;
3698
Steve French5a77e752018-05-09 17:43:08 -05003699 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003700 flags |= CIFS_TRANSFORM_REQ;
3701
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003702 rdata->iov[0].iov_base = buf;
3703 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003704
3705 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003706
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003707 if (rdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003708 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003709 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003710 shdr->CreditRequest =
3711 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003712
3713 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3714 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003715 goto async_readv_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003716
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003717 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003718 }
3719
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003720 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07003721 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003722 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003723 smb3_handle_read_data, rdata, flags,
3724 &rdata->credits);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003725 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003726 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003727 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003728 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3729 io_parms.tcon->tid,
3730 io_parms.tcon->ses->Suid,
3731 io_parms.offset, io_parms.length, rc);
3732 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003733
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003734async_readv_out:
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003735 cifs_small_buf_release(buf);
3736 return rc;
3737}
Pavel Shilovsky33319142012-09-18 16:20:29 -07003738
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003739int
3740SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3741 unsigned int *nbytes, char **buf, int *buf_type)
3742{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003743 struct smb_rqst rqst;
Colin Ian King1efd4fc2019-07-31 10:05:26 +01003744 int resp_buftype, rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003745 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003746 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003747 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003748 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003749 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003750 int flags = CIFS_LOG_ERROR;
3751 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003752
3753 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07003754 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003755 if (rc)
3756 return rc;
3757
Steve French5a77e752018-05-09 17:43:08 -05003758 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003759 flags |= CIFS_TRANSFORM_REQ;
3760
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003761 iov[0].iov_base = (char *)req;
3762 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003763
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003764 memset(&rqst, 0, sizeof(struct smb_rqst));
3765 rqst.rq_iov = iov;
3766 rqst.rq_nvec = 1;
3767
3768 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003769 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003770
3771 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003772 if (rc != -ENODATA) {
3773 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3774 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003775 trace_smb3_read_err(xid, req->PersistentFileId,
3776 io_parms->tcon->tid, ses->Suid,
3777 io_parms->offset, io_parms->length,
3778 rc);
Steve Frenchb0a42f22019-02-25 15:02:58 -06003779 } else
3780 trace_smb3_read_done(xid, req->PersistentFileId,
3781 io_parms->tcon->tid, ses->Suid,
3782 io_parms->offset, 0);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003783 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Ronnie Sahlberg05fd5c22019-04-23 16:39:45 +10003784 cifs_small_buf_release(req);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003785 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05003786 } else
3787 trace_smb3_read_done(xid, req->PersistentFileId,
3788 io_parms->tcon->tid, ses->Suid,
3789 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003790
ZhangXiaoxu088aaf12019-04-06 15:47:39 +08003791 cifs_small_buf_release(req);
3792
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003793 *nbytes = le32_to_cpu(rsp->DataLength);
3794 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3795 (*nbytes > io_parms->length)) {
3796 cifs_dbg(FYI, "bad length %d for count %d\n",
3797 *nbytes, io_parms->length);
3798 rc = -EIO;
3799 *nbytes = 0;
3800 }
3801
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003802 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003803 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003804 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003805 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003806 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003807 if (resp_buftype == CIFS_SMALL_BUFFER)
3808 *buf_type = CIFS_SMALL_BUFFER;
3809 else if (resp_buftype == CIFS_LARGE_BUFFER)
3810 *buf_type = CIFS_LARGE_BUFFER;
3811 }
3812 return rc;
3813}
3814
Pavel Shilovsky33319142012-09-18 16:20:29 -07003815/*
3816 * Check the mid_state and signature on received buffer (if any), and queue the
3817 * workqueue completion task.
3818 */
3819static void
3820smb2_writev_callback(struct mid_q_entry *mid)
3821{
3822 struct cifs_writedata *wdata = mid->callback_data;
3823 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003824 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003825 unsigned int written;
3826 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003827 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky33319142012-09-18 16:20:29 -07003828
3829 switch (mid->mid_state) {
3830 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003831 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3832 credits.instance = server->reconnect_instance;
3833 wdata->result = smb2_check_receive(mid, server, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003834 if (wdata->result != 0)
3835 break;
3836
3837 written = le32_to_cpu(rsp->DataLength);
3838 /*
3839 * Mask off high 16 bits when bytes written as returned
3840 * by the server is greater than bytes requested by the
3841 * client. OS/2 servers are known to set incorrect
3842 * CountHigh values.
3843 */
3844 if (written > wdata->bytes)
3845 written &= 0xFFFF;
3846
3847 if (written < wdata->bytes)
3848 wdata->result = -ENOSPC;
3849 else
3850 wdata->bytes = written;
3851 break;
3852 case MID_REQUEST_SUBMITTED:
3853 case MID_RETRY_NEEDED:
3854 wdata->result = -EAGAIN;
3855 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003856 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003857 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3858 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003859 /* fall through */
Pavel Shilovsky33319142012-09-18 16:20:29 -07003860 default:
3861 wdata->result = -EIO;
3862 break;
3863 }
Long Lidb223a52017-11-22 17:38:45 -07003864#ifdef CONFIG_CIFS_SMB_DIRECT
3865 /*
3866 * If this wdata has a memory registered, the MR can be freed
3867 * The number of MRs available is limited, it's important to recover
3868 * used MR as soon as I/O is finished. Hold MR longer in the later
3869 * I/O process can possibly result in I/O deadlock due to lack of MR
3870 * to send request on I/O retry
3871 */
3872 if (wdata->mr) {
3873 smbd_deregister_mr(wdata->mr);
3874 wdata->mr = NULL;
3875 }
3876#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003877 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003878 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003879 trace_smb3_write_err(0 /* no xid */,
3880 wdata->cfile->fid.persistent_fid,
3881 tcon->tid, tcon->ses->Suid, wdata->offset,
3882 wdata->bytes, wdata->result);
3883 } else
3884 trace_smb3_write_done(0 /* no xid */,
3885 wdata->cfile->fid.persistent_fid,
3886 tcon->tid, tcon->ses->Suid,
3887 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003888
3889 queue_work(cifsiod_wq, &wdata->work);
3890 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003891 add_credits(server, &credits, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003892}
3893
3894/* smb2_async_writev - send an async write, and set up mid to handle result */
3895int
Steve French4a5c80d2014-02-07 20:45:12 -06003896smb2_async_writev(struct cifs_writedata *wdata,
3897 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07003898{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003899 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003900 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003901 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003902 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003903 struct TCP_Server_Info *server = tcon->ses->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003904 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003905 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003906 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003907
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003908 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003909 if (rc)
3910 return rc;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003911
Steve French5a77e752018-05-09 17:43:08 -05003912 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003913 flags |= CIFS_TRANSFORM_REQ;
3914
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003915 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003916 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003917
3918 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3919 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3920 req->WriteChannelInfoOffset = 0;
3921 req->WriteChannelInfoLength = 0;
3922 req->Channel = 0;
3923 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003924 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003925 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003926 req->RemainingBytes = 0;
Steve Frenchd323c2462019-02-25 00:52:43 -06003927
3928 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3929 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003930#ifdef CONFIG_CIFS_SMB_DIRECT
3931 /*
3932 * If we want to do a server RDMA read, fill in and append
3933 * smbd_buffer_descriptor_v1 to the end of write request
3934 */
Long Libb4c0412018-04-17 12:17:08 -07003935 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003936 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003937
Long Lidb223a52017-11-22 17:38:45 -07003938 struct smbd_buffer_descriptor_v1 *v1;
3939 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3940
3941 wdata->mr = smbd_register_mr(
3942 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003943 wdata->nr_pages, wdata->page_offset,
3944 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07003945 if (!wdata->mr) {
Long Lib7972092019-04-05 21:36:34 +00003946 rc = -EAGAIN;
Long Lidb223a52017-11-22 17:38:45 -07003947 goto async_writev_out;
3948 }
3949 req->Length = 0;
3950 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07003951 if (wdata->nr_pages > 1)
3952 req->RemainingBytes =
3953 cpu_to_le32(
3954 (wdata->nr_pages - 1) * wdata->pagesz -
3955 wdata->page_offset + wdata->tailsz
3956 );
3957 else
3958 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003959 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3960 if (need_invalidate)
3961 req->Channel = SMB2_CHANNEL_RDMA_V1;
3962 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003963 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003964 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003965 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003966 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003967 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3968 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3969 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003970 }
3971#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003972 iov[0].iov_len = total_len - 1;
3973 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003974
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003975 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003976 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003977 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07003978 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003979 rqst.rq_npages = wdata->nr_pages;
3980 rqst.rq_pagesz = wdata->pagesz;
3981 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003982#ifdef CONFIG_CIFS_SMB_DIRECT
3983 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003984 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07003985 rqst.rq_npages = 0;
3986 }
3987#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003988 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3989 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003990
Long Lidb223a52017-11-22 17:38:45 -07003991#ifdef CONFIG_CIFS_SMB_DIRECT
3992 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3993 if (!wdata->mr)
3994 req->Length = cpu_to_le32(wdata->bytes);
3995#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003996 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003997#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003998
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003999 if (wdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004000 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004001 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00004002 shdr->CreditRequest =
4003 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004004
4005 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4006 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004007 goto async_writev_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004008
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004009 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004010 }
4011
Pavel Shilovsky33319142012-09-18 16:20:29 -07004012 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08004013 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08004014 wdata, flags, &wdata->credits);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004015
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004016 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004017 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4018 tcon->tid, tcon->ses->Suid, wdata->offset,
4019 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06004020 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004021 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004022 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07004023
Pavel Shilovsky33319142012-09-18 16:20:29 -07004024async_writev_out:
4025 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004026 return rc;
4027}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004028
4029/*
4030 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4031 * The length field from io_parms must be at least 1 and indicates a number of
4032 * elements with data to write that begins with position 1 in iov array. All
4033 * data length is specified by count.
4034 */
4035int
4036SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4037 unsigned int *nbytes, struct kvec *iov, int n_vec)
4038{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004039 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004040 int rc = 0;
4041 struct smb2_write_req *req = NULL;
4042 struct smb2_write_rsp *rsp = NULL;
4043 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004044 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004045 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004046 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004047
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004048 *nbytes = 0;
4049
4050 if (n_vec < 1)
4051 return rc;
4052
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004053 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
4054 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004055 if (rc)
4056 return rc;
4057
4058 if (io_parms->tcon->ses->server == NULL)
4059 return -ECONNABORTED;
4060
Steve French5a77e752018-05-09 17:43:08 -05004061 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004062 flags |= CIFS_TRANSFORM_REQ;
4063
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004064 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004065
4066 req->PersistentFileId = io_parms->persistent_fid;
4067 req->VolatileFileId = io_parms->volatile_fid;
4068 req->WriteChannelInfoOffset = 0;
4069 req->WriteChannelInfoLength = 0;
4070 req->Channel = 0;
4071 req->Length = cpu_to_le32(io_parms->length);
4072 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004073 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004074 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004075 req->RemainingBytes = 0;
4076
Steve Frenchd323c2462019-02-25 00:52:43 -06004077 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4078 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4079 io_parms->offset, io_parms->length);
4080
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004081 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004082 /* 1 for Buffer */
4083 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004084
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004085 memset(&rqst, 0, sizeof(struct smb_rqst));
4086 rqst.rq_iov = iov;
4087 rqst.rq_nvec = n_vec + 1;
4088
4089 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004090 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004091 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004092
4093 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004094 trace_smb3_write_err(xid, req->PersistentFileId,
4095 io_parms->tcon->tid,
4096 io_parms->tcon->ses->Suid,
4097 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004098 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004099 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05004100 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004101 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05004102 trace_smb3_write_done(xid, req->PersistentFileId,
4103 io_parms->tcon->tid,
4104 io_parms->tcon->ses->Suid,
4105 io_parms->offset, *nbytes);
4106 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004107
ZhangXiaoxu6a3eb332019-04-06 15:47:38 +08004108 cifs_small_buf_release(req);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004109 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004110 return rc;
4111}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004112
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004113static unsigned int
4114num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
4115{
4116 int len;
4117 unsigned int entrycount = 0;
4118 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03004119 char *entryptr;
4120 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004121
4122 if (bufstart == NULL)
4123 return 0;
4124
Dan Carpenter56446f22018-09-06 12:48:22 +03004125 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004126
4127 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03004128 if (entryptr + next_offset < entryptr ||
4129 entryptr + next_offset > end_of_buf ||
4130 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004131 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004132 break;
4133 }
4134
Dan Carpenter56446f22018-09-06 12:48:22 +03004135 entryptr = entryptr + next_offset;
4136 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4137
4138 len = le32_to_cpu(dir_info->FileNameLength);
4139 if (entryptr + len < entryptr ||
4140 entryptr + len > end_of_buf ||
4141 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004142 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4143 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004144 break;
4145 }
4146
Dan Carpenter56446f22018-09-06 12:48:22 +03004147 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004148 entrycount++;
4149
Dan Carpenter56446f22018-09-06 12:48:22 +03004150 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004151 if (!next_offset)
4152 break;
4153 }
4154
4155 return entrycount;
4156}
4157
4158/*
4159 * Readdir/FindFirst
4160 */
4161int
4162SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4163 u64 persistent_fid, u64 volatile_fid, int index,
4164 struct cifs_search_info *srch_inf)
4165{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004166 struct smb_rqst rqst;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004167 struct smb2_query_directory_req *req;
4168 struct smb2_query_directory_rsp *rsp = NULL;
4169 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004170 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004171 int rc = 0;
4172 int len;
Steve French75fdfc82015-03-25 18:51:57 -05004173 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004174 unsigned char *bufptr;
4175 struct TCP_Server_Info *server;
4176 struct cifs_ses *ses = tcon->ses;
4177 __le16 asteriks = cpu_to_le16('*');
4178 char *end_of_smb;
4179 unsigned int output_size = CIFSMaxBufSize;
4180 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004181 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004182 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004183
4184 if (ses && (ses->server))
4185 server = ses->server;
4186 else
4187 return -EIO;
4188
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004189 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4190 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004191 if (rc)
4192 return rc;
4193
Steve French5a77e752018-05-09 17:43:08 -05004194 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004195 flags |= CIFS_TRANSFORM_REQ;
4196
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004197 switch (srch_inf->info_level) {
4198 case SMB_FIND_FILE_DIRECTORY_INFO:
4199 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4200 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4201 break;
4202 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4203 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4204 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4205 break;
4206 default:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004207 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05004208 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004209 rc = -EINVAL;
4210 goto qdir_exit;
4211 }
4212
4213 req->FileIndex = cpu_to_le32(index);
4214 req->PersistentFileId = persistent_fid;
4215 req->VolatileFileId = volatile_fid;
4216
4217 len = 0x2;
4218 bufptr = req->Buffer;
4219 memcpy(bufptr, &asteriks, len);
4220
4221 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004222 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004223 req->FileNameLength = cpu_to_le16(len);
4224 /*
4225 * BB could be 30 bytes or so longer if we used SMB2 specific
4226 * buffer lengths, but this is safe and close enough.
4227 */
4228 output_size = min_t(unsigned int, output_size, server->maxBuf);
4229 output_size = min_t(unsigned int, output_size, 2 << 15);
4230 req->OutputBufferLength = cpu_to_le32(output_size);
4231
4232 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004233 /* 1 for Buffer */
4234 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004235
4236 iov[1].iov_base = (char *)(req->Buffer);
4237 iov[1].iov_len = len;
4238
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004239 memset(&rqst, 0, sizeof(struct smb_rqst));
4240 rqst.rq_iov = iov;
4241 rqst.rq_nvec = 2;
4242
Steve Frenchd323c2462019-02-25 00:52:43 -06004243 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4244 tcon->ses->Suid, index, output_size);
4245
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004246 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004247 cifs_small_buf_release(req);
4248 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004249
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004250 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004251 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004252 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004253 trace_smb3_query_dir_done(xid, persistent_fid,
4254 tcon->tid, tcon->ses->Suid, index, 0);
Pavel Shilovsky52755802014-08-18 20:49:57 +04004255 srch_inf->endOfSearch = true;
4256 rc = 0;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004257 } else {
4258 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4259 tcon->ses->Suid, index, 0, rc);
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08004260 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004261 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004262 goto qdir_exit;
4263 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004264
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004265 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4266 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4267 info_buf_size);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004268 if (rc) {
4269 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4270 tcon->ses->Suid, index, 0, rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004271 goto qdir_exit;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004272 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004273
4274 srch_inf->unicode = true;
4275
4276 if (srch_inf->ntwrk_buf_start) {
4277 if (srch_inf->smallBuf)
4278 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4279 else
4280 cifs_buf_release(srch_inf->ntwrk_buf_start);
4281 }
4282 srch_inf->ntwrk_buf_start = (char *)rsp;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004283 srch_inf->srch_entries_start = srch_inf->last_entry =
4284 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4285 end_of_smb = rsp_iov.iov_len + (char *)rsp;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004286 srch_inf->entries_in_buffer =
4287 num_entries(srch_inf->srch_entries_start, end_of_smb,
4288 &srch_inf->last_entry, info_buf_size);
4289 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05004290 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4291 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4292 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004293 if (resp_buftype == CIFS_LARGE_BUFFER)
4294 srch_inf->smallBuf = false;
4295 else if (resp_buftype == CIFS_SMALL_BUFFER)
4296 srch_inf->smallBuf = true;
4297 else
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004298 cifs_tcon_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004299
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004300 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4301 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004302 return rc;
4303
4304qdir_exit:
4305 free_rsp_buf(resp_buftype, rsp);
4306 return rc;
4307}
4308
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004309int
4310SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004311 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004312 u8 info_type, u32 additional_info,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004313 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004314{
4315 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004316 struct kvec *iov = rqst->rq_iov;
4317 unsigned int i, total_len;
4318 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004319
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004320 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004321 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004322 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004323
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004324 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004325 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004326 req->FileInfoClass = info_class;
4327 req->PersistentFileId = persistent_fid;
4328 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004329 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004330
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004331 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004332 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004333 req->BufferLength = cpu_to_le32(*size);
4334
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004335 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004336 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004337
4338 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004339 /* 1 for Buffer */
4340 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004341
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004342 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004343 le32_add_cpu(&req->BufferLength, size[i]);
4344 iov[i].iov_base = (char *)data[i];
4345 iov[i].iov_len = size[i];
4346 }
4347
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004348 return 0;
4349}
4350
4351void
4352SMB2_set_info_free(struct smb_rqst *rqst)
4353{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10004354 if (rqst && rqst->rq_iov)
4355 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004356}
4357
4358static int
4359send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4360 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4361 u8 info_type, u32 additional_info, unsigned int num,
4362 void **data, unsigned int *size)
4363{
4364 struct smb_rqst rqst;
4365 struct smb2_set_info_rsp *rsp = NULL;
4366 struct kvec *iov;
4367 struct kvec rsp_iov;
4368 int rc = 0;
4369 int resp_buftype;
4370 struct cifs_ses *ses = tcon->ses;
4371 int flags = 0;
4372
4373 if (!ses || !(ses->server))
4374 return -EIO;
4375
4376 if (!num)
4377 return -EINVAL;
4378
4379 if (smb3_encryption_required(tcon))
4380 flags |= CIFS_TRANSFORM_REQ;
4381
4382 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4383 if (!iov)
4384 return -ENOMEM;
4385
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004386 memset(&rqst, 0, sizeof(struct smb_rqst));
4387 rqst.rq_iov = iov;
4388 rqst.rq_nvec = num;
4389
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004390 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4391 info_class, info_type, additional_info,
4392 data, size);
4393 if (rc) {
4394 kfree(iov);
4395 return rc;
4396 }
4397
4398
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004399 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004400 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004401 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004402 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004403
Steve Frencheccb4422018-05-17 21:16:55 -05004404 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004405 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004406 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4407 ses->Suid, info_class, (__u32)info_type, rc);
4408 }
Steve French7d3fb242013-11-18 09:56:28 -06004409
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004410 free_rsp_buf(resp_buftype, rsp);
4411 kfree(iov);
4412 return rc;
4413}
4414
4415int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004416SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004417 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004418{
4419 struct smb2_file_eof_info info;
4420 void *data;
4421 unsigned int size;
4422
4423 info.EndOfFile = *eof;
4424
4425 data = &info;
4426 size = sizeof(struct smb2_file_eof_info);
4427
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004428 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004429 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4430 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004431}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004432
4433int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004434SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4435 u64 persistent_fid, u64 volatile_fid,
4436 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4437{
4438 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4439 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4440 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004441}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004442
4443int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004444SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4445 u64 persistent_fid, u64 volatile_fid,
4446 struct smb2_file_full_ea_info *buf, int len)
4447{
4448 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4449 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4450 0, 1, (void **)&buf, &len);
4451}
4452
4453int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004454SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4455 const u64 persistent_fid, const u64 volatile_fid,
4456 __u8 oplock_level)
4457{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004458 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004459 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10004460 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004461 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004462 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004463 unsigned int total_len;
4464 struct kvec iov[1];
4465 struct kvec rsp_iov;
4466 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004467
Joe Perchesf96637b2013-05-04 22:12:25 -05004468 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004469 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4470 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004471 if (rc)
4472 return rc;
4473
Steve French5a77e752018-05-09 17:43:08 -05004474 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004475 flags |= CIFS_TRANSFORM_REQ;
4476
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004477 req->VolatileFid = volatile_fid;
4478 req->PersistentFid = persistent_fid;
4479 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004480 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004481
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004482 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004483
4484 iov[0].iov_base = (char *)req;
4485 iov[0].iov_len = total_len;
4486
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004487 memset(&rqst, 0, sizeof(struct smb_rqst));
4488 rqst.rq_iov = iov;
4489 rqst.rq_nvec = 1;
4490
4491 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004492 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004493
4494 if (rc) {
4495 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004496 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004497 }
4498
4499 return rc;
4500}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004501
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004502void
4503smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4504 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004505{
4506 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4507 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4508 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05304509 kst->f_bfree = kst->f_bavail =
4510 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004511 return;
4512}
4513
Steve French2d304212018-06-24 23:28:12 -05004514static void
4515copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4516 struct kstatfs *kst)
4517{
4518 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4519 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4520 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4521 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4522 kst->f_bavail = kst->f_bfree;
4523 else
4524 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4525 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4526 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4527 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4528 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4529
4530 return;
4531}
Steve French2d304212018-06-24 23:28:12 -05004532
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004533static int
4534build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4535 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4536{
4537 int rc;
4538 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004539 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004540
Joe Perchesf96637b2013-05-04 22:12:25 -05004541 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004542
4543 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4544 return -EIO;
4545
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004546 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4547 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004548 if (rc)
4549 return rc;
4550
4551 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4552 req->FileInfoClass = level;
4553 req->PersistentFileId = persistent_fid;
4554 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004555 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004556 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004557 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004558 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004559 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004560
4561 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004562 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004563 return 0;
4564}
4565
Steve French2d304212018-06-24 23:28:12 -05004566int
4567SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4568 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4569{
4570 struct smb_rqst rqst;
4571 struct smb2_query_info_rsp *rsp = NULL;
4572 struct kvec iov;
4573 struct kvec rsp_iov;
4574 int rc = 0;
4575 int resp_buftype;
4576 struct cifs_ses *ses = tcon->ses;
4577 FILE_SYSTEM_POSIX_INFO *info = NULL;
4578 int flags = 0;
4579
4580 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4581 sizeof(FILE_SYSTEM_POSIX_INFO),
4582 persistent_fid, volatile_fid);
4583 if (rc)
4584 return rc;
4585
4586 if (smb3_encryption_required(tcon))
4587 flags |= CIFS_TRANSFORM_REQ;
4588
4589 memset(&rqst, 0, sizeof(struct smb_rqst));
4590 rqst.rq_iov = &iov;
4591 rqst.rq_nvec = 1;
4592
4593 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4594 cifs_small_buf_release(iov.iov_base);
4595 if (rc) {
4596 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4597 goto posix_qfsinf_exit;
4598 }
4599 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4600
4601 info = (FILE_SYSTEM_POSIX_INFO *)(
4602 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004603 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4604 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4605 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05004606 if (!rc)
4607 copy_posix_fs_info_to_kstatfs(info, fsdata);
4608
4609posix_qfsinf_exit:
4610 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4611 return rc;
4612}
Steve French2d304212018-06-24 23:28:12 -05004613
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004614int
4615SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4616 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4617{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004618 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004619 struct smb2_query_info_rsp *rsp = NULL;
4620 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004621 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004622 int rc = 0;
4623 int resp_buftype;
4624 struct cifs_ses *ses = tcon->ses;
4625 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004626 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004627
4628 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4629 sizeof(struct smb2_fs_full_size_info),
4630 persistent_fid, volatile_fid);
4631 if (rc)
4632 return rc;
4633
Steve French5a77e752018-05-09 17:43:08 -05004634 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004635 flags |= CIFS_TRANSFORM_REQ;
4636
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004637 memset(&rqst, 0, sizeof(struct smb_rqst));
4638 rqst.rq_iov = &iov;
4639 rqst.rq_nvec = 1;
4640
4641 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004642 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004643 if (rc) {
4644 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05004645 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004646 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004647 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004648
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004649 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004650 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004651 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4652 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4653 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004654 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004655 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004656
Steve French34f62642013-10-09 02:07:00 -05004657qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004658 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004659 return rc;
4660}
4661
4662int
4663SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05004664 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05004665{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004666 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05004667 struct smb2_query_info_rsp *rsp = NULL;
4668 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004669 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05004670 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05004671 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05004672 struct cifs_ses *ses = tcon->ses;
4673 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004674 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05004675
Steven French21671142013-10-09 13:36:35 -05004676 if (level == FS_DEVICE_INFORMATION) {
4677 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4678 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4679 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4680 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4681 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004682 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4683 max_len = sizeof(struct smb3_fs_ss_info);
4684 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05004685 } else if (level == FS_VOLUME_INFORMATION) {
4686 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4687 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05004688 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004689 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05004690 return -EINVAL;
4691 }
4692
4693 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05004694 persistent_fid, volatile_fid);
4695 if (rc)
4696 return rc;
4697
Steve French5a77e752018-05-09 17:43:08 -05004698 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004699 flags |= CIFS_TRANSFORM_REQ;
4700
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004701 memset(&rqst, 0, sizeof(struct smb_rqst));
4702 rqst.rq_iov = &iov;
4703 rqst.rq_nvec = 1;
4704
4705 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004706 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004707 if (rc) {
4708 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4709 goto qfsattr_exit;
4710 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004711 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05004712
4713 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4714 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004715 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05004716 if (rc)
4717 goto qfsattr_exit;
4718
4719 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004720 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004721 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05004722 rsp_len, max_len));
4723 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004724 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004725 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004726 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4727 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004728 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004729 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4730 tcon->perf_sector_size =
4731 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05004732 } else if (level == FS_VOLUME_INFORMATION) {
4733 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4734 (offset + (char *)rsp);
4735 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4736 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004737 }
Steve French34f62642013-10-09 02:07:00 -05004738
4739qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004740 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004741 return rc;
4742}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004743
4744int
4745smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4746 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4747 const __u32 num_lock, struct smb2_lock_element *buf)
4748{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004749 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004750 int rc = 0;
4751 struct smb2_lock_req *req = NULL;
4752 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004753 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004754 int resp_buf_type;
4755 unsigned int count;
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004756 int flags = CIFS_NO_RSP_BUF;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004757 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004758
Joe Perchesf96637b2013-05-04 22:12:25 -05004759 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004760
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004761 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004762 if (rc)
4763 return rc;
4764
Steve French5a77e752018-05-09 17:43:08 -05004765 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004766 flags |= CIFS_TRANSFORM_REQ;
4767
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004768 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004769 req->LockCount = cpu_to_le16(num_lock);
4770
4771 req->PersistentFileId = persist_fid;
4772 req->VolatileFileId = volatile_fid;
4773
4774 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004775
4776 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004777 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004778 iov[1].iov_base = (char *)buf;
4779 iov[1].iov_len = count;
4780
4781 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004782
4783 memset(&rqst, 0, sizeof(struct smb_rqst));
4784 rqst.rq_iov = iov;
4785 rqst.rq_nvec = 2;
4786
4787 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004788 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004789 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004790 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004791 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004792 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004793 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4794 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004795 }
4796
4797 return rc;
4798}
4799
4800int
4801SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4802 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4803 const __u64 length, const __u64 offset, const __u32 lock_flags,
4804 const bool wait)
4805{
4806 struct smb2_lock_element lock;
4807
4808 lock.Offset = cpu_to_le64(offset);
4809 lock.Length = cpu_to_le64(length);
4810 lock.Flags = cpu_to_le32(lock_flags);
4811 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4812 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4813
4814 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4815}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004816
4817int
4818SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4819 __u8 *lease_key, const __le32 lease_state)
4820{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004821 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004822 int rc;
4823 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004824 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004825 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004826 unsigned int total_len;
4827 struct kvec iov[1];
4828 struct kvec rsp_iov;
4829 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05004830 __u64 *please_key_high;
4831 __u64 *please_key_low;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004832
Joe Perchesf96637b2013-05-04 22:12:25 -05004833 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004834 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4835 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004836 if (rc)
4837 return rc;
4838
Steve French5a77e752018-05-09 17:43:08 -05004839 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004840 flags |= CIFS_TRANSFORM_REQ;
4841
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004842 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004843 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004844 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004845
4846 memcpy(req->LeaseKey, lease_key, 16);
4847 req->LeaseState = lease_state;
4848
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004849 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004850
4851 iov[0].iov_base = (char *)req;
4852 iov[0].iov_len = total_len;
4853
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004854 memset(&rqst, 0, sizeof(struct smb_rqst));
4855 rqst.rq_iov = iov;
4856 rqst.rq_nvec = 1;
4857
4858 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004859 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004860
Aurelien Apteld339adc2019-01-31 13:46:07 +01004861 please_key_low = (__u64 *)lease_key;
4862 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004863 if (rc) {
4864 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05004865 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4866 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05004867 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05004868 } else
4869 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4870 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004871
4872 return rc;
4873}