blob: 0dc66f36ff5b44dc619e74feba2df351a689e31d [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020036#include <linux/uuid.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070037#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040038#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070048#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070049#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050050#include "cifs_spnego.h"
Long Lidb223a52017-11-22 17:38:45 -070051#include "smbdirect.h"
Steve Frencheccb4422018-05-17 21:16:55 -050052#include "trace.h"
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -020053#ifdef CONFIG_CIFS_DFS_UPCALL
54#include "dfs_cache.h"
55#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040056
57/*
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
60 *
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
63 */
64static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
67 /* SMB2_LOGOFF */ 4,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
70 /* SMB2_CREATE */ 57,
71 /* SMB2_CLOSE */ 24,
72 /* SMB2_FLUSH */ 24,
73 /* SMB2_READ */ 49,
74 /* SMB2_WRITE */ 49,
75 /* SMB2_LOCK */ 48,
76 /* SMB2_IOCTL */ 57,
77 /* SMB2_CANCEL */ 4,
78 /* SMB2_ECHO */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
84};
85
Ronnie Sahlberg730928c2018-08-08 15:07:49 +100086int smb3_encryption_required(const struct cifs_tcon *tcon)
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070087{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080088 if (!tcon)
89 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070090 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
91 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
92 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080093 if (tcon->seal &&
94 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
95 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070096 return 0;
97}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040098
99static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700100smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400101 const struct cifs_tcon *tcon)
102{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700103 shdr->ProtocolId = SMB2_PROTO_NUMBER;
104 shdr->StructureSize = cpu_to_le16(64);
105 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
108
109 spin_lock(&server->req_lock);
Steve French69dc4b12019-03-05 21:04:56 -0600110 /* Request up to 10 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500111 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700112 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100113 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700114 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500115 min_t(int, server->max_credits -
Steve French69dc4b12019-03-05 21:04:56 -0600116 server->credits, 10));
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100117 spin_unlock(&server->req_lock);
118 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700119 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100120 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400122
123 if (!tcon)
124 goto out;
125
Steve French2b80d042013-06-23 18:43:37 -0500126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500128 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700130 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500131 /* else CreditCharge MBZ */
132
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700133 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400134 /* Uid is not converted */
135 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700136 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500137
138 /*
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
147 */
148/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500150
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
Steve French5a77e752018-05-09 17:43:08 -0500152 !smb3_encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700153 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400154out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400155 return;
156}
157
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200158#ifdef CONFIG_CIFS_DFS_UPCALL
159static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
161{
162 int rc;
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100165 char *tree;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200166 const char *tcp_host;
167 size_t tcp_host_len;
168 const char *dfs_host;
169 size_t dfs_host_len;
170
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
172 if (!tree)
173 return -ENOMEM;
174
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200175 if (tcon->ipc) {
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000176 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
179 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200180 }
181
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
184 goto out;
185 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200186
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
188 if (rc)
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100189 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200190
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
192 &tcp_host_len);
193
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
196 const char *tgt = dfs_cache_get_tgt_name(it);
197
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
199
200 if (dfs_host_len != tcp_host_len
201 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
202 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
203 __func__,
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
206 continue;
207 }
208
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000209 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200210
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
212 if (!rc)
213 break;
214 if (rc == -EREMOTE)
215 break;
216 }
217
218 if (!rc) {
219 if (it)
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
221 it);
222 else
223 rc = -ENOENT;
224 }
225 dfs_cache_free_tgts(&tl);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100226out:
227 kfree(tree);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200228 return rc;
229}
230#else
231static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
233{
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
235}
236#endif
237
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400238static int
239smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
240{
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300241 int rc;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200245 int retries;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400246
247 /*
248 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
249 * check for tcp and smb session status done differently
250 * for those three - in the calling routine.
251 */
252 if (tcon == NULL)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300253 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400254
255 if (smb2_command == SMB2_TREE_CONNECT)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300256 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400257
258 if (tcon->tidStatus == CifsExiting) {
259 /*
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
263 */
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
268 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400269 return -ENODEV;
270 }
271 }
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
274 return -EIO;
275
276 ses = tcon->ses;
277 server = ses->server;
278
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200279 retries = server->nr_targets;
280
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400281 /*
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200282 * Give demultiplex thread up to 10 seconds to each target available for
283 * reconnect -- should be greater than cifs socket timeout which is 7
284 * seconds.
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400285 */
286 while (server->tcpStatus == CifsNeedReconnect) {
287 /*
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
290 */
291 switch (smb2_command) {
292 /*
293 * BB Should we keep oplock break and add flush to exceptions?
294 */
295 case SMB2_TREE_DISCONNECT:
296 case SMB2_CANCEL:
297 case SMB2_CLOSE:
298 case SMB2_OPLOCK_BREAK:
299 return -EAGAIN;
300 }
301
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
304 10 * HZ);
305 if (rc < 0) {
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
308 return -ERESTARTSYS;
309 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400310
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
313 break;
314
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200315 if (--retries)
316 continue;
317
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400318 /*
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
321 * back on-line
322 */
323 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400325 return -EHOSTDOWN;
326 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200327 retries = server->nr_targets;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400328 }
329
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300331 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400332
333 nls_codepage = load_nls_default();
334
335 /*
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
338 */
339 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200340
341 /*
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
345 */
346 if (server->tcpStatus == CifsNeedReconnect) {
347 rc = -EHOSTDOWN;
348 mutex_unlock(&tcon->ses->session_mutex);
349 goto out;
350 }
351
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400352 rc = cifs_negotiate_protocol(0, tcon->ses);
353 if (!rc && tcon->ses->need_reconnect)
354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
355
356 if (rc || !tcon->need_reconnect) {
357 mutex_unlock(&tcon->ses->session_mutex);
358 goto out;
359 }
360
361 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800362 if (tcon->use_persistent)
363 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500364
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200365 rc = __smb2_reconnect(nls_codepage, tcon);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400366 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500367
Joe Perchesf96637b2013-05-04 22:12:25 -0500368 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500369 if (rc) {
370 /* If sess reconnected but tcon didn't, something strange ... */
371 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400372 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500373 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800374
375 if (smb2_command != SMB2_INTERNAL_CMD)
376 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
377
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400378 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400379out:
380 /*
381 * Check if handle based operation so we know whether we can continue
382 * or not without returning to caller to reset file handle.
383 */
384 /*
385 * BB Is flush done by server on drop of tcp session? Should we special
386 * case it and skip above?
387 */
388 switch (smb2_command) {
389 case SMB2_FLUSH:
390 case SMB2_READ:
391 case SMB2_WRITE:
392 case SMB2_LOCK:
393 case SMB2_IOCTL:
394 case SMB2_QUERY_DIRECTORY:
395 case SMB2_CHANGE_NOTIFY:
396 case SMB2_QUERY_INFO:
397 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800398 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400399 }
400 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400401 return rc;
402}
403
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700404static void
405fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
406 unsigned int *total_len)
407{
408 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
409 /* lookup word count ie StructureSize from table */
410 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
411
412 /*
413 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
414 * largest operations (Create)
415 */
416 memset(buf, 0, 256);
417
418 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
419 spdu->StructureSize2 = cpu_to_le16(parmsize);
420
421 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
422}
423
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100424/*
425 * Allocate and return pointer to an SMB request hdr, and set basic
426 * SMB information in the SMB header. If the return code is zero, this
427 * function must have filled in request_buf pointer.
428 */
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800429static int
430smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
431 void **request_buf, unsigned int *total_len)
432{
433 int rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800434
435 rc = smb2_reconnect(smb2_command, tcon);
436 if (rc)
437 return rc;
438
439 /* BB eventually switch this to SMB2 specific small buf size */
Stefano Briviof46ecbd2018-07-05 11:46:42 +0200440 if (smb2_command == SMB2_SET_INFO)
441 *request_buf = cifs_buf_get();
442 else
443 *request_buf = cifs_small_buf_get();
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800444 if (*request_buf == NULL) {
445 /* BB should we add a retry in here if not a writepage? */
446 return -ENOMEM;
447 }
448
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100449 fill_small_buf(smb2_command, tcon,
450 (struct smb2_sync_hdr *)(*request_buf),
451 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400452
453 if (tcon != NULL) {
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400454 uint16_t com_code = le16_to_cpu(smb2_command);
455 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400456 cifs_stats_inc(&tcon->num_smbs_sent);
457 }
458
459 return rc;
460}
461
Steve French0fdfef92018-06-28 19:30:23 -0500462
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500463#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
464#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
Steve Frenchfcef0db2018-05-19 20:45:27 -0500465#define SMB2_POSIX_EXTENSIONS_AVAILABLE cpu_to_le16(0x100)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500466
467static void
468build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
469{
470 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
471 pneg_ctxt->DataLength = cpu_to_le16(38);
472 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
473 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
474 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
475 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
476}
477
478static void
479build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
480{
481 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French23657ad2018-04-22 15:14:58 -0500482 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
483 pneg_ctxt->CipherCount = cpu_to_le16(1);
484/* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
485 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500486}
487
488static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500489build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
490{
491 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
492 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
Steve French0d481322019-02-24 17:56:33 -0600493 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
494 pneg_ctxt->Name[0] = 0x93;
495 pneg_ctxt->Name[1] = 0xAD;
496 pneg_ctxt->Name[2] = 0x25;
497 pneg_ctxt->Name[3] = 0x50;
498 pneg_ctxt->Name[4] = 0x9C;
499 pneg_ctxt->Name[5] = 0xB4;
500 pneg_ctxt->Name[6] = 0x11;
501 pneg_ctxt->Name[7] = 0xE7;
502 pneg_ctxt->Name[8] = 0xB4;
503 pneg_ctxt->Name[9] = 0x23;
504 pneg_ctxt->Name[10] = 0x83;
505 pneg_ctxt->Name[11] = 0xDE;
506 pneg_ctxt->Name[12] = 0x96;
507 pneg_ctxt->Name[13] = 0x8B;
508 pneg_ctxt->Name[14] = 0xCD;
509 pneg_ctxt->Name[15] = 0x7C;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500510}
511
512static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100513assemble_neg_contexts(struct smb2_negotiate_req *req,
514 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500515{
Steve Frenchd5c70762019-01-03 02:37:21 -0600516 char *pneg_ctxt = (char *)req;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500517 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500518
Steve Frenchd5c70762019-01-03 02:37:21 -0600519 if (*total_len > 200) {
520 /* In case length corrupted don't want to overrun smb buffer */
521 cifs_dbg(VFS, "Bad frame length assembling neg contexts\n");
522 return;
523 }
524
525 /*
526 * round up total_len of fixed part of SMB3 negotiate request to 8
527 * byte boundary before adding negotiate contexts
528 */
529 *total_len = roundup(*total_len, 8);
530
531 pneg_ctxt = (*total_len) + (char *)req;
532 req->NegotiateContextOffset = cpu_to_le32(*total_len);
533
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500534 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500535 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
536 *total_len += ctxt_len;
537 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100538
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500539 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500540 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
541 *total_len += ctxt_len;
542 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100543
Steve Frenchfcef0db2018-05-19 20:45:27 -0500544 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
545 *total_len += sizeof(struct smb2_posix_neg_context);
546
Steve Frenchfcef0db2018-05-19 20:45:27 -0500547 req->NegotiateContextCount = cpu_to_le16(3);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500548}
Steve French5100d8a2018-04-09 10:47:14 -0500549
550static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
551{
552 unsigned int len = le16_to_cpu(ctxt->DataLength);
553
554 /* If invalid preauth context warn but use what we requested, SHA-512 */
555 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
556 printk_once(KERN_WARNING "server sent bad preauth context\n");
557 return;
558 }
559 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
560 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
561 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
562 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
563}
564
565static int decode_encrypt_ctx(struct TCP_Server_Info *server,
566 struct smb2_encryption_neg_context *ctxt)
567{
568 unsigned int len = le16_to_cpu(ctxt->DataLength);
569
570 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
571 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
572 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
573 return -EINVAL;
574 }
575
576 if (le16_to_cpu(ctxt->CipherCount) != 1) {
577 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
578 return -EINVAL;
579 }
580 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
581 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
582 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
583 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
584 return -EINVAL;
585 }
586 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500587 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500588 return 0;
589}
590
591static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000592 struct TCP_Server_Info *server,
593 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500594{
595 struct smb2_neg_context *pctx;
596 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
597 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500598 unsigned int len_of_ctxts, i;
599 int rc = 0;
600
601 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
602 if (len_of_smb <= offset) {
603 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
604 return -EINVAL;
605 }
606
607 len_of_ctxts = len_of_smb - offset;
608
609 for (i = 0; i < ctxt_cnt; i++) {
610 int clen;
611 /* check that offset is not beyond end of SMB */
612 if (len_of_ctxts == 0)
613 break;
614
615 if (len_of_ctxts < sizeof(struct smb2_neg_context))
616 break;
617
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000618 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500619 clen = le16_to_cpu(pctx->DataLength);
620 if (clen > len_of_ctxts)
621 break;
622
623 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
624 decode_preauth_context(
625 (struct smb2_preauth_neg_context *)pctx);
626 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
627 rc = decode_encrypt_ctx(server,
628 (struct smb2_encryption_neg_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500629 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
630 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500631 else
632 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
633 le16_to_cpu(pctx->ContextType));
634
635 if (rc)
636 break;
637 /* offsets must be 8 byte aligned */
638 clen = (clen + 7) & ~0x7;
639 offset += clen + sizeof(struct smb2_neg_context);
640 len_of_ctxts -= clen;
641 }
642 return rc;
643}
644
Steve Frenchce558b02018-05-31 19:16:54 -0500645static struct create_posix *
646create_posix_buf(umode_t mode)
647{
648 struct create_posix *buf;
649
650 buf = kzalloc(sizeof(struct create_posix),
651 GFP_KERNEL);
652 if (!buf)
653 return NULL;
654
655 buf->ccontext.DataOffset =
656 cpu_to_le16(offsetof(struct create_posix, Mode));
657 buf->ccontext.DataLength = cpu_to_le32(4);
658 buf->ccontext.NameOffset =
659 cpu_to_le16(offsetof(struct create_posix, Name));
660 buf->ccontext.NameLength = cpu_to_le16(16);
661
662 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
663 buf->Name[0] = 0x93;
664 buf->Name[1] = 0xAD;
665 buf->Name[2] = 0x25;
666 buf->Name[3] = 0x50;
667 buf->Name[4] = 0x9C;
668 buf->Name[5] = 0xB4;
669 buf->Name[6] = 0x11;
670 buf->Name[7] = 0xE7;
671 buf->Name[8] = 0xB4;
672 buf->Name[9] = 0x23;
673 buf->Name[10] = 0x83;
674 buf->Name[11] = 0xDE;
675 buf->Name[12] = 0x96;
676 buf->Name[13] = 0x8B;
677 buf->Name[14] = 0xCD;
678 buf->Name[15] = 0x7C;
679 buf->Mode = cpu_to_le32(mode);
680 cifs_dbg(FYI, "mode on posix create 0%o", mode);
681 return buf;
682}
683
684static int
685add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
686{
687 struct smb2_create_req *req = iov[0].iov_base;
688 unsigned int num = *num_iovec;
689
690 iov[num].iov_base = create_posix_buf(mode);
691 if (iov[num].iov_base == NULL)
692 return -ENOMEM;
693 iov[num].iov_len = sizeof(struct create_posix);
694 if (!req->CreateContextsOffset)
695 req->CreateContextsOffset = cpu_to_le32(
696 sizeof(struct smb2_create_req) +
697 iov[num - 1].iov_len);
698 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
699 *num_iovec = num + 1;
700 return 0;
701}
702
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500703
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400704/*
705 *
706 * SMB2 Worker functions follow:
707 *
708 * The general structure of the worker functions is:
709 * 1) Call smb2_init (assembles SMB2 header)
710 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
711 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
712 * 4) Decode SMB2 command specific fields in the fixed length area
713 * 5) Decode variable length data area (if any for this SMB2 command type)
714 * 6) Call free smb buffer
715 * 7) return
716 *
717 */
718
719int
720SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
721{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000722 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400723 struct smb2_negotiate_req *req;
724 struct smb2_negotiate_rsp *rsp;
725 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700726 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400727 int rc = 0;
728 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400729 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400730 int blob_offset, blob_length;
731 char *security_blob;
732 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100733 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400734
Joe Perchesf96637b2013-05-04 22:12:25 -0500735 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400736
Jeff Layton3534b852013-05-24 07:41:01 -0400737 if (!server) {
738 WARN(1, "%s: server is NULL!\n", __func__);
739 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400740 }
741
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100742 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400743 if (rc)
744 return rc;
745
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100746 req->sync_hdr.SessionId = 0;
Steve French0fdfef92018-06-28 19:30:23 -0500747
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100748 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
749 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400750
Steve French9764c022017-09-17 10:41:35 -0500751 if (strcmp(ses->server->vals->version_string,
752 SMB3ANY_VERSION_STRING) == 0) {
753 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
754 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
755 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100756 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500757 } else if (strcmp(ses->server->vals->version_string,
758 SMBDEFAULT_VERSION_STRING) == 0) {
759 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
760 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
761 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600762 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
763 req->DialectCount = cpu_to_le16(4);
764 total_len += 8;
Steve French9764c022017-09-17 10:41:35 -0500765 } else {
766 /* otherwise send specific dialect */
767 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
768 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100769 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500770 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400771
772 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400773 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500774 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400775 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500776 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400777 else
778 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400779
Steve Frenche4aa25e2012-10-01 12:26:22 -0500780 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400781
Steve French3c5f9be12014-05-13 13:37:45 -0700782 /* ClientGUID must be zero for SMB2.02 dialect */
783 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
784 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500785 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700786 memcpy(req->ClientGUID, server->client_guid,
787 SMB2_CLIENT_GUID_SIZE);
Steve Frenchd5c70762019-01-03 02:37:21 -0600788 if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
789 (strcmp(ses->server->vals->version_string,
790 SMBDEFAULT_VERSION_STRING) == 0))
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100791 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500792 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400793 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100794 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400795
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000796 memset(&rqst, 0, sizeof(struct smb_rqst));
797 rqst.rq_iov = iov;
798 rqst.rq_nvec = 1;
799
800 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700801 cifs_small_buf_release(req);
802 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400803 /*
804 * No tcon so can't do
805 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
806 */
Steve French7e682f72017-08-31 21:34:24 -0500807 if (rc == -EOPNOTSUPP) {
808 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500809 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500810 " older servers\n");
811 goto neg_exit;
812 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400813 goto neg_exit;
814
Steve French9764c022017-09-17 10:41:35 -0500815 if (strcmp(ses->server->vals->version_string,
816 SMB3ANY_VERSION_STRING) == 0) {
817 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
818 cifs_dbg(VFS,
819 "SMB2 dialect returned but not requested\n");
820 return -EIO;
821 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
822 cifs_dbg(VFS,
823 "SMB2.1 dialect returned but not requested\n");
824 return -EIO;
825 }
826 } else if (strcmp(ses->server->vals->version_string,
827 SMBDEFAULT_VERSION_STRING) == 0) {
828 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
829 cifs_dbg(VFS,
830 "SMB2 dialect returned but not requested\n");
831 return -EIO;
832 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
833 /* ops set to 3.0 by default for default so update */
834 ses->server->ops = &smb21_operations;
Steve Frenchd5c70762019-01-03 02:37:21 -0600835 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
836 ses->server->ops = &smb311_operations;
Steve French590d08d2017-09-19 11:43:47 -0500837 } else if (le16_to_cpu(rsp->DialectRevision) !=
838 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500839 /* if requested single dialect ensure returned dialect matched */
840 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500841 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500842 return -EIO;
843 }
844
Joe Perchesf96637b2013-05-04 22:12:25 -0500845 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400846
Steve Frenche4aa25e2012-10-01 12:26:22 -0500847 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500848 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500849 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500850 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500851 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500852 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500853 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
854 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600855 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
856 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400857 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500858 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500859 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400860 rc = -EIO;
861 goto neg_exit;
862 }
863 server->dialect = le16_to_cpu(rsp->DialectRevision);
864
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100865 /*
866 * Keep a copy of the hash after negprot. This hash will be
867 * the starting hash value for all sessions made from this
868 * server.
869 */
870 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
871 SMB2_PREAUTH_HASH_SIZE);
Steve French0fdfef92018-06-28 19:30:23 -0500872
Jeff Laytone598d1d82013-05-26 07:00:59 -0400873 /* SMB2 only has an extended negflavor */
874 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400875 /* set it to the maximum buffer size value we can send with 1 credit */
876 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
877 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400878 server->max_read = le32_to_cpu(rsp->MaxReadSize);
879 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400880 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500881 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
882 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
883 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400884 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400885 /* Internal types */
886 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400887
888 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000889 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500890 /*
891 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
892 * for us will be
893 * ses->sectype = RawNTLMSSP;
894 * but for time being this is our only auth choice so doesn't matter.
895 * We just found a server which sets blob length to zero expecting raw.
896 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700897 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500898 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700899 server->sec_ntlmssp = true;
900 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700901
Jeff Layton38d77c52013-05-26 07:01:00 -0400902 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400903 if (rc)
904 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500905 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500906 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500907 if (rc == 1)
908 rc = 0;
909 else if (rc == 0)
910 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400911 }
Steve French5100d8a2018-04-09 10:47:14 -0500912
Steve French5100d8a2018-04-09 10:47:14 -0500913 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
914 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000915 rc = smb311_decode_neg_context(rsp, server,
916 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -0500917 else
918 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
919 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400920neg_exit:
921 free_rsp_buf(resp_buftype, rsp);
922 return rc;
923}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400924
Steve Frenchff1c0382013-11-19 23:44:46 -0600925int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
926{
Long Li2796d302018-04-25 11:30:04 -0700927 int rc;
928 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200929 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600930 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500931 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600932
933 cifs_dbg(FYI, "validate negotiate\n");
934
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100935 /* In SMB3.11 preauth integrity supersedes validate negotiate */
936 if (tcon->ses->server->dialect == SMB311_PROT_ID)
937 return 0;
938
Steve Frenchff1c0382013-11-19 23:44:46 -0600939 /*
940 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500941 * can not sign it (ie are not known user). Even if signing is not
942 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600943 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500944 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600945 */
Steve French0603c962017-09-20 19:57:18 -0500946 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600947 return 0; /* validation requires signing */
948
Steve French0603c962017-09-20 19:57:18 -0500949 if (tcon->ses->user_name == NULL) {
950 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
951 return 0; /* validation requires signing */
952 }
953
954 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
955 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
956
Long Li2796d302018-04-25 11:30:04 -0700957 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
958 if (!pneg_inbuf)
959 return -ENOMEM;
960
961 pneg_inbuf->Capabilities =
Steve Frenchff1c0382013-11-19 23:44:46 -0600962 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Long Li2796d302018-04-25 11:30:04 -0700963 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100964 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600965
966 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -0700967 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600968 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
969 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -0700970 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600971 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
972 else
Long Li2796d302018-04-25 11:30:04 -0700973 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600974
Steve French9764c022017-09-17 10:41:35 -0500975
976 if (strcmp(tcon->ses->server->vals->version_string,
977 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700978 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
979 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
980 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -0500981 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -0700982 inbuflen = sizeof(*pneg_inbuf) -
Steve Frenchd5c70762019-01-03 02:37:21 -0600983 (2 * sizeof(pneg_inbuf->Dialects[0]));
Steve French9764c022017-09-17 10:41:35 -0500984 } else if (strcmp(tcon->ses->server->vals->version_string,
985 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700986 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
987 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
988 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600989 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
990 pneg_inbuf->DialectCount = cpu_to_le16(4);
Steve French9764c022017-09-17 10:41:35 -0500991 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -0700992 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -0500993 } else {
994 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -0700995 pneg_inbuf->Dialects[0] =
Steve French9764c022017-09-17 10:41:35 -0500996 cpu_to_le16(tcon->ses->server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -0700997 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -0500998 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -0700999 inbuflen = sizeof(*pneg_inbuf) -
1000 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -05001001 }
Steve Frenchff1c0382013-11-19 23:44:46 -06001002
1003 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1004 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001005 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1006 (char **)&pneg_rsp, &rsplen);
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001007 if (rc == -EOPNOTSUPP) {
1008 /*
1009 * Old Windows versions or Netapp SMB server can return
1010 * not supported error. Client should accept it.
1011 */
1012 cifs_dbg(VFS, "Server does not support validate negotiate\n");
1013 return 0;
1014 } else if (rc != 0) {
Steve Frenchff1c0382013-11-19 23:44:46 -06001015 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -07001016 rc = -EIO;
1017 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -06001018 }
1019
Long Li2796d302018-04-25 11:30:04 -07001020 rc = -EIO;
1021 if (rsplen != sizeof(*pneg_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -05001022 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1023 rsplen);
1024
1025 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -07001026 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1027 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001028 }
1029
1030 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +01001031 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -06001032 goto vneg_out;
1033
1034 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
1035 goto vneg_out;
1036
1037 /* do not validate server guid because not saved at negprot time yet */
1038
1039 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1040 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
1041 goto vneg_out;
1042
1043 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -07001044 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001045 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -07001046 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001047
1048vneg_out:
1049 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -07001050out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001051 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -07001052out_free_inbuf:
1053 kfree(pneg_inbuf);
1054 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -06001055}
1056
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301057enum securityEnum
1058smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1059{
1060 switch (requested) {
1061 case Kerberos:
1062 case RawNTLMSSP:
1063 return requested;
1064 case NTLMv2:
1065 return RawNTLMSSP;
1066 case Unspecified:
1067 if (server->sec_ntlmssp &&
1068 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1069 return RawNTLMSSP;
1070 if ((server->sec_kerberos || server->sec_mskerberos) &&
1071 (global_secflags & CIFSSEC_MAY_KRB5))
1072 return Kerberos;
1073 /* Fallthrough */
1074 default:
1075 return Unspecified;
1076 }
1077}
1078
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001079struct SMB2_sess_data {
1080 unsigned int xid;
1081 struct cifs_ses *ses;
1082 struct nls_table *nls_cp;
1083 void (*func)(struct SMB2_sess_data *);
1084 int result;
1085 u64 previous_session;
1086
1087 /* we will send the SMB in three pieces:
1088 * a fixed length beginning part, an optional
1089 * SPNEGO blob (which can be zero length), and a
1090 * last part which will include the strings
1091 * and rest of bcc area. This allows us to avoid
1092 * a large buffer 17K allocation
1093 */
1094 int buf0_type;
1095 struct kvec iov[2];
1096};
1097
1098static int
1099SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1100{
1101 int rc;
1102 struct cifs_ses *ses = sess_data->ses;
1103 struct smb2_sess_setup_req *req;
1104 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001105 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001106
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001107 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1108 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001109 if (rc)
1110 return rc;
1111
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001112 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001113 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001114
1115 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1116 req->PreviousSessionId = sess_data->previous_session;
1117
1118 req->Flags = 0; /* MBZ */
Steve Frenchd4090142018-06-13 17:05:58 -05001119
1120 /* enough to enable echos and oplocks and one max size write */
1121 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001122
1123 /* only one of SMB2 signing flags may be set in SMB2 request */
1124 if (server->sign)
1125 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1126 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1127 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1128 else
1129 req->SecurityMode = 0;
1130
1131 req->Capabilities = 0;
1132 req->Channel = 0; /* MBZ */
1133
1134 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001135 /* 1 for pad */
1136 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001137 /*
1138 * This variable will be used to clear the buffer
1139 * allocated above in case of any error in the calling function.
1140 */
1141 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1142
1143 return 0;
1144}
1145
1146static void
1147SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1148{
1149 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1150 sess_data->buf0_type = CIFS_NO_BUFFER;
1151}
1152
1153static int
1154SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1155{
1156 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001157 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001158 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001159 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001160
1161 /* Testing shows that buffer offset must be at location of Buffer[0] */
1162 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001163 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001164 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1165
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001166 memset(&rqst, 0, sizeof(struct smb_rqst));
1167 rqst.rq_iov = sess_data->iov;
1168 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001169
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001170 /* BB add code to build os and lm fields */
1171 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1172 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001173 &sess_data->buf0_type,
1174 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001175 cifs_small_buf_release(sess_data->iov[0].iov_base);
1176 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001177
1178 return rc;
1179}
1180
1181static int
1182SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1183{
1184 int rc = 0;
1185 struct cifs_ses *ses = sess_data->ses;
1186
1187 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001188 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001189 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001190 if (rc) {
1191 cifs_dbg(FYI,
1192 "SMB3 session key generation failed\n");
1193 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001194 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001195 }
1196 }
1197 if (!ses->server->session_estab) {
1198 ses->server->sequence_number = 0x2;
1199 ses->server->session_estab = true;
1200 }
1201 mutex_unlock(&ses->server->srv_mutex);
1202
1203 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1204 spin_lock(&GlobalMid_Lock);
1205 ses->status = CifsGood;
1206 ses->need_reconnect = false;
1207 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001208 return rc;
1209}
1210
1211#ifdef CONFIG_CIFS_UPCALL
1212static void
1213SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1214{
1215 int rc;
1216 struct cifs_ses *ses = sess_data->ses;
1217 struct cifs_spnego_msg *msg;
1218 struct key *spnego_key = NULL;
1219 struct smb2_sess_setup_rsp *rsp = NULL;
1220
1221 rc = SMB2_sess_alloc_buffer(sess_data);
1222 if (rc)
1223 goto out;
1224
1225 spnego_key = cifs_get_spnego_key(ses);
1226 if (IS_ERR(spnego_key)) {
1227 rc = PTR_ERR(spnego_key);
1228 spnego_key = NULL;
1229 goto out;
1230 }
1231
1232 msg = spnego_key->payload.data[0];
1233 /*
1234 * check version field to make sure that cifs.upcall is
1235 * sending us a response in an expected form
1236 */
1237 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1238 cifs_dbg(VFS,
1239 "bad cifs.upcall version. Expected %d got %d",
1240 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1241 rc = -EKEYREJECTED;
1242 goto out_put_spnego_key;
1243 }
1244
1245 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1246 GFP_KERNEL);
1247 if (!ses->auth_key.response) {
1248 cifs_dbg(VFS,
1249 "Kerberos can't allocate (%u bytes) memory",
1250 msg->sesskey_len);
1251 rc = -ENOMEM;
1252 goto out_put_spnego_key;
1253 }
1254 ses->auth_key.len = msg->sesskey_len;
1255
1256 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1257 sess_data->iov[1].iov_len = msg->secblob_len;
1258
1259 rc = SMB2_sess_sendreceive(sess_data);
1260 if (rc)
1261 goto out_put_spnego_key;
1262
1263 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001264 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001265
1266 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001267
1268 rc = SMB2_sess_establish_session(sess_data);
1269out_put_spnego_key:
1270 key_invalidate(spnego_key);
1271 key_put(spnego_key);
1272out:
1273 sess_data->result = rc;
1274 sess_data->func = NULL;
1275 SMB2_sess_free_buffer(sess_data);
1276}
1277#else
1278static void
1279SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1280{
1281 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1282 sess_data->result = -EOPNOTSUPP;
1283 sess_data->func = NULL;
1284}
1285#endif
1286
Sachin Prabhu166cea42016-10-07 19:11:22 +01001287static void
1288SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1289
1290static void
1291SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1292{
1293 int rc;
1294 struct cifs_ses *ses = sess_data->ses;
1295 struct smb2_sess_setup_rsp *rsp = NULL;
1296 char *ntlmssp_blob = NULL;
1297 bool use_spnego = false; /* else use raw ntlmssp */
1298 u16 blob_length = 0;
1299
1300 /*
1301 * If memory allocation is successful, caller of this function
1302 * frees it.
1303 */
1304 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1305 if (!ses->ntlmssp) {
1306 rc = -ENOMEM;
1307 goto out_err;
1308 }
1309 ses->ntlmssp->sesskey_per_smbsess = true;
1310
1311 rc = SMB2_sess_alloc_buffer(sess_data);
1312 if (rc)
1313 goto out_err;
1314
1315 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1316 GFP_KERNEL);
1317 if (ntlmssp_blob == NULL) {
1318 rc = -ENOMEM;
1319 goto out;
1320 }
1321
1322 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1323 if (use_spnego) {
1324 /* BB eventually need to add this */
1325 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1326 rc = -EOPNOTSUPP;
1327 goto out;
1328 } else {
1329 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1330 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1331 }
1332 sess_data->iov[1].iov_base = ntlmssp_blob;
1333 sess_data->iov[1].iov_len = blob_length;
1334
1335 rc = SMB2_sess_sendreceive(sess_data);
1336 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1337
1338 /* If true, rc here is expected and not an error */
1339 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001340 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001341 rc = 0;
1342
1343 if (rc)
1344 goto out;
1345
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001346 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001347 le16_to_cpu(rsp->SecurityBufferOffset)) {
1348 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1349 le16_to_cpu(rsp->SecurityBufferOffset));
1350 rc = -EIO;
1351 goto out;
1352 }
1353 rc = decode_ntlmssp_challenge(rsp->Buffer,
1354 le16_to_cpu(rsp->SecurityBufferLength), ses);
1355 if (rc)
1356 goto out;
1357
1358 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1359
1360
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001361 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001362 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001363
1364out:
1365 kfree(ntlmssp_blob);
1366 SMB2_sess_free_buffer(sess_data);
1367 if (!rc) {
1368 sess_data->result = 0;
1369 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1370 return;
1371 }
1372out_err:
1373 kfree(ses->ntlmssp);
1374 ses->ntlmssp = NULL;
1375 sess_data->result = rc;
1376 sess_data->func = NULL;
1377}
1378
1379static void
1380SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1381{
1382 int rc;
1383 struct cifs_ses *ses = sess_data->ses;
1384 struct smb2_sess_setup_req *req;
1385 struct smb2_sess_setup_rsp *rsp = NULL;
1386 unsigned char *ntlmssp_blob = NULL;
1387 bool use_spnego = false; /* else use raw ntlmssp */
1388 u16 blob_length = 0;
1389
1390 rc = SMB2_sess_alloc_buffer(sess_data);
1391 if (rc)
1392 goto out;
1393
1394 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001395 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001396
1397 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1398 sess_data->nls_cp);
1399 if (rc) {
1400 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1401 goto out;
1402 }
1403
1404 if (use_spnego) {
1405 /* BB eventually need to add this */
1406 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1407 rc = -EOPNOTSUPP;
1408 goto out;
1409 }
1410 sess_data->iov[1].iov_base = ntlmssp_blob;
1411 sess_data->iov[1].iov_len = blob_length;
1412
1413 rc = SMB2_sess_sendreceive(sess_data);
1414 if (rc)
1415 goto out;
1416
1417 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1418
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001419 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001420 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001421
1422 rc = SMB2_sess_establish_session(sess_data);
1423out:
1424 kfree(ntlmssp_blob);
1425 SMB2_sess_free_buffer(sess_data);
1426 kfree(ses->ntlmssp);
1427 ses->ntlmssp = NULL;
1428 sess_data->result = rc;
1429 sess_data->func = NULL;
1430}
1431
1432static int
1433SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1434{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301435 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001436
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301437 type = smb2_select_sectype(ses->server, ses->sectype);
1438 cifs_dbg(FYI, "sess setup type %d\n", type);
1439 if (type == Unspecified) {
1440 cifs_dbg(VFS,
1441 "Unable to select appropriate authentication method!");
1442 return -EINVAL;
1443 }
1444
1445 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001446 case Kerberos:
1447 sess_data->func = SMB2_auth_kerberos;
1448 break;
1449 case RawNTLMSSP:
1450 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1451 break;
1452 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301453 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001454 return -EOPNOTSUPP;
1455 }
1456
1457 return 0;
1458}
1459
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001460int
1461SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1462 const struct nls_table *nls_cp)
1463{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001464 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001465 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001466 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001467
Joe Perchesf96637b2013-05-04 22:12:25 -05001468 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001469
Jeff Layton3534b852013-05-24 07:41:01 -04001470 if (!server) {
1471 WARN(1, "%s: server is NULL!\n", __func__);
1472 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001473 }
1474
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001475 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1476 if (!sess_data)
1477 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001478
1479 rc = SMB2_select_sec(ses, sess_data);
1480 if (rc)
1481 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001482 sess_data->xid = xid;
1483 sess_data->ses = ses;
1484 sess_data->buf0_type = CIFS_NO_BUFFER;
1485 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001486 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001487
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001488 /*
1489 * Initialize the session hash with the server one.
1490 */
1491 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1492 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001493
Sachin Prabhu166cea42016-10-07 19:11:22 +01001494 while (sess_data->func)
1495 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001496
Steve Frenchc721c382017-09-19 18:40:03 -05001497 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1498 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001499 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001500out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001501 kfree(sess_data);
1502 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001503}
1504
1505int
1506SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1507{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001508 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001509 struct smb2_logoff_req *req; /* response is also trivial struct */
1510 int rc = 0;
1511 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001512 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001513 unsigned int total_len;
1514 struct kvec iov[1];
1515 struct kvec rsp_iov;
1516 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001517
Joe Perchesf96637b2013-05-04 22:12:25 -05001518 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001519
1520 if (ses && (ses->server))
1521 server = ses->server;
1522 else
1523 return -EIO;
1524
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001525 /* no need to send SMB logoff if uid already closed due to reconnect */
1526 if (ses->need_reconnect)
1527 goto smb2_session_already_dead;
1528
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001529 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001530 if (rc)
1531 return rc;
1532
1533 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001534 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001535
1536 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1537 flags |= CIFS_TRANSFORM_REQ;
1538 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001539 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001540
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001541 flags |= CIFS_NO_RESP;
1542
1543 iov[0].iov_base = (char *)req;
1544 iov[0].iov_len = total_len;
1545
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001546 memset(&rqst, 0, sizeof(struct smb_rqst));
1547 rqst.rq_iov = iov;
1548 rqst.rq_nvec = 1;
1549
1550 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001551 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001552 /*
1553 * No tcon so can't do
1554 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1555 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001556
1557smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001558 return rc;
1559}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001560
1561static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1562{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001563 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001564}
1565
1566#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1567
Steve Frenchde9f68df2013-11-15 11:26:24 -06001568/* These are similar values to what Windows uses */
1569static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1570{
1571 tcon->max_chunks = 256;
1572 tcon->max_bytes_chunk = 1048576;
1573 tcon->max_bytes_copy = 16777216;
1574}
1575
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001576int
1577SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1578 struct cifs_tcon *tcon, const struct nls_table *cp)
1579{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001580 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001581 struct smb2_tree_connect_req *req;
1582 struct smb2_tree_connect_rsp *rsp = NULL;
1583 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001584 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001585 int rc = 0;
1586 int resp_buftype;
1587 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001588 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001589 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001590 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001591
Joe Perchesf96637b2013-05-04 22:12:25 -05001592 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001593
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001594 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001595 return -EIO;
1596
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001597 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1598 if (unc_path == NULL)
1599 return -ENOMEM;
1600
1601 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1602 unc_path_len *= 2;
1603 if (unc_path_len < 2) {
1604 kfree(unc_path);
1605 return -EINVAL;
1606 }
1607
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001608 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001609 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001610 atomic_set(&tcon->num_remote_opens, 0);
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001611 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1612 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001613 if (rc) {
1614 kfree(unc_path);
1615 return rc;
1616 }
1617
Steve French5a77e752018-05-09 17:43:08 -05001618 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001619 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001620
1621 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001622 /* 1 for pad */
1623 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001624
1625 /* Testing shows that buffer offset must be at location of Buffer[0] */
1626 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001627 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001628 req->PathLength = cpu_to_le16(unc_path_len - 2);
1629 iov[1].iov_base = unc_path;
1630 iov[1].iov_len = unc_path_len;
1631
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001632 /*
1633 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1634 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
Steve French8c11a602019-03-22 22:31:17 -05001635 * (Samba servers don't always set the flag so also check if null user)
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001636 */
Steve French6188f282018-03-13 02:29:36 -05001637 if ((ses->server->dialect == SMB311_PROT_ID) &&
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001638 !smb3_encryption_required(tcon) &&
Steve French8c11a602019-03-22 22:31:17 -05001639 !(ses->session_flags &
1640 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1641 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
Steve French6188f282018-03-13 02:29:36 -05001642 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1643
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001644 memset(&rqst, 0, sizeof(struct smb_rqst));
1645 rqst.rq_iov = iov;
1646 rqst.rq_nvec = 2;
1647
Steve French4fe75c42019-02-14 01:19:02 -06001648 /* Need 64 for max size write so ask for more in case not there yet */
1649 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1650
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001651 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001652 cifs_small_buf_release(req);
1653 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001654 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001655 if (rc != 0) {
1656 if (tcon) {
1657 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1658 tcon->need_reconnect = true;
1659 }
1660 goto tcon_error_exit;
1661 }
1662
Christophe JAILLETcd123002017-05-12 17:59:32 +02001663 switch (rsp->ShareType) {
1664 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001665 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001666 break;
1667 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001668 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001669 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001670 break;
1671 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001672 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001673 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001674 break;
1675 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001676 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001677 rc = -EOPNOTSUPP;
1678 goto tcon_error_exit;
1679 }
1680
1681 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001682 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001683 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1684 tcon->tidStatus = CifsGood;
1685 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001686 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001687 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001688
1689 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1690 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001691 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001692
1693 if (tcon->seal &&
1694 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1695 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1696
Steve Frenchde9f68df2013-11-15 11:26:24 -06001697 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001698 if (tcon->ses->server->ops->validate_negotiate)
1699 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001700tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001701
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001702 free_rsp_buf(resp_buftype, rsp);
1703 kfree(unc_path);
1704 return rc;
1705
1706tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001707 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001708 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001709 }
1710 goto tcon_exit;
1711}
1712
1713int
1714SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1715{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001716 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001717 struct smb2_tree_disconnect_req *req; /* response is trivial */
1718 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001719 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001720 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001721 unsigned int total_len;
1722 struct kvec iov[1];
1723 struct kvec rsp_iov;
1724 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001725
Joe Perchesf96637b2013-05-04 22:12:25 -05001726 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001727
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001728 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001729 return -EIO;
1730
1731 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1732 return 0;
1733
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001734 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1735 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001736 if (rc)
1737 return rc;
1738
Steve French5a77e752018-05-09 17:43:08 -05001739 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001740 flags |= CIFS_TRANSFORM_REQ;
1741
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001742 flags |= CIFS_NO_RESP;
1743
1744 iov[0].iov_base = (char *)req;
1745 iov[0].iov_len = total_len;
1746
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001747 memset(&rqst, 0, sizeof(struct smb_rqst));
1748 rqst.rq_iov = iov;
1749 rqst.rq_nvec = 1;
1750
1751 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001752 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001753 if (rc)
1754 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1755
1756 return rc;
1757}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001758
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001759
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001760static struct create_durable *
1761create_durable_buf(void)
1762{
1763 struct create_durable *buf;
1764
1765 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1766 if (!buf)
1767 return NULL;
1768
1769 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001770 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001771 buf->ccontext.DataLength = cpu_to_le32(16);
1772 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1773 (struct create_durable, Name));
1774 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001775 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001776 buf->Name[0] = 'D';
1777 buf->Name[1] = 'H';
1778 buf->Name[2] = 'n';
1779 buf->Name[3] = 'Q';
1780 return buf;
1781}
1782
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001783static struct create_durable *
1784create_reconnect_durable_buf(struct cifs_fid *fid)
1785{
1786 struct create_durable *buf;
1787
1788 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1789 if (!buf)
1790 return NULL;
1791
1792 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1793 (struct create_durable, Data));
1794 buf->ccontext.DataLength = cpu_to_le32(16);
1795 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1796 (struct create_durable, Name));
1797 buf->ccontext.NameLength = cpu_to_le16(4);
1798 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1799 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001800 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001801 buf->Name[0] = 'D';
1802 buf->Name[1] = 'H';
1803 buf->Name[2] = 'n';
1804 buf->Name[3] = 'C';
1805 return buf;
1806}
1807
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +10001808__u8
1809smb2_parse_lease_state(struct TCP_Server_Info *server,
1810 struct smb2_create_rsp *rsp,
1811 unsigned int *epoch, char *lease_key)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001812{
1813 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001814 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001815 unsigned int next;
1816 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001817 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001818
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001819 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001820 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001821 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001822 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001823 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001824 if (le16_to_cpu(cc->NameLength) == 4 &&
1825 strncmp(name, "RqLs", 4) == 0)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001826 return server->ops->parse_lease_buf(cc, epoch,
1827 lease_key);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001828
1829 next = le32_to_cpu(cc->Next);
1830 if (!next)
1831 break;
1832 remaining -= next;
1833 cc = (struct create_context *)((char *)cc + next);
1834 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001835
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001836 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001837}
1838
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001839static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001840add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02001841 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001842{
1843 struct smb2_create_req *req = iov[0].iov_base;
1844 unsigned int num = *num_iovec;
1845
Stefano Brivio729c0c92018-07-05 15:10:02 +02001846 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001847 if (iov[num].iov_base == NULL)
1848 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001849 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001850 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1851 if (!req->CreateContextsOffset)
1852 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001853 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001854 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001855 le32_add_cpu(&req->CreateContextsLength,
1856 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001857 *num_iovec = num + 1;
1858 return 0;
1859}
1860
Steve Frenchb56eae42015-11-03 09:26:27 -06001861static struct create_durable_v2 *
1862create_durable_v2_buf(struct cifs_fid *pfid)
1863{
1864 struct create_durable_v2 *buf;
1865
1866 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1867 if (!buf)
1868 return NULL;
1869
1870 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1871 (struct create_durable_v2, dcontext));
1872 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1873 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1874 (struct create_durable_v2, Name));
1875 buf->ccontext.NameLength = cpu_to_le16(4);
1876
1877 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1878 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001879 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001880 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1881
1882 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1883 buf->Name[0] = 'D';
1884 buf->Name[1] = 'H';
1885 buf->Name[2] = '2';
1886 buf->Name[3] = 'Q';
1887 return buf;
1888}
1889
1890static struct create_durable_handle_reconnect_v2 *
1891create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1892{
1893 struct create_durable_handle_reconnect_v2 *buf;
1894
1895 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1896 GFP_KERNEL);
1897 if (!buf)
1898 return NULL;
1899
1900 buf->ccontext.DataOffset =
1901 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1902 dcontext));
1903 buf->ccontext.DataLength =
1904 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1905 buf->ccontext.NameOffset =
1906 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1907 Name));
1908 buf->ccontext.NameLength = cpu_to_le16(4);
1909
1910 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1911 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1912 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1913 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1914
1915 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1916 buf->Name[0] = 'D';
1917 buf->Name[1] = 'H';
1918 buf->Name[2] = '2';
1919 buf->Name[3] = 'C';
1920 return buf;
1921}
1922
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001923static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001924add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001925 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001926{
1927 struct smb2_create_req *req = iov[0].iov_base;
1928 unsigned int num = *num_iovec;
1929
Steve Frenchb56eae42015-11-03 09:26:27 -06001930 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1931 if (iov[num].iov_base == NULL)
1932 return -ENOMEM;
1933 iov[num].iov_len = sizeof(struct create_durable_v2);
1934 if (!req->CreateContextsOffset)
1935 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001936 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001937 iov[1].iov_len);
1938 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001939 *num_iovec = num + 1;
1940 return 0;
1941}
1942
1943static int
1944add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1945 struct cifs_open_parms *oparms)
1946{
1947 struct smb2_create_req *req = iov[0].iov_base;
1948 unsigned int num = *num_iovec;
1949
1950 /* indicate that we don't need to relock the file */
1951 oparms->reconnect = false;
1952
1953 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1954 if (iov[num].iov_base == NULL)
1955 return -ENOMEM;
1956 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1957 if (!req->CreateContextsOffset)
1958 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001959 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001960 iov[1].iov_len);
1961 le32_add_cpu(&req->CreateContextsLength,
1962 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001963 *num_iovec = num + 1;
1964 return 0;
1965}
1966
1967static int
1968add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1969 struct cifs_open_parms *oparms, bool use_persistent)
1970{
1971 struct smb2_create_req *req = iov[0].iov_base;
1972 unsigned int num = *num_iovec;
1973
1974 if (use_persistent) {
1975 if (oparms->reconnect)
1976 return add_durable_reconnect_v2_context(iov, num_iovec,
1977 oparms);
1978 else
1979 return add_durable_v2_context(iov, num_iovec, oparms);
1980 }
1981
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001982 if (oparms->reconnect) {
1983 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1984 /* indicate that we don't need to relock the file */
1985 oparms->reconnect = false;
1986 } else
1987 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001988 if (iov[num].iov_base == NULL)
1989 return -ENOMEM;
1990 iov[num].iov_len = sizeof(struct create_durable);
1991 if (!req->CreateContextsOffset)
1992 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001993 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001994 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001995 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001996 *num_iovec = num + 1;
1997 return 0;
1998}
1999
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002000/* See MS-SMB2 2.2.13.2.7 */
2001static struct crt_twarp_ctxt *
2002create_twarp_buf(__u64 timewarp)
2003{
2004 struct crt_twarp_ctxt *buf;
2005
2006 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2007 if (!buf)
2008 return NULL;
2009
2010 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2011 (struct crt_twarp_ctxt, Timestamp));
2012 buf->ccontext.DataLength = cpu_to_le32(8);
2013 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2014 (struct crt_twarp_ctxt, Name));
2015 buf->ccontext.NameLength = cpu_to_le16(4);
2016 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2017 buf->Name[0] = 'T';
2018 buf->Name[1] = 'W';
2019 buf->Name[2] = 'r';
2020 buf->Name[3] = 'p';
2021 buf->Timestamp = cpu_to_le64(timewarp);
2022 return buf;
2023}
2024
2025/* See MS-SMB2 2.2.13.2.7 */
2026static int
2027add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2028{
2029 struct smb2_create_req *req = iov[0].iov_base;
2030 unsigned int num = *num_iovec;
2031
2032 iov[num].iov_base = create_twarp_buf(timewarp);
2033 if (iov[num].iov_base == NULL)
2034 return -ENOMEM;
2035 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2036 if (!req->CreateContextsOffset)
2037 req->CreateContextsOffset = cpu_to_le32(
2038 sizeof(struct smb2_create_req) +
2039 iov[num - 1].iov_len);
2040 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2041 *num_iovec = num + 1;
2042 return 0;
2043}
2044
Aurelien Aptelf0712922017-02-22 14:47:17 +01002045static int
2046alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2047 const char *treename, const __le16 *path)
2048{
2049 int treename_len, path_len;
2050 struct nls_table *cp;
2051 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2052
2053 /*
2054 * skip leading "\\"
2055 */
2056 treename_len = strlen(treename);
2057 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2058 return -EINVAL;
2059
2060 treename += 2;
2061 treename_len -= 2;
2062
2063 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2064
2065 /*
2066 * make room for one path separator between the treename and
2067 * path
2068 */
2069 *out_len = treename_len + 1 + path_len;
2070
2071 /*
2072 * final path needs to be null-terminated UTF16 with a
2073 * size aligned to 8
2074 */
2075
2076 *out_size = roundup((*out_len+1)*2, 8);
2077 *out_path = kzalloc(*out_size, GFP_KERNEL);
2078 if (!*out_path)
2079 return -ENOMEM;
2080
2081 cp = load_nls_default();
2082 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2083 UniStrcat(*out_path, sep);
2084 UniStrcat(*out_path, path);
2085 unload_nls(cp);
2086
2087 return 0;
2088}
2089
Steve Frenchbea851b2018-06-14 21:56:32 -05002090int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2091 umode_t mode, struct cifs_tcon *tcon,
2092 const char *full_path,
2093 struct cifs_sb_info *cifs_sb)
2094{
2095 struct smb_rqst rqst;
2096 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002097 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002098 struct cifs_ses *ses = tcon->ses;
2099 struct kvec iov[3]; /* make sure at least one for each open context */
2100 struct kvec rsp_iov = {NULL, 0};
2101 int resp_buftype;
2102 int uni_path_len;
2103 __le16 *copy_path = NULL;
2104 int copy_size;
2105 int rc = 0;
2106 unsigned int n_iov = 2;
2107 __u32 file_attributes = 0;
2108 char *pc_buf = NULL;
2109 int flags = 0;
2110 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002111 __le16 *utf16_path = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002112
2113 cifs_dbg(FYI, "mkdir\n");
2114
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002115 /* resource #1: path allocation */
2116 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2117 if (!utf16_path)
2118 return -ENOMEM;
2119
YueHaibing29cbfa12018-12-18 02:51:51 +00002120 if (!ses || !(ses->server)) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002121 rc = -EIO;
2122 goto err_free_path;
2123 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002124
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002125 /* resource #2: request */
Steve Frenchbea851b2018-06-14 21:56:32 -05002126 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002127 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002128 goto err_free_path;
2129
Steve Frenchbea851b2018-06-14 21:56:32 -05002130
2131 if (smb3_encryption_required(tcon))
2132 flags |= CIFS_TRANSFORM_REQ;
2133
Steve Frenchbea851b2018-06-14 21:56:32 -05002134 req->ImpersonationLevel = IL_IMPERSONATION;
2135 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2136 /* File attributes ignored on open (used in create though) */
2137 req->FileAttributes = cpu_to_le32(file_attributes);
2138 req->ShareAccess = FILE_SHARE_ALL_LE;
2139 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2140 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2141
2142 iov[0].iov_base = (char *)req;
2143 /* -1 since last byte is buf[0] which is sent below (path) */
2144 iov[0].iov_len = total_len - 1;
2145
2146 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2147
2148 /* [MS-SMB2] 2.2.13 NameOffset:
2149 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2150 * the SMB2 header, the file name includes a prefix that will
2151 * be processed during DFS name normalization as specified in
2152 * section 3.3.5.9. Otherwise, the file name is relative to
2153 * the share that is identified by the TreeId in the SMB2
2154 * header.
2155 */
2156 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2157 int name_len;
2158
2159 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2160 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2161 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002162 tcon->treeName, utf16_path);
2163 if (rc)
2164 goto err_free_req;
2165
Steve Frenchbea851b2018-06-14 21:56:32 -05002166 req->NameLength = cpu_to_le16(name_len * 2);
2167 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002168 /* free before overwriting resource */
2169 kfree(utf16_path);
2170 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002171 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002172 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002173 /* MUST set path len (NameLength) to 0 opening root of share */
2174 req->NameLength = cpu_to_le16(uni_path_len - 2);
2175 if (uni_path_len % 8 != 0) {
2176 copy_size = roundup(uni_path_len, 8);
2177 copy_path = kzalloc(copy_size, GFP_KERNEL);
2178 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002179 rc = -ENOMEM;
2180 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002181 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002182 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002183 uni_path_len);
2184 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002185 /* free before overwriting resource */
2186 kfree(utf16_path);
2187 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002188 }
2189 }
2190
2191 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002192 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002193 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2194
2195 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002196 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002197 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002198 if (rc)
2199 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002200 pc_buf = iov[n_iov-1].iov_base;
2201 }
2202
2203
2204 memset(&rqst, 0, sizeof(struct smb_rqst));
2205 rqst.rq_iov = iov;
2206 rqst.rq_nvec = n_iov;
2207
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002208 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2209 FILE_WRITE_ATTRIBUTES);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002210 /* resource #4: response buffer */
2211 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2212 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002213 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2214 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002215 CREATE_NOT_FILE,
2216 FILE_WRITE_ATTRIBUTES, rc);
2217 goto err_free_rsp_buf;
2218 }
2219
2220 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2221 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2222 ses->Suid, CREATE_NOT_FILE,
2223 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002224
2225 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2226
2227 /* Eventually save off posix specific response info and timestaps */
2228
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002229err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002230 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002231 kfree(pc_buf);
2232err_free_req:
2233 cifs_small_buf_release(req);
2234err_free_path:
2235 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002236 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002237}
Steve Frenchbea851b2018-06-14 21:56:32 -05002238
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002239int
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002240SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2241 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002242{
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002243 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002244 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002245 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002246 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002247 int copy_size;
2248 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002249 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002250 struct kvec *iov = rqst->rq_iov;
2251 __le16 *copy_path;
2252 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002253
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002254 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002255 if (rc)
2256 return rc;
2257
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002258 iov[0].iov_base = (char *)req;
2259 /* -1 since last byte is buf[0] which is sent below (path) */
2260 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002261
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002262 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002263 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002264 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2265 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002266
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002267 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002268 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002269 /* File attributes ignored on open (used in create though) */
2270 req->FileAttributes = cpu_to_le32(file_attributes);
2271 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002272 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2273 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002274 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002275
2276 /* [MS-SMB2] 2.2.13 NameOffset:
2277 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2278 * the SMB2 header, the file name includes a prefix that will
2279 * be processed during DFS name normalization as specified in
2280 * section 3.3.5.9. Otherwise, the file name is relative to
2281 * the share that is identified by the TreeId in the SMB2
2282 * header.
2283 */
2284 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2285 int name_len;
2286
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002287 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002288 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2289 &name_len,
2290 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002291 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002292 return rc;
2293 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002294 uni_path_len = copy_size;
2295 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002296 } else {
2297 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2298 /* MUST set path len (NameLength) to 0 opening root of share */
2299 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002300 copy_size = uni_path_len;
2301 if (copy_size % 8 != 0)
2302 copy_size = roundup(copy_size, 8);
2303 copy_path = kzalloc(copy_size, GFP_KERNEL);
2304 if (!copy_path)
2305 return -ENOMEM;
2306 memcpy((char *)copy_path, (const char *)path,
2307 uni_path_len);
2308 uni_path_len = copy_size;
2309 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002310 }
2311
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002312 iov[1].iov_len = uni_path_len;
2313 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002314
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002315 if (!server->oplocks)
2316 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2317
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002318 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002319 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2320 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002321 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2322 (oparms->create_options & CREATE_NOT_FILE))
2323 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002324 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002325 rc = add_lease_context(server, iov, &n_iov,
2326 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002327 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002328 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002329 }
2330
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002331 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2332 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002333 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002334 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002335 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002336 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002337 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002338 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002339
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002340 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002341 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002342 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002343 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002344 }
2345
Steve Frenchce558b02018-05-31 19:16:54 -05002346 if (tcon->posix_extensions) {
2347 if (n_iov > 2) {
2348 struct create_context *ccontext =
2349 (struct create_context *)iov[n_iov-1].iov_base;
2350 ccontext->Next =
2351 cpu_to_le32(iov[n_iov-1].iov_len);
2352 }
2353
2354 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002355 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002356 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002357 }
Steve Frenchce558b02018-05-31 19:16:54 -05002358
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002359 if (tcon->snapshot_time) {
2360 cifs_dbg(FYI, "adding snapshot context\n");
2361 if (n_iov > 2) {
2362 struct create_context *ccontext =
2363 (struct create_context *)iov[n_iov-1].iov_base;
2364 ccontext->Next =
2365 cpu_to_le32(iov[n_iov-1].iov_len);
2366 }
2367
2368 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2369 if (rc)
2370 return rc;
2371 }
2372
2373
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002374 rqst->rq_nvec = n_iov;
2375 return 0;
2376}
2377
2378/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2379 * All other vectors are freed by kfree().
2380 */
2381void
2382SMB2_open_free(struct smb_rqst *rqst)
2383{
2384 int i;
2385
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002386 if (rqst && rqst->rq_iov) {
2387 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2388 for (i = 1; i < rqst->rq_nvec; i++)
2389 if (rqst->rq_iov[i].iov_base != smb2_padding)
2390 kfree(rqst->rq_iov[i].iov_base);
2391 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002392}
2393
2394int
2395SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2396 __u8 *oplock, struct smb2_file_all_info *buf,
2397 struct kvec *err_iov, int *buftype)
2398{
2399 struct smb_rqst rqst;
2400 struct smb2_create_rsp *rsp = NULL;
2401 struct TCP_Server_Info *server;
2402 struct cifs_tcon *tcon = oparms->tcon;
2403 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002404 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002405 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002406 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002407 int rc = 0;
2408 int flags = 0;
2409
2410 cifs_dbg(FYI, "create/open\n");
2411 if (ses && (ses->server))
2412 server = ses->server;
2413 else
2414 return -EIO;
2415
2416 if (smb3_encryption_required(tcon))
2417 flags |= CIFS_TRANSFORM_REQ;
2418
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002419 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002420 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002421 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002422 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002423
2424 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2425 if (rc)
2426 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002427
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002428 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2429 oparms->create_options, oparms->desired_access);
2430
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002431 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002432 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002433 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002434
2435 if (rc != 0) {
2436 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002437 if (err_iov && rsp) {
2438 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002439 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002440 resp_buftype = CIFS_NO_BUFFER;
2441 rsp = NULL;
2442 }
Steve French28d59362018-05-30 21:42:34 -05002443 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2444 oparms->create_options, oparms->desired_access, rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002445 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002446 } else
2447 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2448 ses->Suid, oparms->create_options,
2449 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002450
Steve Frenchfae80442018-10-19 17:14:32 -05002451 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002452 oparms->fid->persistent_fid = rsp->PersistentFileId;
2453 oparms->fid->volatile_fid = rsp->VolatileFileId;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002454#ifdef CONFIG_CIFS_DEBUG2
2455 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2456#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002457
2458 if (buf) {
2459 memcpy(buf, &rsp->CreationTime, 32);
2460 buf->AllocationSize = rsp->AllocationSize;
2461 buf->EndOfFile = rsp->EndofFile;
2462 buf->Attributes = rsp->FileAttributes;
2463 buf->NumberOfLinks = cpu_to_le32(1);
2464 buf->DeletePending = 0;
2465 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002466
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002467 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +10002468 *oplock = smb2_parse_lease_state(server, rsp,
2469 &oparms->fid->epoch,
2470 oparms->fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002471 else
2472 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002473creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002474 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002475 free_rsp_buf(resp_buftype, rsp);
2476 return rc;
2477}
2478
Steve French4a72daf2013-06-25 00:20:49 -05002479int
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002480SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2481 u64 persistent_fid, u64 volatile_fid, u32 opcode,
Steve French153322f2019-03-28 22:32:49 -05002482 bool is_fsctl, char *in_data, u32 indatalen,
2483 __u32 max_response_size)
Steve French4a72daf2013-06-25 00:20:49 -05002484{
2485 struct smb2_ioctl_req *req;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002486 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002487 unsigned int total_len;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002488 int rc;
Steve French4a72daf2013-06-25 00:20:49 -05002489
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002490 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002491 if (rc)
2492 return rc;
2493
2494 req->CtlCode = cpu_to_le32(opcode);
2495 req->PersistentFileId = persistent_fid;
2496 req->VolatileFileId = volatile_fid;
2497
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002498 iov[0].iov_base = (char *)req;
2499 /*
2500 * If no input data, the size of ioctl struct in
2501 * protocol spec still includes a 1 byte data buffer,
2502 * but if input data passed to ioctl, we do not
2503 * want to double count this, so we do not send
2504 * the dummy one byte of data in iovec[0] if sending
2505 * input data (in iovec[1]).
2506 */
Steve French4a72daf2013-06-25 00:20:49 -05002507 if (indatalen) {
2508 req->InputCount = cpu_to_le32(indatalen);
2509 /* do not set InputOffset if no input data */
2510 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002511 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002512 rqst->rq_nvec = 2;
2513 iov[0].iov_len = total_len - 1;
Steve French4a72daf2013-06-25 00:20:49 -05002514 iov[1].iov_base = in_data;
2515 iov[1].iov_len = indatalen;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002516 } else {
2517 rqst->rq_nvec = 1;
2518 iov[0].iov_len = total_len;
2519 }
Steve French4a72daf2013-06-25 00:20:49 -05002520
2521 req->OutputOffset = 0;
2522 req->OutputCount = 0; /* MBZ */
2523
2524 /*
Steve French153322f2019-03-28 22:32:49 -05002525 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2526 * We Could increase default MaxOutputResponse, but that could require
2527 * more credits. Windows typically sets this smaller, but for some
Steve French4a72daf2013-06-25 00:20:49 -05002528 * ioctls it may be useful to allow server to send more. No point
2529 * limiting what the server can send as long as fits in one credit
Steve French153322f2019-03-28 22:32:49 -05002530 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2531 * to increase this limit up in the future.
2532 * Note that for snapshot queries that servers like Azure expect that
2533 * the first query be minimal size (and just used to get the number/size
2534 * of previous versions) so response size must be specified as EXACTLY
2535 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2536 * of eight bytes. Currently that is the only case where we set max
2537 * response size smaller.
Steve French4a72daf2013-06-25 00:20:49 -05002538 */
Steve French153322f2019-03-28 22:32:49 -05002539 req->MaxOutputResponse = cpu_to_le32(max_response_size);
Steve French4a72daf2013-06-25 00:20:49 -05002540
2541 if (is_fsctl)
2542 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2543 else
2544 req->Flags = 0;
2545
Steve French4587eee2017-10-25 15:58:31 -05002546 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2547 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002548 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002549
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002550 return 0;
2551}
2552
2553void
2554SMB2_ioctl_free(struct smb_rqst *rqst)
2555{
2556 if (rqst && rqst->rq_iov)
2557 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2558}
2559
Steve French153322f2019-03-28 22:32:49 -05002560
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002561/*
2562 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2563 */
2564int
2565SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2566 u64 volatile_fid, u32 opcode, bool is_fsctl,
Steve French153322f2019-03-28 22:32:49 -05002567 char *in_data, u32 indatalen, u32 max_out_data_len,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002568 char **out_data, u32 *plen /* returned data len */)
2569{
2570 struct smb_rqst rqst;
2571 struct smb2_ioctl_rsp *rsp = NULL;
2572 struct cifs_ses *ses;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002573 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002574 struct kvec rsp_iov = {NULL, 0};
2575 int resp_buftype = CIFS_NO_BUFFER;
2576 int rc = 0;
2577 int flags = 0;
2578
2579 cifs_dbg(FYI, "SMB2 IOCTL\n");
2580
2581 if (out_data != NULL)
2582 *out_data = NULL;
2583
2584 /* zero out returned data len, in case of error */
2585 if (plen)
2586 *plen = 0;
2587
2588 if (tcon)
2589 ses = tcon->ses;
2590 else
2591 return -EIO;
2592
2593 if (!ses || !(ses->server))
2594 return -EIO;
2595
2596 if (smb3_encryption_required(tcon))
2597 flags |= CIFS_TRANSFORM_REQ;
2598
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002599 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002600 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002601 rqst.rq_iov = iov;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002602 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002603
Steve French153322f2019-03-28 22:32:49 -05002604 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2605 is_fsctl, in_data, indatalen, max_out_data_len);
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002606 if (rc)
2607 goto ioctl_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002608
2609 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002610 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002611 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002612
Steve Frencheccb4422018-05-17 21:16:55 -05002613 if (rc != 0)
2614 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2615 ses->Suid, 0, opcode, rc);
2616
Steve French9bf0c9c2013-11-16 18:05:28 -06002617 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002618 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002619 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002620 } else if (rc == -EINVAL) {
2621 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2622 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002623 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002624 goto ioctl_exit;
2625 }
Steve French4a72daf2013-06-25 00:20:49 -05002626 }
2627
2628 /* check if caller wants to look at return data or just return rc */
2629 if ((plen == NULL) || (out_data == NULL))
2630 goto ioctl_exit;
2631
2632 *plen = le32_to_cpu(rsp->OutputCount);
2633
2634 /* We check for obvious errors in the output buffer length and offset */
2635 if (*plen == 0)
2636 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002637 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Steve French4a72daf2013-06-25 00:20:49 -05002638 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2639 *plen = 0;
2640 rc = -EIO;
2641 goto ioctl_exit;
2642 }
2643
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002644 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Steve French4a72daf2013-06-25 00:20:49 -05002645 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2646 le32_to_cpu(rsp->OutputOffset));
2647 *plen = 0;
2648 rc = -EIO;
2649 goto ioctl_exit;
2650 }
2651
YueHaibingd034fee2018-09-10 01:33:06 +00002652 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2653 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05002654 if (*out_data == NULL) {
2655 rc = -ENOMEM;
2656 goto ioctl_exit;
2657 }
2658
Steve French4a72daf2013-06-25 00:20:49 -05002659ioctl_exit:
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002660 SMB2_ioctl_free(&rqst);
Steve French4a72daf2013-06-25 00:20:49 -05002661 free_rsp_buf(resp_buftype, rsp);
2662 return rc;
2663}
2664
Steve French64a5cfa2013-10-14 15:31:32 -05002665/*
2666 * Individual callers to ioctl worker function follow
2667 */
2668
2669int
2670SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2671 u64 persistent_fid, u64 volatile_fid)
2672{
2673 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002674 struct compress_ioctl fsctl_input;
2675 char *ret_data = NULL;
2676
2677 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002678 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002679
2680 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2681 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2682 (char *)&fsctl_input /* data input */,
Steve French153322f2019-03-28 22:32:49 -05002683 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2684 &ret_data /* out data */, NULL);
Steve French64a5cfa2013-10-14 15:31:32 -05002685
2686 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002687
2688 return rc;
2689}
2690
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002691int
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002692SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2693 u64 persistent_fid, u64 volatile_fid)
2694{
2695 struct smb2_close_req *req;
2696 struct kvec *iov = rqst->rq_iov;
2697 unsigned int total_len;
2698 int rc;
2699
2700 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2701 if (rc)
2702 return rc;
2703
2704 req->PersistentFileId = persistent_fid;
2705 req->VolatileFileId = volatile_fid;
2706 iov[0].iov_base = (char *)req;
2707 iov[0].iov_len = total_len;
2708
2709 return 0;
2710}
2711
2712void
2713SMB2_close_free(struct smb_rqst *rqst)
2714{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002715 if (rqst && rqst->rq_iov)
2716 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002717}
2718
2719int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002720SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2721 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002722{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002723 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002724 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002725 struct cifs_ses *ses = tcon->ses;
2726 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002727 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01002728 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002729 int rc = 0;
2730
Joe Perchesf96637b2013-05-04 22:12:25 -05002731 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002732
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002733 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002734 return -EIO;
2735
Steve French5a77e752018-05-09 17:43:08 -05002736 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002737 flags |= CIFS_TRANSFORM_REQ;
2738
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002739 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002740 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002741 rqst.rq_iov = iov;
2742 rqst.rq_nvec = 1;
2743
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002744 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2745 if (rc)
2746 goto close_exit;
2747
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002748 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002749 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002750
2751 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002752 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002753 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2754 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002755 goto close_exit;
2756 }
2757
Steve Frenchfae80442018-10-19 17:14:32 -05002758 atomic_dec(&tcon->num_remote_opens);
2759
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002760 /* BB FIXME - decode close response, update inode for caching */
2761
2762close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002763 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002764 free_rsp_buf(resp_buftype, rsp);
2765 return rc;
2766}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002767
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002768int
2769SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2770 u64 persistent_fid, u64 volatile_fid)
2771{
2772 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2773}
2774
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002775int
2776smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2777 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002778{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002779 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002780 char *end_of_smb = smb_len + (char *)iov->iov_base;
2781 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002782 char *end_of_buf = begin_of_buf + buffer_length;
2783
2784
2785 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002786 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2787 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002788 return -EINVAL;
2789 }
2790
2791 /* check if beyond RFC1001 maximum length */
2792 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002793 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2794 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002795 return -EINVAL;
2796 }
2797
2798 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002799 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002800 return -EINVAL;
2801 }
2802
2803 return 0;
2804}
2805
2806/*
2807 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2808 * Caller must free buffer.
2809 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002810int
2811smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2812 struct kvec *iov, unsigned int minbufsize,
2813 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002814{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002815 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002816 int rc;
2817
2818 if (!data)
2819 return -EINVAL;
2820
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002821 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002822 if (rc)
2823 return rc;
2824
2825 memcpy(data, begin_of_buf, buffer_length);
2826
2827 return 0;
2828}
2829
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002830int
2831SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2832 u64 persistent_fid, u64 volatile_fid,
2833 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002834 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002835{
2836 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002837 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002838 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002839 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002840
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002841 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2842 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002843 if (rc)
2844 return rc;
2845
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002846 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002847 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002848 req->PersistentFileId = persistent_fid;
2849 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002850 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002851
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002852 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002853 if (input_len) {
2854 req->InputBufferLength = cpu_to_le32(input_len);
2855 /* total_len for smb query request never close to le16 max */
2856 req->InputBufferOffset = cpu_to_le16(total_len - 1);
2857 memcpy(req->Buffer, input, input_len);
2858 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002859
2860 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002861 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002862 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002863 return 0;
2864}
2865
2866void
2867SMB2_query_info_free(struct smb_rqst *rqst)
2868{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002869 if (rqst && rqst->rq_iov)
2870 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002871}
2872
2873static int
2874query_info(const unsigned int xid, struct cifs_tcon *tcon,
2875 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2876 u32 additional_info, size_t output_len, size_t min_len, void **data,
2877 u32 *dlen)
2878{
2879 struct smb_rqst rqst;
2880 struct smb2_query_info_rsp *rsp = NULL;
2881 struct kvec iov[1];
2882 struct kvec rsp_iov;
2883 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01002884 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002885 struct cifs_ses *ses = tcon->ses;
2886 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00002887 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002888
2889 cifs_dbg(FYI, "Query Info\n");
2890
2891 if (!ses || !(ses->server))
2892 return -EIO;
2893
2894 if (smb3_encryption_required(tcon))
2895 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002896
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002897 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002898 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002899 rqst.rq_iov = iov;
2900 rqst.rq_nvec = 1;
2901
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002902 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
2903 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002904 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002905 if (rc)
2906 goto qinf_exit;
2907
Steve Frenchd42043a2019-02-26 21:58:30 -06002908 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
2909 ses->Suid, info_class, (__u32)info_type);
2910
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002911 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002912 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002913
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002914 if (rc) {
2915 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002916 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2917 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002918 goto qinf_exit;
2919 }
2920
Steve Frenchd42043a2019-02-26 21:58:30 -06002921 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
2922 ses->Suid, info_class, (__u32)info_type);
2923
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002924 if (dlen) {
2925 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2926 if (!*data) {
2927 *data = kmalloc(*dlen, GFP_KERNEL);
2928 if (!*data) {
2929 cifs_dbg(VFS,
2930 "Error %d allocating memory for acl\n",
2931 rc);
2932 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00002933 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002934 goto qinf_exit;
2935 }
Colin Ian King73aaf922019-01-16 16:28:59 +00002936 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002937 }
2938 }
2939
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002940 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
2941 le32_to_cpu(rsp->OutputBufferLength),
2942 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00002943 if (rc && allocated) {
2944 kfree(*data);
2945 *data = NULL;
2946 *dlen = 0;
2947 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002948
2949qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002950 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002951 free_rsp_buf(resp_buftype, rsp);
2952 return rc;
2953}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002954
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002955int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2956 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002957{
2958 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002959 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002960 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002961 sizeof(struct smb2_file_all_info), (void **)&data,
2962 NULL);
2963}
2964
2965int
2966SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2967 u64 persistent_fid, u64 volatile_fid,
2968 void **data, u32 *plen)
2969{
2970 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2971 *plen = 0;
2972
2973 return query_info(xid, tcon, persistent_fid, volatile_fid,
2974 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05002975 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002976}
2977
2978int
2979SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2980 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2981{
2982 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002983 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002984 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002985 sizeof(struct smb2_file_internal_info),
2986 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002987}
2988
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002989/*
2990 * This is a no-op for now. We're not really interested in the reply, but
2991 * rather in the fact that the server sent one and that server->lstrp
2992 * gets updated.
2993 *
2994 * FIXME: maybe we should consider checking that the reply matches request?
2995 */
2996static void
2997smb2_echo_callback(struct mid_q_entry *mid)
2998{
2999 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003000 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003001 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003002
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003003 if (mid->mid_state == MID_RESPONSE_RECEIVED
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003004 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3005 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3006 credits.instance = server->reconnect_instance;
3007 }
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003008
3009 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003010 add_credits(server, &credits, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003011}
3012
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003013void smb2_reconnect_server(struct work_struct *work)
3014{
3015 struct TCP_Server_Info *server = container_of(work,
3016 struct TCP_Server_Info, reconnect.work);
3017 struct cifs_ses *ses;
3018 struct cifs_tcon *tcon, *tcon2;
3019 struct list_head tmp_list;
3020 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01003021 int rc;
3022 int resched = false;
3023
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003024
3025 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3026 mutex_lock(&server->reconnect_mutex);
3027
3028 INIT_LIST_HEAD(&tmp_list);
3029 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3030
3031 spin_lock(&cifs_tcp_ses_lock);
3032 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3033 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003034 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003035 tcon->tc_count++;
3036 list_add_tail(&tcon->rlist, &tmp_list);
3037 tcon_exist = true;
3038 }
3039 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01003040 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3041 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3042 tcon_exist = true;
3043 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003044 }
3045 /*
3046 * Get the reference to server struct to be sure that the last call of
3047 * cifs_put_tcon() in the loop below won't release the server pointer.
3048 */
3049 if (tcon_exist)
3050 server->srv_count++;
3051
3052 spin_unlock(&cifs_tcp_ses_lock);
3053
3054 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01003055 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3056 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003057 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01003058 else
3059 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003060 list_del_init(&tcon->rlist);
3061 cifs_put_tcon(tcon);
3062 }
3063
3064 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01003065 if (resched)
3066 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003067 mutex_unlock(&server->reconnect_mutex);
3068
3069 /* now we can safely release srv struct */
3070 if (tcon_exist)
3071 cifs_put_tcp_session(server, 1);
3072}
3073
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003074int
3075SMB2_echo(struct TCP_Server_Info *server)
3076{
3077 struct smb2_echo_req *req;
3078 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003079 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003080 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003081 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003082 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003083
Joe Perchesf96637b2013-05-04 22:12:25 -05003084 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003085
Steve French4fcd1812016-06-22 20:12:05 -05003086 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003087 /* No need to send echo on newly established connections */
3088 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3089 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003090 }
3091
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003092 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003093 if (rc)
3094 return rc;
3095
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003096 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003097
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003098 iov[0].iov_len = total_len;
3099 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003100
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003101 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003102 server, CIFS_ECHO_OP, NULL);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003103 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003104 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003105
3106 cifs_small_buf_release(req);
3107 return rc;
3108}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003109
3110int
3111SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3112 u64 volatile_fid)
3113{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003114 struct smb_rqst rqst;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003115 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003116 struct cifs_ses *ses = tcon->ses;
3117 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003118 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003119 int resp_buftype;
3120 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003121 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003122 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003123
Joe Perchesf96637b2013-05-04 22:12:25 -05003124 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003125
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003126 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003127 return -EIO;
3128
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003129 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003130 if (rc)
3131 return rc;
3132
Steve French5a77e752018-05-09 17:43:08 -05003133 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003134 flags |= CIFS_TRANSFORM_REQ;
3135
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003136 req->PersistentFileId = persistent_fid;
3137 req->VolatileFileId = volatile_fid;
3138
3139 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003140 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003141
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003142 memset(&rqst, 0, sizeof(struct smb_rqst));
3143 rqst.rq_iov = iov;
3144 rqst.rq_nvec = 1;
3145
3146 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003147 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003148
Steve Frencheccb4422018-05-17 21:16:55 -05003149 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003150 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003151 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3152 rc);
3153 }
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003154
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003155 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003156 return rc;
3157}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003158
3159/*
3160 * To form a chain of read requests, any read requests after the first should
3161 * have the end_of_chain boolean set to true.
3162 */
3163static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003164smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003165 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3166 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003167{
3168 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003169 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003170 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07003171 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003172
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003173 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3174 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003175 if (rc)
3176 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003177
3178 server = io_parms->tcon->ses->server;
3179 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003180 return -ECONNABORTED;
3181
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003182 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003183 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003184
3185 req->PersistentFileId = io_parms->persistent_fid;
3186 req->VolatileFileId = io_parms->volatile_fid;
3187 req->ReadChannelInfoOffset = 0; /* reserved */
3188 req->ReadChannelInfoLength = 0; /* reserved */
3189 req->Channel = 0; /* reserved */
3190 req->MinimumCount = 0;
3191 req->Length = cpu_to_le32(io_parms->length);
3192 req->Offset = cpu_to_le64(io_parms->offset);
Steve Frenchd323c2462019-02-25 00:52:43 -06003193
3194 trace_smb3_read_enter(0 /* xid */,
3195 io_parms->persistent_fid,
3196 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3197 io_parms->offset, io_parms->length);
Long Libd3dcc62017-11-22 17:38:47 -07003198#ifdef CONFIG_CIFS_SMB_DIRECT
3199 /*
3200 * If we want to do a RDMA write, fill in and append
3201 * smbd_buffer_descriptor_v1 to the end of read request
3202 */
Long Libb4c0412018-04-17 12:17:08 -07003203 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003204 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003205
Long Libd3dcc62017-11-22 17:38:47 -07003206 struct smbd_buffer_descriptor_v1 *v1;
3207 bool need_invalidate =
3208 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3209
3210 rdata->mr = smbd_register_mr(
3211 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003212 rdata->nr_pages, rdata->page_offset,
3213 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003214 if (!rdata->mr)
3215 return -ENOBUFS;
3216
3217 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3218 if (need_invalidate)
3219 req->Channel = SMB2_CHANNEL_RDMA_V1;
3220 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003221 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003222 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003223 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003224 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003225 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3226 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3227 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003228
3229 *total_len += sizeof(*v1) - 1;
3230 }
3231#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003232 if (request_type & CHAINED_REQUEST) {
3233 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003234 /* next 8-byte aligned request */
3235 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3236 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003237 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003238 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003239 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003240 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003241 /*
3242 * Related requests use info from previous read request
3243 * in chain.
3244 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003245 shdr->SessionId = 0xFFFFFFFF;
3246 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003247 req->PersistentFileId = 0xFFFFFFFF;
3248 req->VolatileFileId = 0xFFFFFFFF;
3249 }
3250 }
3251 if (remaining_bytes > io_parms->length)
3252 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3253 else
3254 req->RemainingBytes = 0;
3255
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003256 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003257 return rc;
3258}
3259
3260static void
3261smb2_readv_callback(struct mid_q_entry *mid)
3262{
3263 struct cifs_readdata *rdata = mid->callback_data;
3264 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3265 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003266 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003267 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003268 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003269 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3270 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07003271 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003272 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003273 .rq_npages = rdata->nr_pages,
3274 .rq_pagesz = rdata->pagesz,
3275 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003276
Joe Perchesf96637b2013-05-04 22:12:25 -05003277 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3278 __func__, mid->mid, mid->mid_state, rdata->result,
3279 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003280
3281 switch (mid->mid_state) {
3282 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003283 credits.value = le16_to_cpu(shdr->CreditRequest);
3284 credits.instance = server->reconnect_instance;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003285 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003286 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003287 int rc;
3288
Jeff Layton0b688cf2012-09-18 16:20:34 -07003289 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003290 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003291 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
3292 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003293 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003294 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003295 task_io_account_read(rdata->got_bytes);
3296 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003297 break;
3298 case MID_REQUEST_SUBMITTED:
3299 case MID_RETRY_NEEDED:
3300 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003301 if (server->sign && rdata->got_bytes)
3302 /* reset bytes number since we can not check a sign */
3303 rdata->got_bytes = 0;
3304 /* FIXME: should this be counted toward the initiating task? */
3305 task_io_account_read(rdata->got_bytes);
3306 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003307 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003308 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003309 credits.value = le16_to_cpu(shdr->CreditRequest);
3310 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003311 /* fall through */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003312 default:
Pavel Shilovsky6b15eb18c2019-01-18 15:46:14 -08003313 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003314 }
Long Libd3dcc62017-11-22 17:38:47 -07003315#ifdef CONFIG_CIFS_SMB_DIRECT
3316 /*
3317 * If this rdata has a memmory registered, the MR can be freed
3318 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3319 * because they have limited number and are used for future I/Os
3320 */
3321 if (rdata->mr) {
3322 smbd_deregister_mr(rdata->mr);
3323 rdata->mr = NULL;
3324 }
3325#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08003326 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003327 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003328 trace_smb3_read_err(0 /* xid */,
3329 rdata->cfile->fid.persistent_fid,
3330 tcon->tid, tcon->ses->Suid, rdata->offset,
3331 rdata->bytes, rdata->result);
3332 } else
3333 trace_smb3_read_done(0 /* xid */,
3334 rdata->cfile->fid.persistent_fid,
3335 tcon->tid, tcon->ses->Suid,
3336 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003337
3338 queue_work(cifsiod_wq, &rdata->work);
3339 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003340 add_credits(server, &credits, 0);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003341}
3342
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003343/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003344int
3345smb2_async_readv(struct cifs_readdata *rdata)
3346{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003347 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003348 char *buf;
3349 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003350 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003351 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003352 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003353 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003354 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003355
Joe Perchesf96637b2013-05-04 22:12:25 -05003356 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3357 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003358
3359 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3360 io_parms.offset = rdata->offset;
3361 io_parms.length = rdata->bytes;
3362 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3363 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3364 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003365
3366 server = io_parms.tcon->ses->server;
3367
Long Li2dabfd52017-11-07 01:54:53 -07003368 rc = smb2_new_read_req(
3369 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003370 if (rc)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003371 return rc;
3372
Steve French5a77e752018-05-09 17:43:08 -05003373 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003374 flags |= CIFS_TRANSFORM_REQ;
3375
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003376 rdata->iov[0].iov_base = buf;
3377 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003378
3379 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003380
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003381 if (rdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003382 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003383 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003384 shdr->CreditRequest =
3385 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003386
3387 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3388 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003389 goto async_readv_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003390
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003391 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003392 }
3393
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003394 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07003395 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003396 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003397 smb3_handle_read_data, rdata, flags,
3398 &rdata->credits);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003399 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003400 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003401 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003402 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3403 io_parms.tcon->tid,
3404 io_parms.tcon->ses->Suid,
3405 io_parms.offset, io_parms.length, rc);
3406 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003407
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003408async_readv_out:
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003409 cifs_small_buf_release(buf);
3410 return rc;
3411}
Pavel Shilovsky33319142012-09-18 16:20:29 -07003412
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003413int
3414SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3415 unsigned int *nbytes, char **buf, int *buf_type)
3416{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003417 struct smb_rqst rqst;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003418 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003419 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003420 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003421 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003422 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003423 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003424 int flags = CIFS_LOG_ERROR;
3425 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003426
3427 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07003428 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003429 if (rc)
3430 return rc;
3431
Steve French5a77e752018-05-09 17:43:08 -05003432 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003433 flags |= CIFS_TRANSFORM_REQ;
3434
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003435 iov[0].iov_base = (char *)req;
3436 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003437
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003438 memset(&rqst, 0, sizeof(struct smb_rqst));
3439 rqst.rq_iov = iov;
3440 rqst.rq_nvec = 1;
3441
3442 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003443 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003444
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003445 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003446
3447 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003448 if (rc != -ENODATA) {
3449 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3450 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003451 trace_smb3_read_err(xid, req->PersistentFileId,
3452 io_parms->tcon->tid, ses->Suid,
3453 io_parms->offset, io_parms->length,
3454 rc);
Steve Frenchb0a42f22019-02-25 15:02:58 -06003455 } else
3456 trace_smb3_read_done(xid, req->PersistentFileId,
3457 io_parms->tcon->tid, ses->Suid,
3458 io_parms->offset, 0);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003459 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3460 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05003461 } else
3462 trace_smb3_read_done(xid, req->PersistentFileId,
3463 io_parms->tcon->tid, ses->Suid,
3464 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003465
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003466 *nbytes = le32_to_cpu(rsp->DataLength);
3467 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3468 (*nbytes > io_parms->length)) {
3469 cifs_dbg(FYI, "bad length %d for count %d\n",
3470 *nbytes, io_parms->length);
3471 rc = -EIO;
3472 *nbytes = 0;
3473 }
3474
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003475 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003476 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003477 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003478 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003479 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003480 if (resp_buftype == CIFS_SMALL_BUFFER)
3481 *buf_type = CIFS_SMALL_BUFFER;
3482 else if (resp_buftype == CIFS_LARGE_BUFFER)
3483 *buf_type = CIFS_LARGE_BUFFER;
3484 }
3485 return rc;
3486}
3487
Pavel Shilovsky33319142012-09-18 16:20:29 -07003488/*
3489 * Check the mid_state and signature on received buffer (if any), and queue the
3490 * workqueue completion task.
3491 */
3492static void
3493smb2_writev_callback(struct mid_q_entry *mid)
3494{
3495 struct cifs_writedata *wdata = mid->callback_data;
3496 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003497 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003498 unsigned int written;
3499 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003500 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky33319142012-09-18 16:20:29 -07003501
3502 switch (mid->mid_state) {
3503 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003504 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3505 credits.instance = server->reconnect_instance;
3506 wdata->result = smb2_check_receive(mid, server, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003507 if (wdata->result != 0)
3508 break;
3509
3510 written = le32_to_cpu(rsp->DataLength);
3511 /*
3512 * Mask off high 16 bits when bytes written as returned
3513 * by the server is greater than bytes requested by the
3514 * client. OS/2 servers are known to set incorrect
3515 * CountHigh values.
3516 */
3517 if (written > wdata->bytes)
3518 written &= 0xFFFF;
3519
3520 if (written < wdata->bytes)
3521 wdata->result = -ENOSPC;
3522 else
3523 wdata->bytes = written;
3524 break;
3525 case MID_REQUEST_SUBMITTED:
3526 case MID_RETRY_NEEDED:
3527 wdata->result = -EAGAIN;
3528 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003529 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003530 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3531 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003532 /* fall through */
Pavel Shilovsky33319142012-09-18 16:20:29 -07003533 default:
3534 wdata->result = -EIO;
3535 break;
3536 }
Long Lidb223a52017-11-22 17:38:45 -07003537#ifdef CONFIG_CIFS_SMB_DIRECT
3538 /*
3539 * If this wdata has a memory registered, the MR can be freed
3540 * The number of MRs available is limited, it's important to recover
3541 * used MR as soon as I/O is finished. Hold MR longer in the later
3542 * I/O process can possibly result in I/O deadlock due to lack of MR
3543 * to send request on I/O retry
3544 */
3545 if (wdata->mr) {
3546 smbd_deregister_mr(wdata->mr);
3547 wdata->mr = NULL;
3548 }
3549#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003550 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003551 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003552 trace_smb3_write_err(0 /* no xid */,
3553 wdata->cfile->fid.persistent_fid,
3554 tcon->tid, tcon->ses->Suid, wdata->offset,
3555 wdata->bytes, wdata->result);
3556 } else
3557 trace_smb3_write_done(0 /* no xid */,
3558 wdata->cfile->fid.persistent_fid,
3559 tcon->tid, tcon->ses->Suid,
3560 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003561
3562 queue_work(cifsiod_wq, &wdata->work);
3563 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003564 add_credits(server, &credits, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003565}
3566
3567/* smb2_async_writev - send an async write, and set up mid to handle result */
3568int
Steve French4a5c80d2014-02-07 20:45:12 -06003569smb2_async_writev(struct cifs_writedata *wdata,
3570 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07003571{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003572 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003573 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003574 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003575 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003576 struct TCP_Server_Info *server = tcon->ses->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003577 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003578 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003579 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003580
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003581 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003582 if (rc)
3583 return rc;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003584
Steve French5a77e752018-05-09 17:43:08 -05003585 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003586 flags |= CIFS_TRANSFORM_REQ;
3587
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003588 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003589 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003590
3591 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3592 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3593 req->WriteChannelInfoOffset = 0;
3594 req->WriteChannelInfoLength = 0;
3595 req->Channel = 0;
3596 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003597 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003598 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003599 req->RemainingBytes = 0;
Steve Frenchd323c2462019-02-25 00:52:43 -06003600
3601 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3602 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003603#ifdef CONFIG_CIFS_SMB_DIRECT
3604 /*
3605 * If we want to do a server RDMA read, fill in and append
3606 * smbd_buffer_descriptor_v1 to the end of write request
3607 */
Long Libb4c0412018-04-17 12:17:08 -07003608 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003609 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003610
Long Lidb223a52017-11-22 17:38:45 -07003611 struct smbd_buffer_descriptor_v1 *v1;
3612 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3613
3614 wdata->mr = smbd_register_mr(
3615 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003616 wdata->nr_pages, wdata->page_offset,
3617 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07003618 if (!wdata->mr) {
3619 rc = -ENOBUFS;
3620 goto async_writev_out;
3621 }
3622 req->Length = 0;
3623 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07003624 if (wdata->nr_pages > 1)
3625 req->RemainingBytes =
3626 cpu_to_le32(
3627 (wdata->nr_pages - 1) * wdata->pagesz -
3628 wdata->page_offset + wdata->tailsz
3629 );
3630 else
3631 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003632 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3633 if (need_invalidate)
3634 req->Channel = SMB2_CHANNEL_RDMA_V1;
3635 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003636 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003637 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003638 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003639 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003640 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3641 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3642 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003643 }
3644#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003645 iov[0].iov_len = total_len - 1;
3646 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003647
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003648 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003649 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003650 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07003651 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003652 rqst.rq_npages = wdata->nr_pages;
3653 rqst.rq_pagesz = wdata->pagesz;
3654 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003655#ifdef CONFIG_CIFS_SMB_DIRECT
3656 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003657 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07003658 rqst.rq_npages = 0;
3659 }
3660#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003661 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3662 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003663
Long Lidb223a52017-11-22 17:38:45 -07003664#ifdef CONFIG_CIFS_SMB_DIRECT
3665 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3666 if (!wdata->mr)
3667 req->Length = cpu_to_le32(wdata->bytes);
3668#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003669 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003670#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003671
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003672 if (wdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003673 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003674 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003675 shdr->CreditRequest =
3676 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003677
3678 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3679 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003680 goto async_writev_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003681
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003682 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003683 }
3684
Pavel Shilovsky33319142012-09-18 16:20:29 -07003685 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003686 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003687 wdata, flags, &wdata->credits);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003688
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003689 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003690 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3691 tcon->tid, tcon->ses->Suid, wdata->offset,
3692 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06003693 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003694 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003695 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003696
Pavel Shilovsky33319142012-09-18 16:20:29 -07003697async_writev_out:
3698 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003699 return rc;
3700}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003701
3702/*
3703 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3704 * The length field from io_parms must be at least 1 and indicates a number of
3705 * elements with data to write that begins with position 1 in iov array. All
3706 * data length is specified by count.
3707 */
3708int
3709SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3710 unsigned int *nbytes, struct kvec *iov, int n_vec)
3711{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003712 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003713 int rc = 0;
3714 struct smb2_write_req *req = NULL;
3715 struct smb2_write_rsp *rsp = NULL;
3716 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003717 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003718 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003719 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003720
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003721 *nbytes = 0;
3722
3723 if (n_vec < 1)
3724 return rc;
3725
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003726 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3727 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003728 if (rc)
3729 return rc;
3730
3731 if (io_parms->tcon->ses->server == NULL)
3732 return -ECONNABORTED;
3733
Steve French5a77e752018-05-09 17:43:08 -05003734 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003735 flags |= CIFS_TRANSFORM_REQ;
3736
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003737 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003738
3739 req->PersistentFileId = io_parms->persistent_fid;
3740 req->VolatileFileId = io_parms->volatile_fid;
3741 req->WriteChannelInfoOffset = 0;
3742 req->WriteChannelInfoLength = 0;
3743 req->Channel = 0;
3744 req->Length = cpu_to_le32(io_parms->length);
3745 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003746 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003747 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003748 req->RemainingBytes = 0;
3749
Steve Frenchd323c2462019-02-25 00:52:43 -06003750 trace_smb3_write_enter(xid, io_parms->persistent_fid,
3751 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3752 io_parms->offset, io_parms->length);
3753
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003754 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003755 /* 1 for Buffer */
3756 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003757
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003758 memset(&rqst, 0, sizeof(struct smb_rqst));
3759 rqst.rq_iov = iov;
3760 rqst.rq_nvec = n_vec + 1;
3761
3762 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003763 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003764 cifs_small_buf_release(req);
3765 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003766
3767 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003768 trace_smb3_write_err(xid, req->PersistentFileId,
3769 io_parms->tcon->tid,
3770 io_parms->tcon->ses->Suid,
3771 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003772 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003773 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05003774 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003775 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05003776 trace_smb3_write_done(xid, req->PersistentFileId,
3777 io_parms->tcon->tid,
3778 io_parms->tcon->ses->Suid,
3779 io_parms->offset, *nbytes);
3780 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003781
3782 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003783 return rc;
3784}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003785
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003786static unsigned int
3787num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3788{
3789 int len;
3790 unsigned int entrycount = 0;
3791 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03003792 char *entryptr;
3793 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003794
3795 if (bufstart == NULL)
3796 return 0;
3797
Dan Carpenter56446f22018-09-06 12:48:22 +03003798 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003799
3800 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03003801 if (entryptr + next_offset < entryptr ||
3802 entryptr + next_offset > end_of_buf ||
3803 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003804 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003805 break;
3806 }
3807
Dan Carpenter56446f22018-09-06 12:48:22 +03003808 entryptr = entryptr + next_offset;
3809 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
3810
3811 len = le32_to_cpu(dir_info->FileNameLength);
3812 if (entryptr + len < entryptr ||
3813 entryptr + len > end_of_buf ||
3814 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003815 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3816 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003817 break;
3818 }
3819
Dan Carpenter56446f22018-09-06 12:48:22 +03003820 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003821 entrycount++;
3822
Dan Carpenter56446f22018-09-06 12:48:22 +03003823 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003824 if (!next_offset)
3825 break;
3826 }
3827
3828 return entrycount;
3829}
3830
3831/*
3832 * Readdir/FindFirst
3833 */
3834int
3835SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3836 u64 persistent_fid, u64 volatile_fid, int index,
3837 struct cifs_search_info *srch_inf)
3838{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003839 struct smb_rqst rqst;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003840 struct smb2_query_directory_req *req;
3841 struct smb2_query_directory_rsp *rsp = NULL;
3842 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003843 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003844 int rc = 0;
3845 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003846 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003847 unsigned char *bufptr;
3848 struct TCP_Server_Info *server;
3849 struct cifs_ses *ses = tcon->ses;
3850 __le16 asteriks = cpu_to_le16('*');
3851 char *end_of_smb;
3852 unsigned int output_size = CIFSMaxBufSize;
3853 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003854 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003855 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003856
3857 if (ses && (ses->server))
3858 server = ses->server;
3859 else
3860 return -EIO;
3861
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003862 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3863 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003864 if (rc)
3865 return rc;
3866
Steve French5a77e752018-05-09 17:43:08 -05003867 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003868 flags |= CIFS_TRANSFORM_REQ;
3869
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003870 switch (srch_inf->info_level) {
3871 case SMB_FIND_FILE_DIRECTORY_INFO:
3872 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3873 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3874 break;
3875 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3876 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3877 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3878 break;
3879 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003880 cifs_dbg(VFS, "info level %u isn't supported\n",
3881 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003882 rc = -EINVAL;
3883 goto qdir_exit;
3884 }
3885
3886 req->FileIndex = cpu_to_le32(index);
3887 req->PersistentFileId = persistent_fid;
3888 req->VolatileFileId = volatile_fid;
3889
3890 len = 0x2;
3891 bufptr = req->Buffer;
3892 memcpy(bufptr, &asteriks, len);
3893
3894 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003895 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003896 req->FileNameLength = cpu_to_le16(len);
3897 /*
3898 * BB could be 30 bytes or so longer if we used SMB2 specific
3899 * buffer lengths, but this is safe and close enough.
3900 */
3901 output_size = min_t(unsigned int, output_size, server->maxBuf);
3902 output_size = min_t(unsigned int, output_size, 2 << 15);
3903 req->OutputBufferLength = cpu_to_le32(output_size);
3904
3905 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003906 /* 1 for Buffer */
3907 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003908
3909 iov[1].iov_base = (char *)(req->Buffer);
3910 iov[1].iov_len = len;
3911
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003912 memset(&rqst, 0, sizeof(struct smb_rqst));
3913 rqst.rq_iov = iov;
3914 rqst.rq_nvec = 2;
3915
Steve Frenchd323c2462019-02-25 00:52:43 -06003916 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
3917 tcon->ses->Suid, index, output_size);
3918
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003919 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003920 cifs_small_buf_release(req);
3921 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003922
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003923 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003924 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003925 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Steve Frenchadb3b4e2019-02-25 13:51:11 -06003926 trace_smb3_query_dir_done(xid, persistent_fid,
3927 tcon->tid, tcon->ses->Suid, index, 0);
Pavel Shilovsky52755802014-08-18 20:49:57 +04003928 srch_inf->endOfSearch = true;
3929 rc = 0;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06003930 } else {
3931 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
3932 tcon->ses->Suid, index, 0, rc);
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08003933 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06003934 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003935 goto qdir_exit;
3936 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003937
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003938 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
3939 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
3940 info_buf_size);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06003941 if (rc) {
3942 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
3943 tcon->ses->Suid, index, 0, rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003944 goto qdir_exit;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06003945 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003946
3947 srch_inf->unicode = true;
3948
3949 if (srch_inf->ntwrk_buf_start) {
3950 if (srch_inf->smallBuf)
3951 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3952 else
3953 cifs_buf_release(srch_inf->ntwrk_buf_start);
3954 }
3955 srch_inf->ntwrk_buf_start = (char *)rsp;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003956 srch_inf->srch_entries_start = srch_inf->last_entry =
3957 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
3958 end_of_smb = rsp_iov.iov_len + (char *)rsp;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003959 srch_inf->entries_in_buffer =
3960 num_entries(srch_inf->srch_entries_start, end_of_smb,
3961 &srch_inf->last_entry, info_buf_size);
3962 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003963 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3964 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3965 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003966 if (resp_buftype == CIFS_LARGE_BUFFER)
3967 srch_inf->smallBuf = false;
3968 else if (resp_buftype == CIFS_SMALL_BUFFER)
3969 srch_inf->smallBuf = true;
3970 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003971 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003972
Steve Frenchadb3b4e2019-02-25 13:51:11 -06003973 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
3974 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003975 return rc;
3976
3977qdir_exit:
3978 free_rsp_buf(resp_buftype, rsp);
3979 return rc;
3980}
3981
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003982int
3983SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003984 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003985 u8 info_type, u32 additional_info,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003986 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003987{
3988 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003989 struct kvec *iov = rqst->rq_iov;
3990 unsigned int i, total_len;
3991 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003992
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003993 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003994 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003995 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003996
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003997 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003998 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003999 req->FileInfoClass = info_class;
4000 req->PersistentFileId = persistent_fid;
4001 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004002 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004003
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004004 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004005 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004006 req->BufferLength = cpu_to_le32(*size);
4007
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004008 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004009 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004010
4011 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004012 /* 1 for Buffer */
4013 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004014
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004015 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004016 le32_add_cpu(&req->BufferLength, size[i]);
4017 iov[i].iov_base = (char *)data[i];
4018 iov[i].iov_len = size[i];
4019 }
4020
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004021 return 0;
4022}
4023
4024void
4025SMB2_set_info_free(struct smb_rqst *rqst)
4026{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10004027 if (rqst && rqst->rq_iov)
4028 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004029}
4030
4031static int
4032send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4033 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4034 u8 info_type, u32 additional_info, unsigned int num,
4035 void **data, unsigned int *size)
4036{
4037 struct smb_rqst rqst;
4038 struct smb2_set_info_rsp *rsp = NULL;
4039 struct kvec *iov;
4040 struct kvec rsp_iov;
4041 int rc = 0;
4042 int resp_buftype;
4043 struct cifs_ses *ses = tcon->ses;
4044 int flags = 0;
4045
4046 if (!ses || !(ses->server))
4047 return -EIO;
4048
4049 if (!num)
4050 return -EINVAL;
4051
4052 if (smb3_encryption_required(tcon))
4053 flags |= CIFS_TRANSFORM_REQ;
4054
4055 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4056 if (!iov)
4057 return -ENOMEM;
4058
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004059 memset(&rqst, 0, sizeof(struct smb_rqst));
4060 rqst.rq_iov = iov;
4061 rqst.rq_nvec = num;
4062
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004063 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4064 info_class, info_type, additional_info,
4065 data, size);
4066 if (rc) {
4067 kfree(iov);
4068 return rc;
4069 }
4070
4071
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004072 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004073 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004074 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004075 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004076
Steve Frencheccb4422018-05-17 21:16:55 -05004077 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004078 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004079 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4080 ses->Suid, info_class, (__u32)info_type, rc);
4081 }
Steve French7d3fb242013-11-18 09:56:28 -06004082
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004083 free_rsp_buf(resp_buftype, rsp);
4084 kfree(iov);
4085 return rc;
4086}
4087
4088int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004089SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004090 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004091{
4092 struct smb2_file_eof_info info;
4093 void *data;
4094 unsigned int size;
4095
4096 info.EndOfFile = *eof;
4097
4098 data = &info;
4099 size = sizeof(struct smb2_file_eof_info);
4100
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004101 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004102 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4103 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004104}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004105
4106int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004107SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4108 u64 persistent_fid, u64 volatile_fid,
4109 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4110{
4111 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4112 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4113 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004114}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004115
4116int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004117SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4118 u64 persistent_fid, u64 volatile_fid,
4119 struct smb2_file_full_ea_info *buf, int len)
4120{
4121 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4122 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4123 0, 1, (void **)&buf, &len);
4124}
4125
4126int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004127SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4128 const u64 persistent_fid, const u64 volatile_fid,
4129 __u8 oplock_level)
4130{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004131 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004132 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10004133 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004134 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004135 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004136 unsigned int total_len;
4137 struct kvec iov[1];
4138 struct kvec rsp_iov;
4139 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004140
Joe Perchesf96637b2013-05-04 22:12:25 -05004141 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004142 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4143 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004144 if (rc)
4145 return rc;
4146
Steve French5a77e752018-05-09 17:43:08 -05004147 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004148 flags |= CIFS_TRANSFORM_REQ;
4149
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004150 req->VolatileFid = volatile_fid;
4151 req->PersistentFid = persistent_fid;
4152 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004153 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004154
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004155 flags |= CIFS_NO_RESP;
4156
4157 iov[0].iov_base = (char *)req;
4158 iov[0].iov_len = total_len;
4159
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004160 memset(&rqst, 0, sizeof(struct smb_rqst));
4161 rqst.rq_iov = iov;
4162 rqst.rq_nvec = 1;
4163
4164 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004165 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004166
4167 if (rc) {
4168 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004169 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004170 }
4171
4172 return rc;
4173}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004174
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004175void
4176smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4177 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004178{
4179 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4180 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4181 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05304182 kst->f_bfree = kst->f_bavail =
4183 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004184 return;
4185}
4186
Steve French2d304212018-06-24 23:28:12 -05004187static void
4188copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4189 struct kstatfs *kst)
4190{
4191 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4192 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4193 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4194 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4195 kst->f_bavail = kst->f_bfree;
4196 else
4197 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4198 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4199 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4200 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4201 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4202
4203 return;
4204}
Steve French2d304212018-06-24 23:28:12 -05004205
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004206static int
4207build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4208 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4209{
4210 int rc;
4211 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004212 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004213
Joe Perchesf96637b2013-05-04 22:12:25 -05004214 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004215
4216 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4217 return -EIO;
4218
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004219 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4220 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004221 if (rc)
4222 return rc;
4223
4224 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4225 req->FileInfoClass = level;
4226 req->PersistentFileId = persistent_fid;
4227 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004228 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004229 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004230 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004231 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004232 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004233
4234 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004235 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004236 return 0;
4237}
4238
Steve French2d304212018-06-24 23:28:12 -05004239int
4240SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4241 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4242{
4243 struct smb_rqst rqst;
4244 struct smb2_query_info_rsp *rsp = NULL;
4245 struct kvec iov;
4246 struct kvec rsp_iov;
4247 int rc = 0;
4248 int resp_buftype;
4249 struct cifs_ses *ses = tcon->ses;
4250 FILE_SYSTEM_POSIX_INFO *info = NULL;
4251 int flags = 0;
4252
4253 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4254 sizeof(FILE_SYSTEM_POSIX_INFO),
4255 persistent_fid, volatile_fid);
4256 if (rc)
4257 return rc;
4258
4259 if (smb3_encryption_required(tcon))
4260 flags |= CIFS_TRANSFORM_REQ;
4261
4262 memset(&rqst, 0, sizeof(struct smb_rqst));
4263 rqst.rq_iov = &iov;
4264 rqst.rq_nvec = 1;
4265
4266 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4267 cifs_small_buf_release(iov.iov_base);
4268 if (rc) {
4269 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4270 goto posix_qfsinf_exit;
4271 }
4272 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4273
4274 info = (FILE_SYSTEM_POSIX_INFO *)(
4275 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004276 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4277 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4278 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05004279 if (!rc)
4280 copy_posix_fs_info_to_kstatfs(info, fsdata);
4281
4282posix_qfsinf_exit:
4283 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4284 return rc;
4285}
Steve French2d304212018-06-24 23:28:12 -05004286
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004287int
4288SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4289 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4290{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004291 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004292 struct smb2_query_info_rsp *rsp = NULL;
4293 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004294 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004295 int rc = 0;
4296 int resp_buftype;
4297 struct cifs_ses *ses = tcon->ses;
4298 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004299 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004300
4301 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4302 sizeof(struct smb2_fs_full_size_info),
4303 persistent_fid, volatile_fid);
4304 if (rc)
4305 return rc;
4306
Steve French5a77e752018-05-09 17:43:08 -05004307 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004308 flags |= CIFS_TRANSFORM_REQ;
4309
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004310 memset(&rqst, 0, sizeof(struct smb_rqst));
4311 rqst.rq_iov = &iov;
4312 rqst.rq_nvec = 1;
4313
4314 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004315 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004316 if (rc) {
4317 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05004318 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004319 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004320 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004321
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004322 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004323 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004324 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4325 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4326 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004327 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004328 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004329
Steve French34f62642013-10-09 02:07:00 -05004330qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004331 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004332 return rc;
4333}
4334
4335int
4336SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05004337 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05004338{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004339 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05004340 struct smb2_query_info_rsp *rsp = NULL;
4341 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004342 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05004343 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05004344 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05004345 struct cifs_ses *ses = tcon->ses;
4346 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004347 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05004348
Steven French21671142013-10-09 13:36:35 -05004349 if (level == FS_DEVICE_INFORMATION) {
4350 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4351 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4352 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4353 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4354 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004355 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4356 max_len = sizeof(struct smb3_fs_ss_info);
4357 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05004358 } else if (level == FS_VOLUME_INFORMATION) {
4359 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4360 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05004361 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004362 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05004363 return -EINVAL;
4364 }
4365
4366 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05004367 persistent_fid, volatile_fid);
4368 if (rc)
4369 return rc;
4370
Steve French5a77e752018-05-09 17:43:08 -05004371 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004372 flags |= CIFS_TRANSFORM_REQ;
4373
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004374 memset(&rqst, 0, sizeof(struct smb_rqst));
4375 rqst.rq_iov = &iov;
4376 rqst.rq_nvec = 1;
4377
4378 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004379 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004380 if (rc) {
4381 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4382 goto qfsattr_exit;
4383 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004384 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05004385
4386 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4387 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004388 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05004389 if (rc)
4390 goto qfsattr_exit;
4391
4392 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004393 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004394 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05004395 rsp_len, max_len));
4396 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004397 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004398 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004399 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4400 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004401 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004402 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4403 tcon->perf_sector_size =
4404 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05004405 } else if (level == FS_VOLUME_INFORMATION) {
4406 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4407 (offset + (char *)rsp);
4408 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4409 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004410 }
Steve French34f62642013-10-09 02:07:00 -05004411
4412qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004413 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004414 return rc;
4415}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004416
4417int
4418smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4419 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4420 const __u32 num_lock, struct smb2_lock_element *buf)
4421{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004422 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004423 int rc = 0;
4424 struct smb2_lock_req *req = NULL;
4425 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004426 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004427 int resp_buf_type;
4428 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004429 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004430 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004431
Joe Perchesf96637b2013-05-04 22:12:25 -05004432 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004433
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004434 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004435 if (rc)
4436 return rc;
4437
Steve French5a77e752018-05-09 17:43:08 -05004438 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004439 flags |= CIFS_TRANSFORM_REQ;
4440
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004441 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004442 req->LockCount = cpu_to_le16(num_lock);
4443
4444 req->PersistentFileId = persist_fid;
4445 req->VolatileFileId = volatile_fid;
4446
4447 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004448
4449 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004450 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004451 iov[1].iov_base = (char *)buf;
4452 iov[1].iov_len = count;
4453
4454 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004455
4456 memset(&rqst, 0, sizeof(struct smb_rqst));
4457 rqst.rq_iov = iov;
4458 rqst.rq_nvec = 2;
4459
4460 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004461 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004462 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004463 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004464 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004465 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004466 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4467 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004468 }
4469
4470 return rc;
4471}
4472
4473int
4474SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4475 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4476 const __u64 length, const __u64 offset, const __u32 lock_flags,
4477 const bool wait)
4478{
4479 struct smb2_lock_element lock;
4480
4481 lock.Offset = cpu_to_le64(offset);
4482 lock.Length = cpu_to_le64(length);
4483 lock.Flags = cpu_to_le32(lock_flags);
4484 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4485 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4486
4487 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4488}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004489
4490int
4491SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4492 __u8 *lease_key, const __le32 lease_state)
4493{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004494 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004495 int rc;
4496 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004497 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004498 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004499 unsigned int total_len;
4500 struct kvec iov[1];
4501 struct kvec rsp_iov;
4502 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05004503 __u64 *please_key_high;
4504 __u64 *please_key_low;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004505
Joe Perchesf96637b2013-05-04 22:12:25 -05004506 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004507 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4508 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004509 if (rc)
4510 return rc;
4511
Steve French5a77e752018-05-09 17:43:08 -05004512 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004513 flags |= CIFS_TRANSFORM_REQ;
4514
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004515 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004516 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004517 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004518
4519 memcpy(req->LeaseKey, lease_key, 16);
4520 req->LeaseState = lease_state;
4521
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004522 flags |= CIFS_NO_RESP;
4523
4524 iov[0].iov_base = (char *)req;
4525 iov[0].iov_len = total_len;
4526
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004527 memset(&rqst, 0, sizeof(struct smb_rqst));
4528 rqst.rq_iov = iov;
4529 rqst.rq_nvec = 1;
4530
4531 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004532 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004533
Aurelien Apteld339adc2019-01-31 13:46:07 +01004534 please_key_low = (__u64 *)lease_key;
4535 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004536 if (rc) {
4537 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05004538 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4539 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05004540 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05004541 } else
4542 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4543 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004544
4545 return rc;
4546}