blob: a653c429e8dcaebe98a5fecc70b7d5b41becf3c9 [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020036#include <linux/uuid.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070037#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040038#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070048#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070049#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050050#include "cifs_spnego.h"
Long Lidb223a52017-11-22 17:38:45 -070051#include "smbdirect.h"
Steve Frencheccb4422018-05-17 21:16:55 -050052#include "trace.h"
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -020053#ifdef CONFIG_CIFS_DFS_UPCALL
54#include "dfs_cache.h"
55#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040056
57/*
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
60 *
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
63 */
64static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
67 /* SMB2_LOGOFF */ 4,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
70 /* SMB2_CREATE */ 57,
71 /* SMB2_CLOSE */ 24,
72 /* SMB2_FLUSH */ 24,
73 /* SMB2_READ */ 49,
74 /* SMB2_WRITE */ 49,
75 /* SMB2_LOCK */ 48,
76 /* SMB2_IOCTL */ 57,
77 /* SMB2_CANCEL */ 4,
78 /* SMB2_ECHO */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
84};
85
Ronnie Sahlberg730928c2018-08-08 15:07:49 +100086int smb3_encryption_required(const struct cifs_tcon *tcon)
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070087{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080088 if (!tcon)
89 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070090 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
91 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
92 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080093 if (tcon->seal &&
94 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
95 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070096 return 0;
97}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040098
99static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700100smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400101 const struct cifs_tcon *tcon)
102{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700103 shdr->ProtocolId = SMB2_PROTO_NUMBER;
104 shdr->StructureSize = cpu_to_le16(64);
105 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
108
109 spin_lock(&server->req_lock);
Steve French69dc4b12019-03-05 21:04:56 -0600110 /* Request up to 10 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500111 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700112 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100113 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700114 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500115 min_t(int, server->max_credits -
Steve French69dc4b12019-03-05 21:04:56 -0600116 server->credits, 10));
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100117 spin_unlock(&server->req_lock);
118 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700119 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100120 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400122
123 if (!tcon)
124 goto out;
125
Steve French2b80d042013-06-23 18:43:37 -0500126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500128 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700130 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500131 /* else CreditCharge MBZ */
132
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700133 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400134 /* Uid is not converted */
135 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700136 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500137
138 /*
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
147 */
148/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500150
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
Steve French5a77e752018-05-09 17:43:08 -0500152 !smb3_encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700153 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400154out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400155 return;
156}
157
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200158#ifdef CONFIG_CIFS_DFS_UPCALL
159static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
161{
162 int rc;
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100165 char *tree;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200166 const char *tcp_host;
167 size_t tcp_host_len;
168 const char *dfs_host;
169 size_t dfs_host_len;
170
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
172 if (!tree)
173 return -ENOMEM;
174
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200175 if (tcon->ipc) {
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000176 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
179 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200180 }
181
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
184 goto out;
185 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200186
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
188 if (rc)
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100189 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200190
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
192 &tcp_host_len);
193
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
196 const char *tgt = dfs_cache_get_tgt_name(it);
197
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
199
200 if (dfs_host_len != tcp_host_len
201 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
202 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
203 __func__,
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
206 continue;
207 }
208
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000209 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200210
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
212 if (!rc)
213 break;
214 if (rc == -EREMOTE)
215 break;
216 }
217
218 if (!rc) {
219 if (it)
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
221 it);
222 else
223 rc = -ENOENT;
224 }
225 dfs_cache_free_tgts(&tl);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100226out:
227 kfree(tree);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200228 return rc;
229}
230#else
231static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
233{
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
235}
236#endif
237
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400238static int
239smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
240{
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300241 int rc;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200245 int retries;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400246
247 /*
248 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
249 * check for tcp and smb session status done differently
250 * for those three - in the calling routine.
251 */
252 if (tcon == NULL)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300253 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400254
Pavel Shilovskye99c63e2019-07-22 11:34:59 -0700255 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300256 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400257
258 if (tcon->tidStatus == CifsExiting) {
259 /*
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
263 */
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
268 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400269 return -ENODEV;
270 }
271 }
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
274 return -EIO;
275
276 ses = tcon->ses;
277 server = ses->server;
278
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200279 retries = server->nr_targets;
280
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400281 /*
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200282 * Give demultiplex thread up to 10 seconds to each target available for
283 * reconnect -- should be greater than cifs socket timeout which is 7
284 * seconds.
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400285 */
286 while (server->tcpStatus == CifsNeedReconnect) {
287 /*
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
290 */
291 switch (smb2_command) {
292 /*
293 * BB Should we keep oplock break and add flush to exceptions?
294 */
295 case SMB2_TREE_DISCONNECT:
296 case SMB2_CANCEL:
297 case SMB2_CLOSE:
298 case SMB2_OPLOCK_BREAK:
299 return -EAGAIN;
300 }
301
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
304 10 * HZ);
305 if (rc < 0) {
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
308 return -ERESTARTSYS;
309 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400310
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
313 break;
314
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200315 if (--retries)
316 continue;
317
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400318 /*
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
321 * back on-line
322 */
323 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400325 return -EHOSTDOWN;
326 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200327 retries = server->nr_targets;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400328 }
329
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300331 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400332
333 nls_codepage = load_nls_default();
334
335 /*
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
338 */
339 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200340
341 /*
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
345 */
346 if (server->tcpStatus == CifsNeedReconnect) {
347 rc = -EHOSTDOWN;
348 mutex_unlock(&tcon->ses->session_mutex);
349 goto out;
350 }
351
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400352 rc = cifs_negotiate_protocol(0, tcon->ses);
353 if (!rc && tcon->ses->need_reconnect)
354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
355
356 if (rc || !tcon->need_reconnect) {
357 mutex_unlock(&tcon->ses->session_mutex);
358 goto out;
359 }
360
361 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800362 if (tcon->use_persistent)
363 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500364
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200365 rc = __smb2_reconnect(nls_codepage, tcon);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400366 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500367
Joe Perchesf96637b2013-05-04 22:12:25 -0500368 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500369 if (rc) {
370 /* If sess reconnected but tcon didn't, something strange ... */
371 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400372 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500373 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800374
375 if (smb2_command != SMB2_INTERNAL_CMD)
376 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
377
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400378 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400379out:
380 /*
381 * Check if handle based operation so we know whether we can continue
382 * or not without returning to caller to reset file handle.
383 */
384 /*
385 * BB Is flush done by server on drop of tcp session? Should we special
386 * case it and skip above?
387 */
388 switch (smb2_command) {
389 case SMB2_FLUSH:
390 case SMB2_READ:
391 case SMB2_WRITE:
392 case SMB2_LOCK:
393 case SMB2_IOCTL:
394 case SMB2_QUERY_DIRECTORY:
395 case SMB2_CHANGE_NOTIFY:
396 case SMB2_QUERY_INFO:
397 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800398 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400399 }
400 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400401 return rc;
402}
403
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700404static void
405fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
406 unsigned int *total_len)
407{
408 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
409 /* lookup word count ie StructureSize from table */
410 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
411
412 /*
413 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
414 * largest operations (Create)
415 */
416 memset(buf, 0, 256);
417
418 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
419 spdu->StructureSize2 = cpu_to_le16(parmsize);
420
421 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
422}
423
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100424/*
425 * Allocate and return pointer to an SMB request hdr, and set basic
426 * SMB information in the SMB header. If the return code is zero, this
427 * function must have filled in request_buf pointer.
428 */
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800429static int
430smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
431 void **request_buf, unsigned int *total_len)
432{
433 int rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800434
435 rc = smb2_reconnect(smb2_command, tcon);
436 if (rc)
437 return rc;
438
439 /* BB eventually switch this to SMB2 specific small buf size */
Stefano Briviof46ecbd2018-07-05 11:46:42 +0200440 if (smb2_command == SMB2_SET_INFO)
441 *request_buf = cifs_buf_get();
442 else
443 *request_buf = cifs_small_buf_get();
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800444 if (*request_buf == NULL) {
445 /* BB should we add a retry in here if not a writepage? */
446 return -ENOMEM;
447 }
448
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100449 fill_small_buf(smb2_command, tcon,
450 (struct smb2_sync_hdr *)(*request_buf),
451 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400452
453 if (tcon != NULL) {
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400454 uint16_t com_code = le16_to_cpu(smb2_command);
455 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400456 cifs_stats_inc(&tcon->num_smbs_sent);
457 }
458
459 return rc;
460}
461
Steve Frenchd7bef4c2019-04-18 11:03:58 -0500462/* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500463
464static void
465build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
466{
467 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
468 pneg_ctxt->DataLength = cpu_to_le16(38);
469 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
470 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
471 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
472 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
473}
474
475static void
Steve French26ea8882019-04-26 20:36:08 -0700476build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
477{
478 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
479 pneg_ctxt->DataLength =
480 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
481 - sizeof(struct smb2_neg_context));
482 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
483 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
484 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
485 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
486}
487
488static void
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500489build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
490{
491 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French9ac63ec2019-06-07 08:59:40 -0500492 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
493 pneg_ctxt->CipherCount = cpu_to_le16(2);
494 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
495 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500496}
497
Steve French96d3cca2019-06-25 04:39:51 -0500498static unsigned int
499build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
500{
501 struct nls_table *cp = load_nls_default();
502
503 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
504
505 /* copy up to max of first 100 bytes of server name to NetName field */
506 pneg_ctxt->DataLength = cpu_to_le16(2 +
507 (2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp)));
508 /* context size is DataLength + minimal smb2_neg_context */
509 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
510 sizeof(struct smb2_neg_context), 8) * 8;
511}
512
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500513static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500514build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
515{
516 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
517 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
Steve French0d481322019-02-24 17:56:33 -0600518 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
519 pneg_ctxt->Name[0] = 0x93;
520 pneg_ctxt->Name[1] = 0xAD;
521 pneg_ctxt->Name[2] = 0x25;
522 pneg_ctxt->Name[3] = 0x50;
523 pneg_ctxt->Name[4] = 0x9C;
524 pneg_ctxt->Name[5] = 0xB4;
525 pneg_ctxt->Name[6] = 0x11;
526 pneg_ctxt->Name[7] = 0xE7;
527 pneg_ctxt->Name[8] = 0xB4;
528 pneg_ctxt->Name[9] = 0x23;
529 pneg_ctxt->Name[10] = 0x83;
530 pneg_ctxt->Name[11] = 0xDE;
531 pneg_ctxt->Name[12] = 0x96;
532 pneg_ctxt->Name[13] = 0x8B;
533 pneg_ctxt->Name[14] = 0xCD;
534 pneg_ctxt->Name[15] = 0x7C;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500535}
536
537static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100538assemble_neg_contexts(struct smb2_negotiate_req *req,
Steve French9fe5ff12019-06-24 20:39:04 -0500539 struct TCP_Server_Info *server, unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500540{
Steve Frenchd5c70762019-01-03 02:37:21 -0600541 char *pneg_ctxt = (char *)req;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500542 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500543
Steve Frenchd5c70762019-01-03 02:37:21 -0600544 if (*total_len > 200) {
545 /* In case length corrupted don't want to overrun smb buffer */
546 cifs_dbg(VFS, "Bad frame length assembling neg contexts\n");
547 return;
548 }
549
550 /*
551 * round up total_len of fixed part of SMB3 negotiate request to 8
552 * byte boundary before adding negotiate contexts
553 */
554 *total_len = roundup(*total_len, 8);
555
556 pneg_ctxt = (*total_len) + (char *)req;
557 req->NegotiateContextOffset = cpu_to_le32(*total_len);
558
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500559 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500560 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
561 *total_len += ctxt_len;
562 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100563
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500564 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500565 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
566 *total_len += ctxt_len;
567 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100568
Steve French9fe5ff12019-06-24 20:39:04 -0500569 if (server->compress_algorithm) {
570 build_compression_ctxt((struct smb2_compression_capabilities_context *)
Steve French26ea8882019-04-26 20:36:08 -0700571 pneg_ctxt);
Steve French9fe5ff12019-06-24 20:39:04 -0500572 ctxt_len = DIV_ROUND_UP(
573 sizeof(struct smb2_compression_capabilities_context),
574 8) * 8;
575 *total_len += ctxt_len;
576 pneg_ctxt += ctxt_len;
Steve French96d3cca2019-06-25 04:39:51 -0500577 req->NegotiateContextCount = cpu_to_le16(5);
Steve French9fe5ff12019-06-24 20:39:04 -0500578 } else
Steve French96d3cca2019-06-25 04:39:51 -0500579 req->NegotiateContextCount = cpu_to_le16(4);
580
581 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
582 server->hostname);
583 *total_len += ctxt_len;
584 pneg_ctxt += ctxt_len;
585
Steve Frenchfcef0db2018-05-19 20:45:27 -0500586 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
587 *total_len += sizeof(struct smb2_posix_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500588}
Steve French5100d8a2018-04-09 10:47:14 -0500589
590static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
591{
592 unsigned int len = le16_to_cpu(ctxt->DataLength);
593
594 /* If invalid preauth context warn but use what we requested, SHA-512 */
595 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
596 printk_once(KERN_WARNING "server sent bad preauth context\n");
597 return;
598 }
599 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
600 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
601 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
602 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
603}
604
Steve French26ea8882019-04-26 20:36:08 -0700605static void decode_compress_ctx(struct TCP_Server_Info *server,
606 struct smb2_compression_capabilities_context *ctxt)
607{
608 unsigned int len = le16_to_cpu(ctxt->DataLength);
609
610 /* sizeof compress context is a one element compression capbility struct */
611 if (len < 10) {
612 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
613 return;
614 }
615 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
616 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
617 return;
618 }
619 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
620 printk_once(KERN_WARNING "unknown compression algorithm\n");
621 return;
622 }
623 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
624}
625
Steve French5100d8a2018-04-09 10:47:14 -0500626static int decode_encrypt_ctx(struct TCP_Server_Info *server,
627 struct smb2_encryption_neg_context *ctxt)
628{
629 unsigned int len = le16_to_cpu(ctxt->DataLength);
630
631 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
632 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
633 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
634 return -EINVAL;
635 }
636
637 if (le16_to_cpu(ctxt->CipherCount) != 1) {
638 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
639 return -EINVAL;
640 }
641 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
642 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
643 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
644 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
645 return -EINVAL;
646 }
647 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500648 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500649 return 0;
650}
651
652static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000653 struct TCP_Server_Info *server,
654 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500655{
656 struct smb2_neg_context *pctx;
657 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
658 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500659 unsigned int len_of_ctxts, i;
660 int rc = 0;
661
662 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
663 if (len_of_smb <= offset) {
664 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
665 return -EINVAL;
666 }
667
668 len_of_ctxts = len_of_smb - offset;
669
670 for (i = 0; i < ctxt_cnt; i++) {
671 int clen;
672 /* check that offset is not beyond end of SMB */
673 if (len_of_ctxts == 0)
674 break;
675
676 if (len_of_ctxts < sizeof(struct smb2_neg_context))
677 break;
678
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000679 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500680 clen = le16_to_cpu(pctx->DataLength);
681 if (clen > len_of_ctxts)
682 break;
683
684 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
685 decode_preauth_context(
686 (struct smb2_preauth_neg_context *)pctx);
687 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
688 rc = decode_encrypt_ctx(server,
689 (struct smb2_encryption_neg_context *)pctx);
Steve French26ea8882019-04-26 20:36:08 -0700690 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
691 decode_compress_ctx(server,
692 (struct smb2_compression_capabilities_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500693 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
694 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500695 else
696 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
697 le16_to_cpu(pctx->ContextType));
698
699 if (rc)
700 break;
701 /* offsets must be 8 byte aligned */
702 clen = (clen + 7) & ~0x7;
703 offset += clen + sizeof(struct smb2_neg_context);
704 len_of_ctxts -= clen;
705 }
706 return rc;
707}
708
Steve Frenchce558b02018-05-31 19:16:54 -0500709static struct create_posix *
710create_posix_buf(umode_t mode)
711{
712 struct create_posix *buf;
713
714 buf = kzalloc(sizeof(struct create_posix),
715 GFP_KERNEL);
716 if (!buf)
717 return NULL;
718
719 buf->ccontext.DataOffset =
720 cpu_to_le16(offsetof(struct create_posix, Mode));
721 buf->ccontext.DataLength = cpu_to_le32(4);
722 buf->ccontext.NameOffset =
723 cpu_to_le16(offsetof(struct create_posix, Name));
724 buf->ccontext.NameLength = cpu_to_le16(16);
725
726 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
727 buf->Name[0] = 0x93;
728 buf->Name[1] = 0xAD;
729 buf->Name[2] = 0x25;
730 buf->Name[3] = 0x50;
731 buf->Name[4] = 0x9C;
732 buf->Name[5] = 0xB4;
733 buf->Name[6] = 0x11;
734 buf->Name[7] = 0xE7;
735 buf->Name[8] = 0xB4;
736 buf->Name[9] = 0x23;
737 buf->Name[10] = 0x83;
738 buf->Name[11] = 0xDE;
739 buf->Name[12] = 0x96;
740 buf->Name[13] = 0x8B;
741 buf->Name[14] = 0xCD;
742 buf->Name[15] = 0x7C;
743 buf->Mode = cpu_to_le32(mode);
744 cifs_dbg(FYI, "mode on posix create 0%o", mode);
745 return buf;
746}
747
748static int
749add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
750{
751 struct smb2_create_req *req = iov[0].iov_base;
752 unsigned int num = *num_iovec;
753
754 iov[num].iov_base = create_posix_buf(mode);
755 if (iov[num].iov_base == NULL)
756 return -ENOMEM;
757 iov[num].iov_len = sizeof(struct create_posix);
758 if (!req->CreateContextsOffset)
759 req->CreateContextsOffset = cpu_to_le32(
760 sizeof(struct smb2_create_req) +
761 iov[num - 1].iov_len);
762 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
763 *num_iovec = num + 1;
764 return 0;
765}
766
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500767
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400768/*
769 *
770 * SMB2 Worker functions follow:
771 *
772 * The general structure of the worker functions is:
773 * 1) Call smb2_init (assembles SMB2 header)
774 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
775 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
776 * 4) Decode SMB2 command specific fields in the fixed length area
777 * 5) Decode variable length data area (if any for this SMB2 command type)
778 * 6) Call free smb buffer
779 * 7) return
780 *
781 */
782
783int
784SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
785{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000786 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400787 struct smb2_negotiate_req *req;
788 struct smb2_negotiate_rsp *rsp;
789 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700790 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400791 int rc = 0;
792 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400793 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400794 int blob_offset, blob_length;
795 char *security_blob;
796 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100797 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400798
Joe Perchesf96637b2013-05-04 22:12:25 -0500799 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400800
Jeff Layton3534b852013-05-24 07:41:01 -0400801 if (!server) {
802 WARN(1, "%s: server is NULL!\n", __func__);
803 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400804 }
805
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100806 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400807 if (rc)
808 return rc;
809
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100810 req->sync_hdr.SessionId = 0;
Steve French0fdfef92018-06-28 19:30:23 -0500811
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100812 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
813 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400814
Steve French9764c022017-09-17 10:41:35 -0500815 if (strcmp(ses->server->vals->version_string,
816 SMB3ANY_VERSION_STRING) == 0) {
817 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
818 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
819 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100820 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500821 } else if (strcmp(ses->server->vals->version_string,
822 SMBDEFAULT_VERSION_STRING) == 0) {
823 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
824 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
825 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600826 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
827 req->DialectCount = cpu_to_le16(4);
828 total_len += 8;
Steve French9764c022017-09-17 10:41:35 -0500829 } else {
830 /* otherwise send specific dialect */
831 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
832 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100833 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500834 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400835
836 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400837 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500838 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400839 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500840 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400841 else
842 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400843
Steve Frenche4aa25e2012-10-01 12:26:22 -0500844 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400845
Steve French3c5f9be12014-05-13 13:37:45 -0700846 /* ClientGUID must be zero for SMB2.02 dialect */
847 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
848 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500849 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700850 memcpy(req->ClientGUID, server->client_guid,
851 SMB2_CLIENT_GUID_SIZE);
Steve Frenchd5c70762019-01-03 02:37:21 -0600852 if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
853 (strcmp(ses->server->vals->version_string,
854 SMBDEFAULT_VERSION_STRING) == 0))
Steve French9fe5ff12019-06-24 20:39:04 -0500855 assemble_neg_contexts(req, server, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500856 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400857 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100858 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400859
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000860 memset(&rqst, 0, sizeof(struct smb_rqst));
861 rqst.rq_iov = iov;
862 rqst.rq_nvec = 1;
863
864 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700865 cifs_small_buf_release(req);
866 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400867 /*
868 * No tcon so can't do
869 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
870 */
Steve French7e682f72017-08-31 21:34:24 -0500871 if (rc == -EOPNOTSUPP) {
872 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500873 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500874 " older servers\n");
875 goto neg_exit;
876 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400877 goto neg_exit;
878
Steve French9764c022017-09-17 10:41:35 -0500879 if (strcmp(ses->server->vals->version_string,
880 SMB3ANY_VERSION_STRING) == 0) {
881 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
882 cifs_dbg(VFS,
883 "SMB2 dialect returned but not requested\n");
884 return -EIO;
885 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
886 cifs_dbg(VFS,
887 "SMB2.1 dialect returned but not requested\n");
888 return -EIO;
889 }
890 } else if (strcmp(ses->server->vals->version_string,
891 SMBDEFAULT_VERSION_STRING) == 0) {
892 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
893 cifs_dbg(VFS,
894 "SMB2 dialect returned but not requested\n");
895 return -EIO;
896 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
897 /* ops set to 3.0 by default for default so update */
898 ses->server->ops = &smb21_operations;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800899 ses->server->vals = &smb21_values;
900 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
Steve Frenchd5c70762019-01-03 02:37:21 -0600901 ses->server->ops = &smb311_operations;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800902 ses->server->vals = &smb311_values;
903 }
Steve French590d08d2017-09-19 11:43:47 -0500904 } else if (le16_to_cpu(rsp->DialectRevision) !=
905 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500906 /* if requested single dialect ensure returned dialect matched */
907 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500908 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500909 return -EIO;
910 }
911
Joe Perchesf96637b2013-05-04 22:12:25 -0500912 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400913
Steve Frenche4aa25e2012-10-01 12:26:22 -0500914 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500915 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500916 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500917 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500918 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500919 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500920 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
921 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600922 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
923 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400924 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500925 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500926 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400927 rc = -EIO;
928 goto neg_exit;
929 }
930 server->dialect = le16_to_cpu(rsp->DialectRevision);
931
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100932 /*
933 * Keep a copy of the hash after negprot. This hash will be
934 * the starting hash value for all sessions made from this
935 * server.
936 */
937 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
938 SMB2_PREAUTH_HASH_SIZE);
Steve French0fdfef92018-06-28 19:30:23 -0500939
Jeff Laytone598d1d82013-05-26 07:00:59 -0400940 /* SMB2 only has an extended negflavor */
941 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400942 /* set it to the maximum buffer size value we can send with 1 credit */
943 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
944 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400945 server->max_read = le32_to_cpu(rsp->MaxReadSize);
946 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400947 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500948 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
949 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
950 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400951 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400952 /* Internal types */
953 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400954
955 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000956 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500957 /*
958 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
959 * for us will be
960 * ses->sectype = RawNTLMSSP;
961 * but for time being this is our only auth choice so doesn't matter.
962 * We just found a server which sets blob length to zero expecting raw.
963 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700964 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500965 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700966 server->sec_ntlmssp = true;
967 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700968
Jeff Layton38d77c52013-05-26 07:01:00 -0400969 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400970 if (rc)
971 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500972 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500973 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500974 if (rc == 1)
975 rc = 0;
976 else if (rc == 0)
977 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400978 }
Steve French5100d8a2018-04-09 10:47:14 -0500979
Steve French5100d8a2018-04-09 10:47:14 -0500980 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
981 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000982 rc = smb311_decode_neg_context(rsp, server,
983 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -0500984 else
985 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
986 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400987neg_exit:
988 free_rsp_buf(resp_buftype, rsp);
989 return rc;
990}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400991
Steve Frenchff1c0382013-11-19 23:44:46 -0600992int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
993{
Long Li2796d302018-04-25 11:30:04 -0700994 int rc;
995 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200996 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600997 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500998 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600999
1000 cifs_dbg(FYI, "validate negotiate\n");
1001
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001002 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1003 if (tcon->ses->server->dialect == SMB311_PROT_ID)
1004 return 0;
1005
Steve Frenchff1c0382013-11-19 23:44:46 -06001006 /*
1007 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -05001008 * can not sign it (ie are not known user). Even if signing is not
1009 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -06001010 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -05001011 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -06001012 */
Steve French0603c962017-09-20 19:57:18 -05001013 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -06001014 return 0; /* validation requires signing */
1015
Steve French0603c962017-09-20 19:57:18 -05001016 if (tcon->ses->user_name == NULL) {
1017 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1018 return 0; /* validation requires signing */
1019 }
1020
1021 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1022 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1023
Long Li2796d302018-04-25 11:30:04 -07001024 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1025 if (!pneg_inbuf)
1026 return -ENOMEM;
1027
1028 pneg_inbuf->Capabilities =
Steve Frenchff1c0382013-11-19 23:44:46 -06001029 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Long Li2796d302018-04-25 11:30:04 -07001030 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +01001031 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -06001032
1033 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -07001034 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001035 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1036 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -07001037 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001038 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1039 else
Long Li2796d302018-04-25 11:30:04 -07001040 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001041
Steve French9764c022017-09-17 10:41:35 -05001042
1043 if (strcmp(tcon->ses->server->vals->version_string,
1044 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001045 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1046 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1047 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -05001048 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -07001049 inbuflen = sizeof(*pneg_inbuf) -
Steve Frenchd5c70762019-01-03 02:37:21 -06001050 (2 * sizeof(pneg_inbuf->Dialects[0]));
Steve French9764c022017-09-17 10:41:35 -05001051 } else if (strcmp(tcon->ses->server->vals->version_string,
1052 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001053 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1054 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1055 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -06001056 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1057 pneg_inbuf->DialectCount = cpu_to_le16(4);
Steve French9764c022017-09-17 10:41:35 -05001058 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -07001059 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -05001060 } else {
1061 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -07001062 pneg_inbuf->Dialects[0] =
Steve French9764c022017-09-17 10:41:35 -05001063 cpu_to_le16(tcon->ses->server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -07001064 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -05001065 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -07001066 inbuflen = sizeof(*pneg_inbuf) -
1067 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -05001068 }
Steve Frenchff1c0382013-11-19 23:44:46 -06001069
1070 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1071 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001072 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1073 (char **)&pneg_rsp, &rsplen);
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001074 if (rc == -EOPNOTSUPP) {
1075 /*
1076 * Old Windows versions or Netapp SMB server can return
1077 * not supported error. Client should accept it.
1078 */
1079 cifs_dbg(VFS, "Server does not support validate negotiate\n");
Colin Ian King21078202019-05-17 09:12:33 +01001080 rc = 0;
1081 goto out_free_inbuf;
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001082 } else if (rc != 0) {
Steve Frenchff1c0382013-11-19 23:44:46 -06001083 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -07001084 rc = -EIO;
1085 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -06001086 }
1087
Long Li2796d302018-04-25 11:30:04 -07001088 rc = -EIO;
1089 if (rsplen != sizeof(*pneg_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -05001090 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1091 rsplen);
1092
1093 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -07001094 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1095 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001096 }
1097
1098 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +01001099 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -06001100 goto vneg_out;
1101
1102 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
1103 goto vneg_out;
1104
1105 /* do not validate server guid because not saved at negprot time yet */
1106
1107 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1108 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
1109 goto vneg_out;
1110
1111 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -07001112 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001113 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -07001114 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001115
1116vneg_out:
1117 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -07001118out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001119 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -07001120out_free_inbuf:
1121 kfree(pneg_inbuf);
1122 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -06001123}
1124
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301125enum securityEnum
1126smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1127{
1128 switch (requested) {
1129 case Kerberos:
1130 case RawNTLMSSP:
1131 return requested;
1132 case NTLMv2:
1133 return RawNTLMSSP;
1134 case Unspecified:
1135 if (server->sec_ntlmssp &&
1136 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1137 return RawNTLMSSP;
1138 if ((server->sec_kerberos || server->sec_mskerberos) &&
1139 (global_secflags & CIFSSEC_MAY_KRB5))
1140 return Kerberos;
1141 /* Fallthrough */
1142 default:
1143 return Unspecified;
1144 }
1145}
1146
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001147struct SMB2_sess_data {
1148 unsigned int xid;
1149 struct cifs_ses *ses;
1150 struct nls_table *nls_cp;
1151 void (*func)(struct SMB2_sess_data *);
1152 int result;
1153 u64 previous_session;
1154
1155 /* we will send the SMB in three pieces:
1156 * a fixed length beginning part, an optional
1157 * SPNEGO blob (which can be zero length), and a
1158 * last part which will include the strings
1159 * and rest of bcc area. This allows us to avoid
1160 * a large buffer 17K allocation
1161 */
1162 int buf0_type;
1163 struct kvec iov[2];
1164};
1165
1166static int
1167SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1168{
1169 int rc;
1170 struct cifs_ses *ses = sess_data->ses;
1171 struct smb2_sess_setup_req *req;
1172 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001173 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001174
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001175 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1176 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001177 if (rc)
1178 return rc;
1179
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001180 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001181 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001182
1183 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1184 req->PreviousSessionId = sess_data->previous_session;
1185
1186 req->Flags = 0; /* MBZ */
Steve Frenchd4090142018-06-13 17:05:58 -05001187
1188 /* enough to enable echos and oplocks and one max size write */
1189 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001190
1191 /* only one of SMB2 signing flags may be set in SMB2 request */
1192 if (server->sign)
1193 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1194 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1195 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1196 else
1197 req->SecurityMode = 0;
1198
Steve French8d330962019-07-25 18:13:10 -05001199#ifdef CONFIG_CIFS_DFS_UPCALL
1200 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1201#else
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001202 req->Capabilities = 0;
Steve French8d330962019-07-25 18:13:10 -05001203#endif /* DFS_UPCALL */
1204
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001205 req->Channel = 0; /* MBZ */
1206
1207 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001208 /* 1 for pad */
1209 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001210 /*
1211 * This variable will be used to clear the buffer
1212 * allocated above in case of any error in the calling function.
1213 */
1214 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1215
1216 return 0;
1217}
1218
1219static void
1220SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1221{
1222 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1223 sess_data->buf0_type = CIFS_NO_BUFFER;
1224}
1225
1226static int
1227SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1228{
1229 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001230 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001231 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001232 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001233
1234 /* Testing shows that buffer offset must be at location of Buffer[0] */
1235 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001236 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001237 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1238
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001239 memset(&rqst, 0, sizeof(struct smb_rqst));
1240 rqst.rq_iov = sess_data->iov;
1241 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001242
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001243 /* BB add code to build os and lm fields */
1244 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1245 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001246 &sess_data->buf0_type,
1247 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001248 cifs_small_buf_release(sess_data->iov[0].iov_base);
1249 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001250
1251 return rc;
1252}
1253
1254static int
1255SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1256{
1257 int rc = 0;
1258 struct cifs_ses *ses = sess_data->ses;
1259
1260 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001261 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001262 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001263 if (rc) {
1264 cifs_dbg(FYI,
1265 "SMB3 session key generation failed\n");
1266 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001267 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001268 }
1269 }
1270 if (!ses->server->session_estab) {
1271 ses->server->sequence_number = 0x2;
1272 ses->server->session_estab = true;
1273 }
1274 mutex_unlock(&ses->server->srv_mutex);
1275
1276 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1277 spin_lock(&GlobalMid_Lock);
1278 ses->status = CifsGood;
1279 ses->need_reconnect = false;
1280 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001281 return rc;
1282}
1283
1284#ifdef CONFIG_CIFS_UPCALL
1285static void
1286SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1287{
1288 int rc;
1289 struct cifs_ses *ses = sess_data->ses;
1290 struct cifs_spnego_msg *msg;
1291 struct key *spnego_key = NULL;
1292 struct smb2_sess_setup_rsp *rsp = NULL;
1293
1294 rc = SMB2_sess_alloc_buffer(sess_data);
1295 if (rc)
1296 goto out;
1297
1298 spnego_key = cifs_get_spnego_key(ses);
1299 if (IS_ERR(spnego_key)) {
1300 rc = PTR_ERR(spnego_key);
1301 spnego_key = NULL;
1302 goto out;
1303 }
1304
1305 msg = spnego_key->payload.data[0];
1306 /*
1307 * check version field to make sure that cifs.upcall is
1308 * sending us a response in an expected form
1309 */
1310 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1311 cifs_dbg(VFS,
1312 "bad cifs.upcall version. Expected %d got %d",
1313 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1314 rc = -EKEYREJECTED;
1315 goto out_put_spnego_key;
1316 }
1317
1318 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1319 GFP_KERNEL);
1320 if (!ses->auth_key.response) {
1321 cifs_dbg(VFS,
1322 "Kerberos can't allocate (%u bytes) memory",
1323 msg->sesskey_len);
1324 rc = -ENOMEM;
1325 goto out_put_spnego_key;
1326 }
1327 ses->auth_key.len = msg->sesskey_len;
1328
1329 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1330 sess_data->iov[1].iov_len = msg->secblob_len;
1331
1332 rc = SMB2_sess_sendreceive(sess_data);
1333 if (rc)
1334 goto out_put_spnego_key;
1335
1336 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001337 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001338
1339 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001340
1341 rc = SMB2_sess_establish_session(sess_data);
1342out_put_spnego_key:
1343 key_invalidate(spnego_key);
1344 key_put(spnego_key);
1345out:
1346 sess_data->result = rc;
1347 sess_data->func = NULL;
1348 SMB2_sess_free_buffer(sess_data);
1349}
1350#else
1351static void
1352SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1353{
1354 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1355 sess_data->result = -EOPNOTSUPP;
1356 sess_data->func = NULL;
1357}
1358#endif
1359
Sachin Prabhu166cea42016-10-07 19:11:22 +01001360static void
1361SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1362
1363static void
1364SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1365{
1366 int rc;
1367 struct cifs_ses *ses = sess_data->ses;
1368 struct smb2_sess_setup_rsp *rsp = NULL;
1369 char *ntlmssp_blob = NULL;
1370 bool use_spnego = false; /* else use raw ntlmssp */
1371 u16 blob_length = 0;
1372
1373 /*
1374 * If memory allocation is successful, caller of this function
1375 * frees it.
1376 */
1377 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1378 if (!ses->ntlmssp) {
1379 rc = -ENOMEM;
1380 goto out_err;
1381 }
1382 ses->ntlmssp->sesskey_per_smbsess = true;
1383
1384 rc = SMB2_sess_alloc_buffer(sess_data);
1385 if (rc)
1386 goto out_err;
1387
1388 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1389 GFP_KERNEL);
1390 if (ntlmssp_blob == NULL) {
1391 rc = -ENOMEM;
1392 goto out;
1393 }
1394
1395 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1396 if (use_spnego) {
1397 /* BB eventually need to add this */
1398 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1399 rc = -EOPNOTSUPP;
1400 goto out;
1401 } else {
1402 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1403 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1404 }
1405 sess_data->iov[1].iov_base = ntlmssp_blob;
1406 sess_data->iov[1].iov_len = blob_length;
1407
1408 rc = SMB2_sess_sendreceive(sess_data);
1409 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1410
1411 /* If true, rc here is expected and not an error */
1412 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001413 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001414 rc = 0;
1415
1416 if (rc)
1417 goto out;
1418
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001419 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001420 le16_to_cpu(rsp->SecurityBufferOffset)) {
1421 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1422 le16_to_cpu(rsp->SecurityBufferOffset));
1423 rc = -EIO;
1424 goto out;
1425 }
1426 rc = decode_ntlmssp_challenge(rsp->Buffer,
1427 le16_to_cpu(rsp->SecurityBufferLength), ses);
1428 if (rc)
1429 goto out;
1430
1431 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1432
1433
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001434 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001435 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001436
1437out:
1438 kfree(ntlmssp_blob);
1439 SMB2_sess_free_buffer(sess_data);
1440 if (!rc) {
1441 sess_data->result = 0;
1442 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1443 return;
1444 }
1445out_err:
1446 kfree(ses->ntlmssp);
1447 ses->ntlmssp = NULL;
1448 sess_data->result = rc;
1449 sess_data->func = NULL;
1450}
1451
1452static void
1453SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1454{
1455 int rc;
1456 struct cifs_ses *ses = sess_data->ses;
1457 struct smb2_sess_setup_req *req;
1458 struct smb2_sess_setup_rsp *rsp = NULL;
1459 unsigned char *ntlmssp_blob = NULL;
1460 bool use_spnego = false; /* else use raw ntlmssp */
1461 u16 blob_length = 0;
1462
1463 rc = SMB2_sess_alloc_buffer(sess_data);
1464 if (rc)
1465 goto out;
1466
1467 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001468 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001469
1470 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1471 sess_data->nls_cp);
1472 if (rc) {
1473 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1474 goto out;
1475 }
1476
1477 if (use_spnego) {
1478 /* BB eventually need to add this */
1479 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1480 rc = -EOPNOTSUPP;
1481 goto out;
1482 }
1483 sess_data->iov[1].iov_base = ntlmssp_blob;
1484 sess_data->iov[1].iov_len = blob_length;
1485
1486 rc = SMB2_sess_sendreceive(sess_data);
1487 if (rc)
1488 goto out;
1489
1490 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1491
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001492 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001493 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001494
1495 rc = SMB2_sess_establish_session(sess_data);
1496out:
1497 kfree(ntlmssp_blob);
1498 SMB2_sess_free_buffer(sess_data);
1499 kfree(ses->ntlmssp);
1500 ses->ntlmssp = NULL;
1501 sess_data->result = rc;
1502 sess_data->func = NULL;
1503}
1504
1505static int
1506SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1507{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301508 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001509
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301510 type = smb2_select_sectype(ses->server, ses->sectype);
1511 cifs_dbg(FYI, "sess setup type %d\n", type);
1512 if (type == Unspecified) {
1513 cifs_dbg(VFS,
1514 "Unable to select appropriate authentication method!");
1515 return -EINVAL;
1516 }
1517
1518 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001519 case Kerberos:
1520 sess_data->func = SMB2_auth_kerberos;
1521 break;
1522 case RawNTLMSSP:
1523 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1524 break;
1525 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301526 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001527 return -EOPNOTSUPP;
1528 }
1529
1530 return 0;
1531}
1532
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001533int
1534SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1535 const struct nls_table *nls_cp)
1536{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001537 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001538 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001539 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001540
Joe Perchesf96637b2013-05-04 22:12:25 -05001541 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001542
Jeff Layton3534b852013-05-24 07:41:01 -04001543 if (!server) {
1544 WARN(1, "%s: server is NULL!\n", __func__);
1545 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001546 }
1547
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001548 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1549 if (!sess_data)
1550 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001551
1552 rc = SMB2_select_sec(ses, sess_data);
1553 if (rc)
1554 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001555 sess_data->xid = xid;
1556 sess_data->ses = ses;
1557 sess_data->buf0_type = CIFS_NO_BUFFER;
1558 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001559 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001560
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001561 /*
1562 * Initialize the session hash with the server one.
1563 */
1564 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1565 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001566
Sachin Prabhu166cea42016-10-07 19:11:22 +01001567 while (sess_data->func)
1568 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001569
Steve Frenchc721c382017-09-19 18:40:03 -05001570 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1571 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001572 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001573out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001574 kfree(sess_data);
1575 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001576}
1577
1578int
1579SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1580{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001581 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001582 struct smb2_logoff_req *req; /* response is also trivial struct */
1583 int rc = 0;
1584 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001585 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001586 unsigned int total_len;
1587 struct kvec iov[1];
1588 struct kvec rsp_iov;
1589 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001590
Joe Perchesf96637b2013-05-04 22:12:25 -05001591 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001592
1593 if (ses && (ses->server))
1594 server = ses->server;
1595 else
1596 return -EIO;
1597
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001598 /* no need to send SMB logoff if uid already closed due to reconnect */
1599 if (ses->need_reconnect)
1600 goto smb2_session_already_dead;
1601
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001602 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001603 if (rc)
1604 return rc;
1605
1606 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001607 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001608
1609 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1610 flags |= CIFS_TRANSFORM_REQ;
1611 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001612 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001613
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001614 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001615
1616 iov[0].iov_base = (char *)req;
1617 iov[0].iov_len = total_len;
1618
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001619 memset(&rqst, 0, sizeof(struct smb_rqst));
1620 rqst.rq_iov = iov;
1621 rqst.rq_nvec = 1;
1622
1623 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001624 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001625 /*
1626 * No tcon so can't do
1627 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1628 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001629
1630smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001631 return rc;
1632}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001633
1634static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1635{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001636 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001637}
1638
1639#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1640
Steve Frenchde9f68df2013-11-15 11:26:24 -06001641/* These are similar values to what Windows uses */
1642static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1643{
1644 tcon->max_chunks = 256;
1645 tcon->max_bytes_chunk = 1048576;
1646 tcon->max_bytes_copy = 16777216;
1647}
1648
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001649int
1650SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1651 struct cifs_tcon *tcon, const struct nls_table *cp)
1652{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001653 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001654 struct smb2_tree_connect_req *req;
1655 struct smb2_tree_connect_rsp *rsp = NULL;
1656 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001657 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001658 int rc = 0;
1659 int resp_buftype;
1660 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001661 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001662 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001663 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001664
Joe Perchesf96637b2013-05-04 22:12:25 -05001665 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001666
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001667 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001668 return -EIO;
1669
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001670 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1671 if (unc_path == NULL)
1672 return -ENOMEM;
1673
1674 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1675 unc_path_len *= 2;
1676 if (unc_path_len < 2) {
1677 kfree(unc_path);
1678 return -EINVAL;
1679 }
1680
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001681 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001682 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001683 atomic_set(&tcon->num_remote_opens, 0);
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001684 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1685 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001686 if (rc) {
1687 kfree(unc_path);
1688 return rc;
1689 }
1690
Steve French5a77e752018-05-09 17:43:08 -05001691 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001692 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001693
1694 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001695 /* 1 for pad */
1696 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001697
1698 /* Testing shows that buffer offset must be at location of Buffer[0] */
1699 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001700 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001701 req->PathLength = cpu_to_le16(unc_path_len - 2);
1702 iov[1].iov_base = unc_path;
1703 iov[1].iov_len = unc_path_len;
1704
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001705 /*
1706 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1707 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
Steve French8c11a602019-03-22 22:31:17 -05001708 * (Samba servers don't always set the flag so also check if null user)
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001709 */
Steve French6188f282018-03-13 02:29:36 -05001710 if ((ses->server->dialect == SMB311_PROT_ID) &&
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001711 !smb3_encryption_required(tcon) &&
Steve French8c11a602019-03-22 22:31:17 -05001712 !(ses->session_flags &
1713 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1714 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
Steve French6188f282018-03-13 02:29:36 -05001715 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1716
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001717 memset(&rqst, 0, sizeof(struct smb_rqst));
1718 rqst.rq_iov = iov;
1719 rqst.rq_nvec = 2;
1720
Steve French4fe75c42019-02-14 01:19:02 -06001721 /* Need 64 for max size write so ask for more in case not there yet */
1722 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1723
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001724 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001725 cifs_small_buf_release(req);
1726 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001727 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001728 if (rc != 0) {
1729 if (tcon) {
1730 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1731 tcon->need_reconnect = true;
1732 }
1733 goto tcon_error_exit;
1734 }
1735
Christophe JAILLETcd123002017-05-12 17:59:32 +02001736 switch (rsp->ShareType) {
1737 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001738 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001739 break;
1740 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001741 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001742 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001743 break;
1744 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001745 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001746 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001747 break;
1748 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001749 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001750 rc = -EOPNOTSUPP;
1751 goto tcon_error_exit;
1752 }
1753
1754 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001755 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001756 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1757 tcon->tidStatus = CifsGood;
1758 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001759 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001760 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001761
1762 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1763 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001764 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001765
1766 if (tcon->seal &&
1767 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1768 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1769
Steve Frenchde9f68df2013-11-15 11:26:24 -06001770 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001771 if (tcon->ses->server->ops->validate_negotiate)
1772 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001773tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001774
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001775 free_rsp_buf(resp_buftype, rsp);
1776 kfree(unc_path);
1777 return rc;
1778
1779tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001780 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001781 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001782 }
1783 goto tcon_exit;
1784}
1785
1786int
1787SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1788{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001789 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001790 struct smb2_tree_disconnect_req *req; /* response is trivial */
1791 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001792 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001793 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001794 unsigned int total_len;
1795 struct kvec iov[1];
1796 struct kvec rsp_iov;
1797 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001798
Joe Perchesf96637b2013-05-04 22:12:25 -05001799 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001800
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001801 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001802 return -EIO;
1803
1804 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1805 return 0;
1806
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001807 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1808 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001809 if (rc)
1810 return rc;
1811
Steve French5a77e752018-05-09 17:43:08 -05001812 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001813 flags |= CIFS_TRANSFORM_REQ;
1814
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001815 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001816
1817 iov[0].iov_base = (char *)req;
1818 iov[0].iov_len = total_len;
1819
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001820 memset(&rqst, 0, sizeof(struct smb_rqst));
1821 rqst.rq_iov = iov;
1822 rqst.rq_nvec = 1;
1823
1824 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001825 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001826 if (rc)
1827 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1828
1829 return rc;
1830}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001831
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001832
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001833static struct create_durable *
1834create_durable_buf(void)
1835{
1836 struct create_durable *buf;
1837
1838 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1839 if (!buf)
1840 return NULL;
1841
1842 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001843 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001844 buf->ccontext.DataLength = cpu_to_le32(16);
1845 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1846 (struct create_durable, Name));
1847 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001848 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001849 buf->Name[0] = 'D';
1850 buf->Name[1] = 'H';
1851 buf->Name[2] = 'n';
1852 buf->Name[3] = 'Q';
1853 return buf;
1854}
1855
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001856static struct create_durable *
1857create_reconnect_durable_buf(struct cifs_fid *fid)
1858{
1859 struct create_durable *buf;
1860
1861 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1862 if (!buf)
1863 return NULL;
1864
1865 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1866 (struct create_durable, Data));
1867 buf->ccontext.DataLength = cpu_to_le32(16);
1868 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1869 (struct create_durable, Name));
1870 buf->ccontext.NameLength = cpu_to_le16(4);
1871 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1872 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001873 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001874 buf->Name[0] = 'D';
1875 buf->Name[1] = 'H';
1876 buf->Name[2] = 'n';
1877 buf->Name[3] = 'C';
1878 return buf;
1879}
1880
Steve French89a5bfa2019-07-18 17:22:18 -05001881static void
1882parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1883{
1884 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1885
1886 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1887 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1888 buf->IndexNumber = pdisk_id->DiskFileId;
1889}
1890
1891void
1892smb2_parse_contexts(struct TCP_Server_Info *server,
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +10001893 struct smb2_create_rsp *rsp,
Steve French89a5bfa2019-07-18 17:22:18 -05001894 unsigned int *epoch, char *lease_key, __u8 *oplock,
1895 struct smb2_file_all_info *buf)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001896{
1897 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001898 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001899 unsigned int next;
1900 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001901 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001902
Steve French89a5bfa2019-07-18 17:22:18 -05001903 *oplock = 0;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001904 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001905 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001906 cc = (struct create_context *)data_offset;
Steve French89a5bfa2019-07-18 17:22:18 -05001907
1908 /* Initialize inode number to 0 in case no valid data in qfid context */
1909 if (buf)
1910 buf->IndexNumber = 0;
1911
Justin Maggarddeb7def2016-02-09 15:52:08 -08001912 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001913 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001914 if (le16_to_cpu(cc->NameLength) == 4 &&
Steve French89a5bfa2019-07-18 17:22:18 -05001915 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1916 *oplock = server->ops->parse_lease_buf(cc, epoch,
1917 lease_key);
1918 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1919 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1920 parse_query_id_ctxt(cc, buf);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001921
1922 next = le32_to_cpu(cc->Next);
1923 if (!next)
1924 break;
1925 remaining -= next;
1926 cc = (struct create_context *)((char *)cc + next);
1927 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001928
Steve French89a5bfa2019-07-18 17:22:18 -05001929 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1930 *oplock = rsp->OplockLevel;
1931
1932 return;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001933}
1934
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001935static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001936add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02001937 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001938{
1939 struct smb2_create_req *req = iov[0].iov_base;
1940 unsigned int num = *num_iovec;
1941
Stefano Brivio729c0c92018-07-05 15:10:02 +02001942 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001943 if (iov[num].iov_base == NULL)
1944 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001945 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001946 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1947 if (!req->CreateContextsOffset)
1948 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001949 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001950 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001951 le32_add_cpu(&req->CreateContextsLength,
1952 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001953 *num_iovec = num + 1;
1954 return 0;
1955}
1956
Steve Frenchb56eae42015-11-03 09:26:27 -06001957static struct create_durable_v2 *
Steve Frenchca567eb2019-03-29 16:31:07 -05001958create_durable_v2_buf(struct cifs_open_parms *oparms)
Steve Frenchb56eae42015-11-03 09:26:27 -06001959{
Steve Frenchca567eb2019-03-29 16:31:07 -05001960 struct cifs_fid *pfid = oparms->fid;
Steve Frenchb56eae42015-11-03 09:26:27 -06001961 struct create_durable_v2 *buf;
1962
1963 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1964 if (!buf)
1965 return NULL;
1966
1967 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1968 (struct create_durable_v2, dcontext));
1969 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1970 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1971 (struct create_durable_v2, Name));
1972 buf->ccontext.NameLength = cpu_to_le16(4);
1973
Steve Frenchca567eb2019-03-29 16:31:07 -05001974 /*
1975 * NB: Handle timeout defaults to 0, which allows server to choose
1976 * (most servers default to 120 seconds) and most clients default to 0.
1977 * This can be overridden at mount ("handletimeout=") if the user wants
1978 * a different persistent (or resilient) handle timeout for all opens
1979 * opens on a particular SMB3 mount.
1980 */
1981 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
Steve Frenchb56eae42015-11-03 09:26:27 -06001982 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001983 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001984 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1985
1986 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1987 buf->Name[0] = 'D';
1988 buf->Name[1] = 'H';
1989 buf->Name[2] = '2';
1990 buf->Name[3] = 'Q';
1991 return buf;
1992}
1993
1994static struct create_durable_handle_reconnect_v2 *
1995create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1996{
1997 struct create_durable_handle_reconnect_v2 *buf;
1998
1999 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2000 GFP_KERNEL);
2001 if (!buf)
2002 return NULL;
2003
2004 buf->ccontext.DataOffset =
2005 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2006 dcontext));
2007 buf->ccontext.DataLength =
2008 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2009 buf->ccontext.NameOffset =
2010 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2011 Name));
2012 buf->ccontext.NameLength = cpu_to_le16(4);
2013
2014 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2015 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2016 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2017 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2018
2019 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2020 buf->Name[0] = 'D';
2021 buf->Name[1] = 'H';
2022 buf->Name[2] = '2';
2023 buf->Name[3] = 'C';
2024 return buf;
2025}
2026
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002027static int
Steve Frenchb56eae42015-11-03 09:26:27 -06002028add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002029 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002030{
2031 struct smb2_create_req *req = iov[0].iov_base;
2032 unsigned int num = *num_iovec;
2033
Steve Frenchca567eb2019-03-29 16:31:07 -05002034 iov[num].iov_base = create_durable_v2_buf(oparms);
Steve Frenchb56eae42015-11-03 09:26:27 -06002035 if (iov[num].iov_base == NULL)
2036 return -ENOMEM;
2037 iov[num].iov_len = sizeof(struct create_durable_v2);
2038 if (!req->CreateContextsOffset)
2039 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002040 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002041 iov[1].iov_len);
2042 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002043 *num_iovec = num + 1;
2044 return 0;
2045}
2046
2047static int
2048add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2049 struct cifs_open_parms *oparms)
2050{
2051 struct smb2_create_req *req = iov[0].iov_base;
2052 unsigned int num = *num_iovec;
2053
2054 /* indicate that we don't need to relock the file */
2055 oparms->reconnect = false;
2056
2057 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2058 if (iov[num].iov_base == NULL)
2059 return -ENOMEM;
2060 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2061 if (!req->CreateContextsOffset)
2062 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002063 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002064 iov[1].iov_len);
2065 le32_add_cpu(&req->CreateContextsLength,
2066 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002067 *num_iovec = num + 1;
2068 return 0;
2069}
2070
2071static int
2072add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2073 struct cifs_open_parms *oparms, bool use_persistent)
2074{
2075 struct smb2_create_req *req = iov[0].iov_base;
2076 unsigned int num = *num_iovec;
2077
2078 if (use_persistent) {
2079 if (oparms->reconnect)
2080 return add_durable_reconnect_v2_context(iov, num_iovec,
2081 oparms);
2082 else
2083 return add_durable_v2_context(iov, num_iovec, oparms);
2084 }
2085
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002086 if (oparms->reconnect) {
2087 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2088 /* indicate that we don't need to relock the file */
2089 oparms->reconnect = false;
2090 } else
2091 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002092 if (iov[num].iov_base == NULL)
2093 return -ENOMEM;
2094 iov[num].iov_len = sizeof(struct create_durable);
2095 if (!req->CreateContextsOffset)
2096 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002097 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002098 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08002099 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002100 *num_iovec = num + 1;
2101 return 0;
2102}
2103
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002104/* See MS-SMB2 2.2.13.2.7 */
2105static struct crt_twarp_ctxt *
2106create_twarp_buf(__u64 timewarp)
2107{
2108 struct crt_twarp_ctxt *buf;
2109
2110 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2111 if (!buf)
2112 return NULL;
2113
2114 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2115 (struct crt_twarp_ctxt, Timestamp));
2116 buf->ccontext.DataLength = cpu_to_le32(8);
2117 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2118 (struct crt_twarp_ctxt, Name));
2119 buf->ccontext.NameLength = cpu_to_le16(4);
2120 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2121 buf->Name[0] = 'T';
2122 buf->Name[1] = 'W';
2123 buf->Name[2] = 'r';
2124 buf->Name[3] = 'p';
2125 buf->Timestamp = cpu_to_le64(timewarp);
2126 return buf;
2127}
2128
2129/* See MS-SMB2 2.2.13.2.7 */
2130static int
2131add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2132{
2133 struct smb2_create_req *req = iov[0].iov_base;
2134 unsigned int num = *num_iovec;
2135
2136 iov[num].iov_base = create_twarp_buf(timewarp);
2137 if (iov[num].iov_base == NULL)
2138 return -ENOMEM;
2139 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2140 if (!req->CreateContextsOffset)
2141 req->CreateContextsOffset = cpu_to_le32(
2142 sizeof(struct smb2_create_req) +
2143 iov[num - 1].iov_len);
2144 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2145 *num_iovec = num + 1;
2146 return 0;
2147}
2148
Steve Frenchff2a09e2019-07-06 14:41:38 -05002149static struct crt_query_id_ctxt *
2150create_query_id_buf(void)
2151{
2152 struct crt_query_id_ctxt *buf;
2153
2154 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2155 if (!buf)
2156 return NULL;
2157
2158 buf->ccontext.DataOffset = cpu_to_le16(0);
2159 buf->ccontext.DataLength = cpu_to_le32(0);
2160 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2161 (struct crt_query_id_ctxt, Name));
2162 buf->ccontext.NameLength = cpu_to_le16(4);
2163 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2164 buf->Name[0] = 'Q';
2165 buf->Name[1] = 'F';
2166 buf->Name[2] = 'i';
2167 buf->Name[3] = 'd';
2168 return buf;
2169}
2170
2171/* See MS-SMB2 2.2.13.2.9 */
2172static int
2173add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2174{
2175 struct smb2_create_req *req = iov[0].iov_base;
2176 unsigned int num = *num_iovec;
2177
2178 iov[num].iov_base = create_query_id_buf();
2179 if (iov[num].iov_base == NULL)
2180 return -ENOMEM;
2181 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2182 if (!req->CreateContextsOffset)
2183 req->CreateContextsOffset = cpu_to_le32(
2184 sizeof(struct smb2_create_req) +
2185 iov[num - 1].iov_len);
2186 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2187 *num_iovec = num + 1;
2188 return 0;
2189}
2190
Aurelien Aptelf0712922017-02-22 14:47:17 +01002191static int
2192alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2193 const char *treename, const __le16 *path)
2194{
2195 int treename_len, path_len;
2196 struct nls_table *cp;
2197 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2198
2199 /*
2200 * skip leading "\\"
2201 */
2202 treename_len = strlen(treename);
2203 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2204 return -EINVAL;
2205
2206 treename += 2;
2207 treename_len -= 2;
2208
2209 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2210
2211 /*
2212 * make room for one path separator between the treename and
2213 * path
2214 */
2215 *out_len = treename_len + 1 + path_len;
2216
2217 /*
2218 * final path needs to be null-terminated UTF16 with a
2219 * size aligned to 8
2220 */
2221
2222 *out_size = roundup((*out_len+1)*2, 8);
2223 *out_path = kzalloc(*out_size, GFP_KERNEL);
2224 if (!*out_path)
2225 return -ENOMEM;
2226
2227 cp = load_nls_default();
2228 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2229 UniStrcat(*out_path, sep);
2230 UniStrcat(*out_path, path);
2231 unload_nls(cp);
2232
2233 return 0;
2234}
2235
Steve Frenchbea851b2018-06-14 21:56:32 -05002236int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2237 umode_t mode, struct cifs_tcon *tcon,
2238 const char *full_path,
2239 struct cifs_sb_info *cifs_sb)
2240{
2241 struct smb_rqst rqst;
2242 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002243 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002244 struct cifs_ses *ses = tcon->ses;
2245 struct kvec iov[3]; /* make sure at least one for each open context */
2246 struct kvec rsp_iov = {NULL, 0};
2247 int resp_buftype;
2248 int uni_path_len;
2249 __le16 *copy_path = NULL;
2250 int copy_size;
2251 int rc = 0;
2252 unsigned int n_iov = 2;
2253 __u32 file_attributes = 0;
2254 char *pc_buf = NULL;
2255 int flags = 0;
2256 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002257 __le16 *utf16_path = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002258
2259 cifs_dbg(FYI, "mkdir\n");
2260
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002261 /* resource #1: path allocation */
2262 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2263 if (!utf16_path)
2264 return -ENOMEM;
2265
YueHaibing29cbfa12018-12-18 02:51:51 +00002266 if (!ses || !(ses->server)) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002267 rc = -EIO;
2268 goto err_free_path;
2269 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002270
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002271 /* resource #2: request */
Steve Frenchbea851b2018-06-14 21:56:32 -05002272 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002273 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002274 goto err_free_path;
2275
Steve Frenchbea851b2018-06-14 21:56:32 -05002276
2277 if (smb3_encryption_required(tcon))
2278 flags |= CIFS_TRANSFORM_REQ;
2279
Steve Frenchbea851b2018-06-14 21:56:32 -05002280 req->ImpersonationLevel = IL_IMPERSONATION;
2281 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2282 /* File attributes ignored on open (used in create though) */
2283 req->FileAttributes = cpu_to_le32(file_attributes);
2284 req->ShareAccess = FILE_SHARE_ALL_LE;
2285 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2286 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2287
2288 iov[0].iov_base = (char *)req;
2289 /* -1 since last byte is buf[0] which is sent below (path) */
2290 iov[0].iov_len = total_len - 1;
2291
2292 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2293
2294 /* [MS-SMB2] 2.2.13 NameOffset:
2295 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2296 * the SMB2 header, the file name includes a prefix that will
2297 * be processed during DFS name normalization as specified in
2298 * section 3.3.5.9. Otherwise, the file name is relative to
2299 * the share that is identified by the TreeId in the SMB2
2300 * header.
2301 */
2302 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2303 int name_len;
2304
2305 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2306 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2307 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002308 tcon->treeName, utf16_path);
2309 if (rc)
2310 goto err_free_req;
2311
Steve Frenchbea851b2018-06-14 21:56:32 -05002312 req->NameLength = cpu_to_le16(name_len * 2);
2313 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002314 /* free before overwriting resource */
2315 kfree(utf16_path);
2316 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002317 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002318 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002319 /* MUST set path len (NameLength) to 0 opening root of share */
2320 req->NameLength = cpu_to_le16(uni_path_len - 2);
2321 if (uni_path_len % 8 != 0) {
2322 copy_size = roundup(uni_path_len, 8);
2323 copy_path = kzalloc(copy_size, GFP_KERNEL);
2324 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002325 rc = -ENOMEM;
2326 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002327 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002328 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002329 uni_path_len);
2330 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002331 /* free before overwriting resource */
2332 kfree(utf16_path);
2333 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002334 }
2335 }
2336
2337 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002338 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002339 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2340
2341 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002342 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002343 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002344 if (rc)
2345 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002346 pc_buf = iov[n_iov-1].iov_base;
2347 }
2348
2349
2350 memset(&rqst, 0, sizeof(struct smb_rqst));
2351 rqst.rq_iov = iov;
2352 rqst.rq_nvec = n_iov;
2353
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002354 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2355 FILE_WRITE_ATTRIBUTES);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002356 /* resource #4: response buffer */
2357 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2358 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002359 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2360 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002361 CREATE_NOT_FILE,
2362 FILE_WRITE_ATTRIBUTES, rc);
2363 goto err_free_rsp_buf;
2364 }
2365
2366 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2367 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2368 ses->Suid, CREATE_NOT_FILE,
2369 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002370
2371 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2372
2373 /* Eventually save off posix specific response info and timestaps */
2374
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002375err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002376 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002377 kfree(pc_buf);
2378err_free_req:
2379 cifs_small_buf_release(req);
2380err_free_path:
2381 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002382 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002383}
Steve Frenchbea851b2018-06-14 21:56:32 -05002384
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002385int
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002386SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2387 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002388{
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002389 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002390 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002391 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002392 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002393 int copy_size;
2394 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002395 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002396 struct kvec *iov = rqst->rq_iov;
2397 __le16 *copy_path;
2398 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002399
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002400 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002401 if (rc)
2402 return rc;
2403
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002404 iov[0].iov_base = (char *)req;
2405 /* -1 since last byte is buf[0] which is sent below (path) */
2406 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002407
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002408 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002409 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002410 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2411 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002412
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002413 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002414 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002415 /* File attributes ignored on open (used in create though) */
2416 req->FileAttributes = cpu_to_le32(file_attributes);
2417 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002418 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2419 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002420 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002421
2422 /* [MS-SMB2] 2.2.13 NameOffset:
2423 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2424 * the SMB2 header, the file name includes a prefix that will
2425 * be processed during DFS name normalization as specified in
2426 * section 3.3.5.9. Otherwise, the file name is relative to
2427 * the share that is identified by the TreeId in the SMB2
2428 * header.
2429 */
2430 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2431 int name_len;
2432
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002433 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002434 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2435 &name_len,
2436 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002437 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002438 return rc;
2439 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002440 uni_path_len = copy_size;
2441 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002442 } else {
2443 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2444 /* MUST set path len (NameLength) to 0 opening root of share */
2445 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002446 copy_size = uni_path_len;
2447 if (copy_size % 8 != 0)
2448 copy_size = roundup(copy_size, 8);
2449 copy_path = kzalloc(copy_size, GFP_KERNEL);
2450 if (!copy_path)
2451 return -ENOMEM;
2452 memcpy((char *)copy_path, (const char *)path,
2453 uni_path_len);
2454 uni_path_len = copy_size;
2455 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002456 }
2457
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002458 iov[1].iov_len = uni_path_len;
2459 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002460
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002461 if (!server->oplocks)
2462 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2463
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002464 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002465 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2466 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002467 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2468 (oparms->create_options & CREATE_NOT_FILE))
2469 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002470 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002471 rc = add_lease_context(server, iov, &n_iov,
2472 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002473 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002474 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002475 }
2476
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002477 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2478 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002479 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002480 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002481 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002482 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002483 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002484 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002485
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002486 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002487 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002488 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002489 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002490 }
2491
Steve Frenchce558b02018-05-31 19:16:54 -05002492 if (tcon->posix_extensions) {
2493 if (n_iov > 2) {
2494 struct create_context *ccontext =
2495 (struct create_context *)iov[n_iov-1].iov_base;
2496 ccontext->Next =
2497 cpu_to_le32(iov[n_iov-1].iov_len);
2498 }
2499
2500 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002501 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002502 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002503 }
Steve Frenchce558b02018-05-31 19:16:54 -05002504
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002505 if (tcon->snapshot_time) {
2506 cifs_dbg(FYI, "adding snapshot context\n");
2507 if (n_iov > 2) {
2508 struct create_context *ccontext =
2509 (struct create_context *)iov[n_iov-1].iov_base;
2510 ccontext->Next =
2511 cpu_to_le32(iov[n_iov-1].iov_len);
2512 }
2513
2514 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2515 if (rc)
2516 return rc;
2517 }
2518
Steve Frenchff2a09e2019-07-06 14:41:38 -05002519 if (n_iov > 2) {
2520 struct create_context *ccontext =
2521 (struct create_context *)iov[n_iov-1].iov_base;
2522 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2523 }
2524 add_query_id_context(iov, &n_iov);
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002525
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002526 rqst->rq_nvec = n_iov;
2527 return 0;
2528}
2529
2530/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2531 * All other vectors are freed by kfree().
2532 */
2533void
2534SMB2_open_free(struct smb_rqst *rqst)
2535{
2536 int i;
2537
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002538 if (rqst && rqst->rq_iov) {
2539 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2540 for (i = 1; i < rqst->rq_nvec; i++)
2541 if (rqst->rq_iov[i].iov_base != smb2_padding)
2542 kfree(rqst->rq_iov[i].iov_base);
2543 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002544}
2545
2546int
2547SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2548 __u8 *oplock, struct smb2_file_all_info *buf,
2549 struct kvec *err_iov, int *buftype)
2550{
2551 struct smb_rqst rqst;
2552 struct smb2_create_rsp *rsp = NULL;
2553 struct TCP_Server_Info *server;
2554 struct cifs_tcon *tcon = oparms->tcon;
2555 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002556 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002557 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002558 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002559 int rc = 0;
2560 int flags = 0;
2561
2562 cifs_dbg(FYI, "create/open\n");
2563 if (ses && (ses->server))
2564 server = ses->server;
2565 else
2566 return -EIO;
2567
2568 if (smb3_encryption_required(tcon))
2569 flags |= CIFS_TRANSFORM_REQ;
2570
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002571 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002572 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002573 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002574 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002575
2576 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2577 if (rc)
2578 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002579
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002580 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2581 oparms->create_options, oparms->desired_access);
2582
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002583 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002584 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002585 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002586
2587 if (rc != 0) {
2588 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002589 if (err_iov && rsp) {
2590 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002591 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002592 resp_buftype = CIFS_NO_BUFFER;
2593 rsp = NULL;
2594 }
Steve French28d59362018-05-30 21:42:34 -05002595 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2596 oparms->create_options, oparms->desired_access, rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002597 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002598 } else
2599 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2600 ses->Suid, oparms->create_options,
2601 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002602
Steve Frenchfae80442018-10-19 17:14:32 -05002603 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002604 oparms->fid->persistent_fid = rsp->PersistentFileId;
2605 oparms->fid->volatile_fid = rsp->VolatileFileId;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002606#ifdef CONFIG_CIFS_DEBUG2
2607 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2608#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002609
2610 if (buf) {
2611 memcpy(buf, &rsp->CreationTime, 32);
2612 buf->AllocationSize = rsp->AllocationSize;
2613 buf->EndOfFile = rsp->EndofFile;
2614 buf->Attributes = rsp->FileAttributes;
2615 buf->NumberOfLinks = cpu_to_le32(1);
2616 buf->DeletePending = 0;
2617 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002618
Steve French89a5bfa2019-07-18 17:22:18 -05002619
2620 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2621 oparms->fid->lease_key, oplock, buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002622creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002623 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002624 free_rsp_buf(resp_buftype, rsp);
2625 return rc;
2626}
2627
Steve French4a72daf2013-06-25 00:20:49 -05002628int
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002629SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2630 u64 persistent_fid, u64 volatile_fid, u32 opcode,
Steve French153322f2019-03-28 22:32:49 -05002631 bool is_fsctl, char *in_data, u32 indatalen,
2632 __u32 max_response_size)
Steve French4a72daf2013-06-25 00:20:49 -05002633{
2634 struct smb2_ioctl_req *req;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002635 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002636 unsigned int total_len;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002637 int rc;
Long Li2c87d6a2019-05-15 14:09:05 -07002638 char *in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002639
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002640 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002641 if (rc)
2642 return rc;
2643
Long Li2c87d6a2019-05-15 14:09:05 -07002644 if (indatalen) {
2645 /*
2646 * indatalen is usually small at a couple of bytes max, so
2647 * just allocate through generic pool
2648 */
YueHaibingd81f0972019-06-01 03:31:10 +00002649 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
Long Li2c87d6a2019-05-15 14:09:05 -07002650 if (!in_data_buf) {
2651 cifs_small_buf_release(req);
2652 return -ENOMEM;
2653 }
Long Li2c87d6a2019-05-15 14:09:05 -07002654 }
2655
Steve French4a72daf2013-06-25 00:20:49 -05002656 req->CtlCode = cpu_to_le32(opcode);
2657 req->PersistentFileId = persistent_fid;
2658 req->VolatileFileId = volatile_fid;
2659
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002660 iov[0].iov_base = (char *)req;
2661 /*
2662 * If no input data, the size of ioctl struct in
2663 * protocol spec still includes a 1 byte data buffer,
2664 * but if input data passed to ioctl, we do not
2665 * want to double count this, so we do not send
2666 * the dummy one byte of data in iovec[0] if sending
2667 * input data (in iovec[1]).
2668 */
Steve French4a72daf2013-06-25 00:20:49 -05002669 if (indatalen) {
2670 req->InputCount = cpu_to_le32(indatalen);
2671 /* do not set InputOffset if no input data */
2672 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002673 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002674 rqst->rq_nvec = 2;
2675 iov[0].iov_len = total_len - 1;
Long Li2c87d6a2019-05-15 14:09:05 -07002676 iov[1].iov_base = in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002677 iov[1].iov_len = indatalen;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002678 } else {
2679 rqst->rq_nvec = 1;
2680 iov[0].iov_len = total_len;
2681 }
Steve French4a72daf2013-06-25 00:20:49 -05002682
2683 req->OutputOffset = 0;
2684 req->OutputCount = 0; /* MBZ */
2685
2686 /*
Steve French153322f2019-03-28 22:32:49 -05002687 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2688 * We Could increase default MaxOutputResponse, but that could require
2689 * more credits. Windows typically sets this smaller, but for some
Steve French4a72daf2013-06-25 00:20:49 -05002690 * ioctls it may be useful to allow server to send more. No point
2691 * limiting what the server can send as long as fits in one credit
Steve French153322f2019-03-28 22:32:49 -05002692 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2693 * to increase this limit up in the future.
2694 * Note that for snapshot queries that servers like Azure expect that
2695 * the first query be minimal size (and just used to get the number/size
2696 * of previous versions) so response size must be specified as EXACTLY
2697 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2698 * of eight bytes. Currently that is the only case where we set max
2699 * response size smaller.
Steve French4a72daf2013-06-25 00:20:49 -05002700 */
Steve French153322f2019-03-28 22:32:49 -05002701 req->MaxOutputResponse = cpu_to_le32(max_response_size);
Steve French4a72daf2013-06-25 00:20:49 -05002702
2703 if (is_fsctl)
2704 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2705 else
2706 req->Flags = 0;
2707
Steve French4587eee2017-10-25 15:58:31 -05002708 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2709 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002710 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002711
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002712 return 0;
2713}
2714
2715void
2716SMB2_ioctl_free(struct smb_rqst *rqst)
2717{
Murphy Zhou6457c202019-05-23 12:12:43 +08002718 int i;
Long Li2c87d6a2019-05-15 14:09:05 -07002719 if (rqst && rqst->rq_iov) {
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002720 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Murphy Zhou6457c202019-05-23 12:12:43 +08002721 for (i = 1; i < rqst->rq_nvec; i++)
2722 if (rqst->rq_iov[i].iov_base != smb2_padding)
2723 kfree(rqst->rq_iov[i].iov_base);
Long Li2c87d6a2019-05-15 14:09:05 -07002724 }
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002725}
2726
Steve French153322f2019-03-28 22:32:49 -05002727
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002728/*
2729 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2730 */
2731int
2732SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2733 u64 volatile_fid, u32 opcode, bool is_fsctl,
Steve French153322f2019-03-28 22:32:49 -05002734 char *in_data, u32 indatalen, u32 max_out_data_len,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002735 char **out_data, u32 *plen /* returned data len */)
2736{
2737 struct smb_rqst rqst;
2738 struct smb2_ioctl_rsp *rsp = NULL;
2739 struct cifs_ses *ses;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002740 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002741 struct kvec rsp_iov = {NULL, 0};
2742 int resp_buftype = CIFS_NO_BUFFER;
2743 int rc = 0;
2744 int flags = 0;
2745
2746 cifs_dbg(FYI, "SMB2 IOCTL\n");
2747
2748 if (out_data != NULL)
2749 *out_data = NULL;
2750
2751 /* zero out returned data len, in case of error */
2752 if (plen)
2753 *plen = 0;
2754
2755 if (tcon)
2756 ses = tcon->ses;
2757 else
2758 return -EIO;
2759
2760 if (!ses || !(ses->server))
2761 return -EIO;
2762
2763 if (smb3_encryption_required(tcon))
2764 flags |= CIFS_TRANSFORM_REQ;
2765
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002766 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002767 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002768 rqst.rq_iov = iov;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002769 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002770
Steve French153322f2019-03-28 22:32:49 -05002771 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2772 is_fsctl, in_data, indatalen, max_out_data_len);
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002773 if (rc)
2774 goto ioctl_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002775
2776 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002777 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002778 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002779
Steve Frencheccb4422018-05-17 21:16:55 -05002780 if (rc != 0)
2781 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2782 ses->Suid, 0, opcode, rc);
2783
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002784 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
Steve French8e353102015-03-26 19:47:02 -05002785 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002786 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002787 } else if (rc == -EINVAL) {
2788 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2789 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002790 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002791 goto ioctl_exit;
2792 }
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002793 } else if (rc == -E2BIG) {
2794 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2795 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2796 goto ioctl_exit;
2797 }
Steve French4a72daf2013-06-25 00:20:49 -05002798 }
2799
2800 /* check if caller wants to look at return data or just return rc */
2801 if ((plen == NULL) || (out_data == NULL))
2802 goto ioctl_exit;
2803
2804 *plen = le32_to_cpu(rsp->OutputCount);
2805
2806 /* We check for obvious errors in the output buffer length and offset */
2807 if (*plen == 0)
2808 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002809 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Steve French4a72daf2013-06-25 00:20:49 -05002810 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2811 *plen = 0;
2812 rc = -EIO;
2813 goto ioctl_exit;
2814 }
2815
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002816 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Steve French4a72daf2013-06-25 00:20:49 -05002817 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2818 le32_to_cpu(rsp->OutputOffset));
2819 *plen = 0;
2820 rc = -EIO;
2821 goto ioctl_exit;
2822 }
2823
YueHaibingd034fee2018-09-10 01:33:06 +00002824 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2825 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05002826 if (*out_data == NULL) {
2827 rc = -ENOMEM;
2828 goto ioctl_exit;
2829 }
2830
Steve French4a72daf2013-06-25 00:20:49 -05002831ioctl_exit:
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002832 SMB2_ioctl_free(&rqst);
Steve French4a72daf2013-06-25 00:20:49 -05002833 free_rsp_buf(resp_buftype, rsp);
2834 return rc;
2835}
2836
Steve French64a5cfa2013-10-14 15:31:32 -05002837/*
2838 * Individual callers to ioctl worker function follow
2839 */
2840
2841int
2842SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2843 u64 persistent_fid, u64 volatile_fid)
2844{
2845 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002846 struct compress_ioctl fsctl_input;
2847 char *ret_data = NULL;
2848
2849 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002850 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002851
2852 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2853 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2854 (char *)&fsctl_input /* data input */,
Steve French153322f2019-03-28 22:32:49 -05002855 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2856 &ret_data /* out data */, NULL);
Steve French64a5cfa2013-10-14 15:31:32 -05002857
2858 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002859
2860 return rc;
2861}
2862
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002863int
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002864SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2865 u64 persistent_fid, u64 volatile_fid)
2866{
2867 struct smb2_close_req *req;
2868 struct kvec *iov = rqst->rq_iov;
2869 unsigned int total_len;
2870 int rc;
2871
2872 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2873 if (rc)
2874 return rc;
2875
2876 req->PersistentFileId = persistent_fid;
2877 req->VolatileFileId = volatile_fid;
2878 iov[0].iov_base = (char *)req;
2879 iov[0].iov_len = total_len;
2880
2881 return 0;
2882}
2883
2884void
2885SMB2_close_free(struct smb_rqst *rqst)
2886{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002887 if (rqst && rqst->rq_iov)
2888 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002889}
2890
2891int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002892SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2893 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002894{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002895 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002896 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002897 struct cifs_ses *ses = tcon->ses;
2898 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002899 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01002900 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002901 int rc = 0;
2902
Joe Perchesf96637b2013-05-04 22:12:25 -05002903 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002904
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002905 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002906 return -EIO;
2907
Steve French5a77e752018-05-09 17:43:08 -05002908 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002909 flags |= CIFS_TRANSFORM_REQ;
2910
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002911 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002912 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002913 rqst.rq_iov = iov;
2914 rqst.rq_nvec = 1;
2915
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002916 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2917 if (rc)
2918 goto close_exit;
2919
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002920 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002921 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002922
2923 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002924 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002925 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2926 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002927 goto close_exit;
2928 }
2929
Steve Frenchfae80442018-10-19 17:14:32 -05002930 atomic_dec(&tcon->num_remote_opens);
2931
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002932 /* BB FIXME - decode close response, update inode for caching */
2933
2934close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002935 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002936 free_rsp_buf(resp_buftype, rsp);
2937 return rc;
2938}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002939
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002940int
2941SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2942 u64 persistent_fid, u64 volatile_fid)
2943{
2944 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2945}
2946
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002947int
2948smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2949 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002950{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002951 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002952 char *end_of_smb = smb_len + (char *)iov->iov_base;
2953 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002954 char *end_of_buf = begin_of_buf + buffer_length;
2955
2956
2957 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002958 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2959 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002960 return -EINVAL;
2961 }
2962
2963 /* check if beyond RFC1001 maximum length */
2964 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002965 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2966 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002967 return -EINVAL;
2968 }
2969
2970 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002971 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002972 return -EINVAL;
2973 }
2974
2975 return 0;
2976}
2977
2978/*
2979 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2980 * Caller must free buffer.
2981 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002982int
2983smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2984 struct kvec *iov, unsigned int minbufsize,
2985 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002986{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002987 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002988 int rc;
2989
2990 if (!data)
2991 return -EINVAL;
2992
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002993 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002994 if (rc)
2995 return rc;
2996
2997 memcpy(data, begin_of_buf, buffer_length);
2998
2999 return 0;
3000}
3001
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003002int
3003SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3004 u64 persistent_fid, u64 volatile_fid,
3005 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003006 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003007{
3008 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003009 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003010 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003011 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003012
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003013 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3014 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003015 if (rc)
3016 return rc;
3017
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003018 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003019 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003020 req->PersistentFileId = persistent_fid;
3021 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003022 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02003023
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003024 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003025 if (input_len) {
3026 req->InputBufferLength = cpu_to_le32(input_len);
3027 /* total_len for smb query request never close to le16 max */
3028 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3029 memcpy(req->Buffer, input, input_len);
3030 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003031
3032 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003033 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003034 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003035 return 0;
3036}
3037
3038void
3039SMB2_query_info_free(struct smb_rqst *rqst)
3040{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003041 if (rqst && rqst->rq_iov)
3042 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003043}
3044
3045static int
3046query_info(const unsigned int xid, struct cifs_tcon *tcon,
3047 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3048 u32 additional_info, size_t output_len, size_t min_len, void **data,
3049 u32 *dlen)
3050{
3051 struct smb_rqst rqst;
3052 struct smb2_query_info_rsp *rsp = NULL;
3053 struct kvec iov[1];
3054 struct kvec rsp_iov;
3055 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01003056 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003057 struct cifs_ses *ses = tcon->ses;
3058 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003059 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003060
3061 cifs_dbg(FYI, "Query Info\n");
3062
3063 if (!ses || !(ses->server))
3064 return -EIO;
3065
3066 if (smb3_encryption_required(tcon))
3067 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003068
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003069 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003070 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003071 rqst.rq_iov = iov;
3072 rqst.rq_nvec = 1;
3073
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003074 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3075 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003076 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003077 if (rc)
3078 goto qinf_exit;
3079
Steve Frenchd42043a2019-02-26 21:58:30 -06003080 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3081 ses->Suid, info_class, (__u32)info_type);
3082
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003083 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003084 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003085
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003086 if (rc) {
3087 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003088 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3089 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003090 goto qinf_exit;
3091 }
3092
Steve Frenchd42043a2019-02-26 21:58:30 -06003093 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3094 ses->Suid, info_class, (__u32)info_type);
3095
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003096 if (dlen) {
3097 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3098 if (!*data) {
3099 *data = kmalloc(*dlen, GFP_KERNEL);
3100 if (!*data) {
3101 cifs_dbg(VFS,
3102 "Error %d allocating memory for acl\n",
3103 rc);
3104 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003105 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003106 goto qinf_exit;
3107 }
Colin Ian King73aaf922019-01-16 16:28:59 +00003108 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003109 }
3110 }
3111
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003112 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3113 le32_to_cpu(rsp->OutputBufferLength),
3114 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00003115 if (rc && allocated) {
3116 kfree(*data);
3117 *data = NULL;
3118 *dlen = 0;
3119 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003120
3121qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003122 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003123 free_rsp_buf(resp_buftype, rsp);
3124 return rc;
3125}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003126
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003127int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3128 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003129{
3130 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003131 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04003132 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003133 sizeof(struct smb2_file_all_info), (void **)&data,
3134 NULL);
3135}
3136
3137int
3138SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3139 u64 persistent_fid, u64 volatile_fid,
3140 void **data, u32 *plen)
3141{
3142 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3143 *plen = 0;
3144
3145 return query_info(xid, tcon, persistent_fid, volatile_fid,
3146 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05003147 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003148}
3149
3150int
3151SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3152 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3153{
3154 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003155 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003156 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003157 sizeof(struct smb2_file_internal_info),
3158 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003159}
3160
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003161/*
3162 * This is a no-op for now. We're not really interested in the reply, but
3163 * rather in the fact that the server sent one and that server->lstrp
3164 * gets updated.
3165 *
3166 * FIXME: maybe we should consider checking that the reply matches request?
3167 */
3168static void
3169smb2_echo_callback(struct mid_q_entry *mid)
3170{
3171 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003172 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003173 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003174
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003175 if (mid->mid_state == MID_RESPONSE_RECEIVED
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003176 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3177 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3178 credits.instance = server->reconnect_instance;
3179 }
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003180
3181 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003182 add_credits(server, &credits, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003183}
3184
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003185void smb2_reconnect_server(struct work_struct *work)
3186{
3187 struct TCP_Server_Info *server = container_of(work,
3188 struct TCP_Server_Info, reconnect.work);
3189 struct cifs_ses *ses;
3190 struct cifs_tcon *tcon, *tcon2;
3191 struct list_head tmp_list;
3192 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01003193 int rc;
3194 int resched = false;
3195
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003196
3197 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3198 mutex_lock(&server->reconnect_mutex);
3199
3200 INIT_LIST_HEAD(&tmp_list);
3201 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3202
3203 spin_lock(&cifs_tcp_ses_lock);
3204 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3205 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003206 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003207 tcon->tc_count++;
3208 list_add_tail(&tcon->rlist, &tmp_list);
3209 tcon_exist = true;
3210 }
3211 }
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003212 /*
3213 * IPC has the same lifetime as its session and uses its
3214 * refcount.
3215 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003216 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3217 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3218 tcon_exist = true;
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003219 ses->ses_count++;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003220 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003221 }
3222 /*
3223 * Get the reference to server struct to be sure that the last call of
3224 * cifs_put_tcon() in the loop below won't release the server pointer.
3225 */
3226 if (tcon_exist)
3227 server->srv_count++;
3228
3229 spin_unlock(&cifs_tcp_ses_lock);
3230
3231 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01003232 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3233 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003234 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01003235 else
3236 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003237 list_del_init(&tcon->rlist);
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003238 if (tcon->ipc)
3239 cifs_put_smb_ses(tcon->ses);
3240 else
3241 cifs_put_tcon(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003242 }
3243
3244 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01003245 if (resched)
3246 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003247 mutex_unlock(&server->reconnect_mutex);
3248
3249 /* now we can safely release srv struct */
3250 if (tcon_exist)
3251 cifs_put_tcp_session(server, 1);
3252}
3253
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003254int
3255SMB2_echo(struct TCP_Server_Info *server)
3256{
3257 struct smb2_echo_req *req;
3258 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003259 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003260 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003261 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003262 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003263
Joe Perchesf96637b2013-05-04 22:12:25 -05003264 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003265
Steve French4fcd1812016-06-22 20:12:05 -05003266 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003267 /* No need to send echo on newly established connections */
3268 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3269 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003270 }
3271
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003272 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003273 if (rc)
3274 return rc;
3275
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003276 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003277
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003278 iov[0].iov_len = total_len;
3279 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003280
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003281 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003282 server, CIFS_ECHO_OP, NULL);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003283 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003284 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003285
3286 cifs_small_buf_release(req);
3287 return rc;
3288}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003289
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003290void
3291SMB2_flush_free(struct smb_rqst *rqst)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003292{
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003293 if (rqst && rqst->rq_iov)
3294 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3295}
3296
3297int
3298SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3299 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3300{
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003301 struct smb2_flush_req *req;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003302 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003303 unsigned int total_len;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003304 int rc;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003305
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003306 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003307 if (rc)
3308 return rc;
3309
3310 req->PersistentFileId = persistent_fid;
3311 req->VolatileFileId = volatile_fid;
3312
3313 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003314 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003315
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003316 return 0;
3317}
3318
3319int
3320SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3321 u64 volatile_fid)
3322{
3323 struct cifs_ses *ses = tcon->ses;
3324 struct smb_rqst rqst;
3325 struct kvec iov[1];
3326 struct kvec rsp_iov = {NULL, 0};
3327 int resp_buftype = CIFS_NO_BUFFER;
3328 int flags = 0;
3329 int rc = 0;
3330
3331 cifs_dbg(FYI, "flush\n");
3332 if (!ses || !(ses->server))
3333 return -EIO;
3334
3335 if (smb3_encryption_required(tcon))
3336 flags |= CIFS_TRANSFORM_REQ;
3337
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003338 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003339 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003340 rqst.rq_iov = iov;
3341 rqst.rq_nvec = 1;
3342
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003343 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3344 if (rc)
3345 goto flush_exit;
3346
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003347 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003348
Steve Frencheccb4422018-05-17 21:16:55 -05003349 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003350 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003351 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3352 rc);
3353 }
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003354
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003355 flush_exit:
3356 SMB2_flush_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003357 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003358 return rc;
3359}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003360
3361/*
3362 * To form a chain of read requests, any read requests after the first should
3363 * have the end_of_chain boolean set to true.
3364 */
3365static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003366smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003367 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3368 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003369{
3370 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003371 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003372 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07003373 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003374
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003375 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3376 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003377 if (rc)
3378 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003379
3380 server = io_parms->tcon->ses->server;
3381 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003382 return -ECONNABORTED;
3383
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003384 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003385 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003386
3387 req->PersistentFileId = io_parms->persistent_fid;
3388 req->VolatileFileId = io_parms->volatile_fid;
3389 req->ReadChannelInfoOffset = 0; /* reserved */
3390 req->ReadChannelInfoLength = 0; /* reserved */
3391 req->Channel = 0; /* reserved */
3392 req->MinimumCount = 0;
3393 req->Length = cpu_to_le32(io_parms->length);
3394 req->Offset = cpu_to_le64(io_parms->offset);
Steve Frenchd323c2462019-02-25 00:52:43 -06003395
3396 trace_smb3_read_enter(0 /* xid */,
3397 io_parms->persistent_fid,
3398 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3399 io_parms->offset, io_parms->length);
Long Libd3dcc62017-11-22 17:38:47 -07003400#ifdef CONFIG_CIFS_SMB_DIRECT
3401 /*
3402 * If we want to do a RDMA write, fill in and append
3403 * smbd_buffer_descriptor_v1 to the end of read request
3404 */
Long Libb4c0412018-04-17 12:17:08 -07003405 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003406 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003407
Long Libd3dcc62017-11-22 17:38:47 -07003408 struct smbd_buffer_descriptor_v1 *v1;
3409 bool need_invalidate =
3410 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3411
3412 rdata->mr = smbd_register_mr(
3413 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003414 rdata->nr_pages, rdata->page_offset,
3415 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003416 if (!rdata->mr)
Long Lib7972092019-04-05 21:36:34 +00003417 return -EAGAIN;
Long Libd3dcc62017-11-22 17:38:47 -07003418
3419 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3420 if (need_invalidate)
3421 req->Channel = SMB2_CHANNEL_RDMA_V1;
3422 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003423 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003424 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003425 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003426 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003427 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3428 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3429 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003430
3431 *total_len += sizeof(*v1) - 1;
3432 }
3433#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003434 if (request_type & CHAINED_REQUEST) {
3435 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003436 /* next 8-byte aligned request */
3437 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3438 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003439 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003440 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003441 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003442 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003443 /*
3444 * Related requests use info from previous read request
3445 * in chain.
3446 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003447 shdr->SessionId = 0xFFFFFFFF;
3448 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003449 req->PersistentFileId = 0xFFFFFFFF;
3450 req->VolatileFileId = 0xFFFFFFFF;
3451 }
3452 }
3453 if (remaining_bytes > io_parms->length)
3454 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3455 else
3456 req->RemainingBytes = 0;
3457
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003458 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003459 return rc;
3460}
3461
3462static void
3463smb2_readv_callback(struct mid_q_entry *mid)
3464{
3465 struct cifs_readdata *rdata = mid->callback_data;
3466 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3467 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003468 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003469 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003470 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003471 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3472 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07003473 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003474 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003475 .rq_npages = rdata->nr_pages,
3476 .rq_pagesz = rdata->pagesz,
3477 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003478
Joe Perchesf96637b2013-05-04 22:12:25 -05003479 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3480 __func__, mid->mid, mid->mid_state, rdata->result,
3481 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003482
3483 switch (mid->mid_state) {
3484 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003485 credits.value = le16_to_cpu(shdr->CreditRequest);
3486 credits.instance = server->reconnect_instance;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003487 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003488 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003489 int rc;
3490
Jeff Layton0b688cf2012-09-18 16:20:34 -07003491 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003492 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003493 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
3494 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003495 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003496 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003497 task_io_account_read(rdata->got_bytes);
3498 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003499 break;
3500 case MID_REQUEST_SUBMITTED:
3501 case MID_RETRY_NEEDED:
3502 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003503 if (server->sign && rdata->got_bytes)
3504 /* reset bytes number since we can not check a sign */
3505 rdata->got_bytes = 0;
3506 /* FIXME: should this be counted toward the initiating task? */
3507 task_io_account_read(rdata->got_bytes);
3508 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003509 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003510 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003511 credits.value = le16_to_cpu(shdr->CreditRequest);
3512 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003513 /* fall through */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003514 default:
Pavel Shilovsky6b15eb12019-01-18 15:46:14 -08003515 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003516 }
Long Libd3dcc62017-11-22 17:38:47 -07003517#ifdef CONFIG_CIFS_SMB_DIRECT
3518 /*
3519 * If this rdata has a memmory registered, the MR can be freed
3520 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3521 * because they have limited number and are used for future I/Os
3522 */
3523 if (rdata->mr) {
3524 smbd_deregister_mr(rdata->mr);
3525 rdata->mr = NULL;
3526 }
3527#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08003528 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003529 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003530 trace_smb3_read_err(0 /* xid */,
3531 rdata->cfile->fid.persistent_fid,
3532 tcon->tid, tcon->ses->Suid, rdata->offset,
3533 rdata->bytes, rdata->result);
3534 } else
3535 trace_smb3_read_done(0 /* xid */,
3536 rdata->cfile->fid.persistent_fid,
3537 tcon->tid, tcon->ses->Suid,
3538 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003539
3540 queue_work(cifsiod_wq, &rdata->work);
3541 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003542 add_credits(server, &credits, 0);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003543}
3544
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003545/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003546int
3547smb2_async_readv(struct cifs_readdata *rdata)
3548{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003549 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003550 char *buf;
3551 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003552 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003553 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003554 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003555 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003556 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003557
Joe Perchesf96637b2013-05-04 22:12:25 -05003558 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3559 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003560
3561 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3562 io_parms.offset = rdata->offset;
3563 io_parms.length = rdata->bytes;
3564 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3565 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3566 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003567
3568 server = io_parms.tcon->ses->server;
3569
Long Li2dabfd52017-11-07 01:54:53 -07003570 rc = smb2_new_read_req(
3571 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003572 if (rc)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003573 return rc;
3574
Steve French5a77e752018-05-09 17:43:08 -05003575 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003576 flags |= CIFS_TRANSFORM_REQ;
3577
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003578 rdata->iov[0].iov_base = buf;
3579 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003580
3581 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003582
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003583 if (rdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003584 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003585 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003586 shdr->CreditRequest =
3587 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003588
3589 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3590 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003591 goto async_readv_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003592
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003593 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003594 }
3595
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003596 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07003597 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003598 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003599 smb3_handle_read_data, rdata, flags,
3600 &rdata->credits);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003601 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003602 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003603 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003604 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3605 io_parms.tcon->tid,
3606 io_parms.tcon->ses->Suid,
3607 io_parms.offset, io_parms.length, rc);
3608 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003609
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003610async_readv_out:
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003611 cifs_small_buf_release(buf);
3612 return rc;
3613}
Pavel Shilovsky33319142012-09-18 16:20:29 -07003614
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003615int
3616SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3617 unsigned int *nbytes, char **buf, int *buf_type)
3618{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003619 struct smb_rqst rqst;
Colin Ian King1efd4fc2019-07-31 10:05:26 +01003620 int resp_buftype, rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003621 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003622 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003623 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003624 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003625 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003626 int flags = CIFS_LOG_ERROR;
3627 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003628
3629 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07003630 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003631 if (rc)
3632 return rc;
3633
Steve French5a77e752018-05-09 17:43:08 -05003634 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003635 flags |= CIFS_TRANSFORM_REQ;
3636
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003637 iov[0].iov_base = (char *)req;
3638 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003639
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003640 memset(&rqst, 0, sizeof(struct smb_rqst));
3641 rqst.rq_iov = iov;
3642 rqst.rq_nvec = 1;
3643
3644 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003645 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003646
3647 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003648 if (rc != -ENODATA) {
3649 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3650 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003651 trace_smb3_read_err(xid, req->PersistentFileId,
3652 io_parms->tcon->tid, ses->Suid,
3653 io_parms->offset, io_parms->length,
3654 rc);
Steve Frenchb0a42f22019-02-25 15:02:58 -06003655 } else
3656 trace_smb3_read_done(xid, req->PersistentFileId,
3657 io_parms->tcon->tid, ses->Suid,
3658 io_parms->offset, 0);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003659 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Ronnie Sahlberg05fd5c22019-04-23 16:39:45 +10003660 cifs_small_buf_release(req);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003661 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05003662 } else
3663 trace_smb3_read_done(xid, req->PersistentFileId,
3664 io_parms->tcon->tid, ses->Suid,
3665 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003666
ZhangXiaoxu088aaf12019-04-06 15:47:39 +08003667 cifs_small_buf_release(req);
3668
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003669 *nbytes = le32_to_cpu(rsp->DataLength);
3670 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3671 (*nbytes > io_parms->length)) {
3672 cifs_dbg(FYI, "bad length %d for count %d\n",
3673 *nbytes, io_parms->length);
3674 rc = -EIO;
3675 *nbytes = 0;
3676 }
3677
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003678 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003679 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003680 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003681 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003682 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003683 if (resp_buftype == CIFS_SMALL_BUFFER)
3684 *buf_type = CIFS_SMALL_BUFFER;
3685 else if (resp_buftype == CIFS_LARGE_BUFFER)
3686 *buf_type = CIFS_LARGE_BUFFER;
3687 }
3688 return rc;
3689}
3690
Pavel Shilovsky33319142012-09-18 16:20:29 -07003691/*
3692 * Check the mid_state and signature on received buffer (if any), and queue the
3693 * workqueue completion task.
3694 */
3695static void
3696smb2_writev_callback(struct mid_q_entry *mid)
3697{
3698 struct cifs_writedata *wdata = mid->callback_data;
3699 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003700 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003701 unsigned int written;
3702 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003703 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky33319142012-09-18 16:20:29 -07003704
3705 switch (mid->mid_state) {
3706 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003707 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3708 credits.instance = server->reconnect_instance;
3709 wdata->result = smb2_check_receive(mid, server, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003710 if (wdata->result != 0)
3711 break;
3712
3713 written = le32_to_cpu(rsp->DataLength);
3714 /*
3715 * Mask off high 16 bits when bytes written as returned
3716 * by the server is greater than bytes requested by the
3717 * client. OS/2 servers are known to set incorrect
3718 * CountHigh values.
3719 */
3720 if (written > wdata->bytes)
3721 written &= 0xFFFF;
3722
3723 if (written < wdata->bytes)
3724 wdata->result = -ENOSPC;
3725 else
3726 wdata->bytes = written;
3727 break;
3728 case MID_REQUEST_SUBMITTED:
3729 case MID_RETRY_NEEDED:
3730 wdata->result = -EAGAIN;
3731 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003732 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003733 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3734 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003735 /* fall through */
Pavel Shilovsky33319142012-09-18 16:20:29 -07003736 default:
3737 wdata->result = -EIO;
3738 break;
3739 }
Long Lidb223a52017-11-22 17:38:45 -07003740#ifdef CONFIG_CIFS_SMB_DIRECT
3741 /*
3742 * If this wdata has a memory registered, the MR can be freed
3743 * The number of MRs available is limited, it's important to recover
3744 * used MR as soon as I/O is finished. Hold MR longer in the later
3745 * I/O process can possibly result in I/O deadlock due to lack of MR
3746 * to send request on I/O retry
3747 */
3748 if (wdata->mr) {
3749 smbd_deregister_mr(wdata->mr);
3750 wdata->mr = NULL;
3751 }
3752#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003753 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003754 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003755 trace_smb3_write_err(0 /* no xid */,
3756 wdata->cfile->fid.persistent_fid,
3757 tcon->tid, tcon->ses->Suid, wdata->offset,
3758 wdata->bytes, wdata->result);
3759 } else
3760 trace_smb3_write_done(0 /* no xid */,
3761 wdata->cfile->fid.persistent_fid,
3762 tcon->tid, tcon->ses->Suid,
3763 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003764
3765 queue_work(cifsiod_wq, &wdata->work);
3766 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003767 add_credits(server, &credits, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003768}
3769
3770/* smb2_async_writev - send an async write, and set up mid to handle result */
3771int
Steve French4a5c80d2014-02-07 20:45:12 -06003772smb2_async_writev(struct cifs_writedata *wdata,
3773 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07003774{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003775 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003776 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003777 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003778 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003779 struct TCP_Server_Info *server = tcon->ses->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003780 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003781 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003782 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003783
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003784 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003785 if (rc)
3786 return rc;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003787
Steve French5a77e752018-05-09 17:43:08 -05003788 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003789 flags |= CIFS_TRANSFORM_REQ;
3790
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003791 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003792 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003793
3794 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3795 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3796 req->WriteChannelInfoOffset = 0;
3797 req->WriteChannelInfoLength = 0;
3798 req->Channel = 0;
3799 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003800 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003801 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003802 req->RemainingBytes = 0;
Steve Frenchd323c2462019-02-25 00:52:43 -06003803
3804 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3805 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003806#ifdef CONFIG_CIFS_SMB_DIRECT
3807 /*
3808 * If we want to do a server RDMA read, fill in and append
3809 * smbd_buffer_descriptor_v1 to the end of write request
3810 */
Long Libb4c0412018-04-17 12:17:08 -07003811 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003812 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003813
Long Lidb223a52017-11-22 17:38:45 -07003814 struct smbd_buffer_descriptor_v1 *v1;
3815 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3816
3817 wdata->mr = smbd_register_mr(
3818 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003819 wdata->nr_pages, wdata->page_offset,
3820 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07003821 if (!wdata->mr) {
Long Lib7972092019-04-05 21:36:34 +00003822 rc = -EAGAIN;
Long Lidb223a52017-11-22 17:38:45 -07003823 goto async_writev_out;
3824 }
3825 req->Length = 0;
3826 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07003827 if (wdata->nr_pages > 1)
3828 req->RemainingBytes =
3829 cpu_to_le32(
3830 (wdata->nr_pages - 1) * wdata->pagesz -
3831 wdata->page_offset + wdata->tailsz
3832 );
3833 else
3834 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003835 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3836 if (need_invalidate)
3837 req->Channel = SMB2_CHANNEL_RDMA_V1;
3838 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003839 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003840 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003841 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003842 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003843 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3844 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3845 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003846 }
3847#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003848 iov[0].iov_len = total_len - 1;
3849 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003850
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003851 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003852 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003853 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07003854 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003855 rqst.rq_npages = wdata->nr_pages;
3856 rqst.rq_pagesz = wdata->pagesz;
3857 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003858#ifdef CONFIG_CIFS_SMB_DIRECT
3859 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003860 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07003861 rqst.rq_npages = 0;
3862 }
3863#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003864 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3865 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003866
Long Lidb223a52017-11-22 17:38:45 -07003867#ifdef CONFIG_CIFS_SMB_DIRECT
3868 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3869 if (!wdata->mr)
3870 req->Length = cpu_to_le32(wdata->bytes);
3871#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003872 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003873#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003874
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003875 if (wdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003876 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003877 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003878 shdr->CreditRequest =
3879 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003880
3881 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3882 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003883 goto async_writev_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003884
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003885 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003886 }
3887
Pavel Shilovsky33319142012-09-18 16:20:29 -07003888 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003889 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003890 wdata, flags, &wdata->credits);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003891
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003892 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003893 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3894 tcon->tid, tcon->ses->Suid, wdata->offset,
3895 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06003896 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003897 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003898 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003899
Pavel Shilovsky33319142012-09-18 16:20:29 -07003900async_writev_out:
3901 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003902 return rc;
3903}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003904
3905/*
3906 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3907 * The length field from io_parms must be at least 1 and indicates a number of
3908 * elements with data to write that begins with position 1 in iov array. All
3909 * data length is specified by count.
3910 */
3911int
3912SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3913 unsigned int *nbytes, struct kvec *iov, int n_vec)
3914{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003915 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003916 int rc = 0;
3917 struct smb2_write_req *req = NULL;
3918 struct smb2_write_rsp *rsp = NULL;
3919 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003920 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003921 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003922 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003923
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003924 *nbytes = 0;
3925
3926 if (n_vec < 1)
3927 return rc;
3928
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003929 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3930 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003931 if (rc)
3932 return rc;
3933
3934 if (io_parms->tcon->ses->server == NULL)
3935 return -ECONNABORTED;
3936
Steve French5a77e752018-05-09 17:43:08 -05003937 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003938 flags |= CIFS_TRANSFORM_REQ;
3939
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003940 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003941
3942 req->PersistentFileId = io_parms->persistent_fid;
3943 req->VolatileFileId = io_parms->volatile_fid;
3944 req->WriteChannelInfoOffset = 0;
3945 req->WriteChannelInfoLength = 0;
3946 req->Channel = 0;
3947 req->Length = cpu_to_le32(io_parms->length);
3948 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003949 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003950 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003951 req->RemainingBytes = 0;
3952
Steve Frenchd323c2462019-02-25 00:52:43 -06003953 trace_smb3_write_enter(xid, io_parms->persistent_fid,
3954 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3955 io_parms->offset, io_parms->length);
3956
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003957 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003958 /* 1 for Buffer */
3959 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003960
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003961 memset(&rqst, 0, sizeof(struct smb_rqst));
3962 rqst.rq_iov = iov;
3963 rqst.rq_nvec = n_vec + 1;
3964
3965 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003966 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003967 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003968
3969 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003970 trace_smb3_write_err(xid, req->PersistentFileId,
3971 io_parms->tcon->tid,
3972 io_parms->tcon->ses->Suid,
3973 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003974 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003975 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05003976 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003977 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05003978 trace_smb3_write_done(xid, req->PersistentFileId,
3979 io_parms->tcon->tid,
3980 io_parms->tcon->ses->Suid,
3981 io_parms->offset, *nbytes);
3982 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003983
ZhangXiaoxu6a3eb332019-04-06 15:47:38 +08003984 cifs_small_buf_release(req);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003985 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003986 return rc;
3987}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003988
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003989static unsigned int
3990num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3991{
3992 int len;
3993 unsigned int entrycount = 0;
3994 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03003995 char *entryptr;
3996 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003997
3998 if (bufstart == NULL)
3999 return 0;
4000
Dan Carpenter56446f22018-09-06 12:48:22 +03004001 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004002
4003 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03004004 if (entryptr + next_offset < entryptr ||
4005 entryptr + next_offset > end_of_buf ||
4006 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004007 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004008 break;
4009 }
4010
Dan Carpenter56446f22018-09-06 12:48:22 +03004011 entryptr = entryptr + next_offset;
4012 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4013
4014 len = le32_to_cpu(dir_info->FileNameLength);
4015 if (entryptr + len < entryptr ||
4016 entryptr + len > end_of_buf ||
4017 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004018 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4019 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004020 break;
4021 }
4022
Dan Carpenter56446f22018-09-06 12:48:22 +03004023 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004024 entrycount++;
4025
Dan Carpenter56446f22018-09-06 12:48:22 +03004026 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004027 if (!next_offset)
4028 break;
4029 }
4030
4031 return entrycount;
4032}
4033
4034/*
4035 * Readdir/FindFirst
4036 */
4037int
4038SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4039 u64 persistent_fid, u64 volatile_fid, int index,
4040 struct cifs_search_info *srch_inf)
4041{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004042 struct smb_rqst rqst;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004043 struct smb2_query_directory_req *req;
4044 struct smb2_query_directory_rsp *rsp = NULL;
4045 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004046 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004047 int rc = 0;
4048 int len;
Steve French75fdfc82015-03-25 18:51:57 -05004049 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004050 unsigned char *bufptr;
4051 struct TCP_Server_Info *server;
4052 struct cifs_ses *ses = tcon->ses;
4053 __le16 asteriks = cpu_to_le16('*');
4054 char *end_of_smb;
4055 unsigned int output_size = CIFSMaxBufSize;
4056 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004057 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004058 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004059
4060 if (ses && (ses->server))
4061 server = ses->server;
4062 else
4063 return -EIO;
4064
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004065 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4066 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004067 if (rc)
4068 return rc;
4069
Steve French5a77e752018-05-09 17:43:08 -05004070 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004071 flags |= CIFS_TRANSFORM_REQ;
4072
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004073 switch (srch_inf->info_level) {
4074 case SMB_FIND_FILE_DIRECTORY_INFO:
4075 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4076 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4077 break;
4078 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4079 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4080 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4081 break;
4082 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05004083 cifs_dbg(VFS, "info level %u isn't supported\n",
4084 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004085 rc = -EINVAL;
4086 goto qdir_exit;
4087 }
4088
4089 req->FileIndex = cpu_to_le32(index);
4090 req->PersistentFileId = persistent_fid;
4091 req->VolatileFileId = volatile_fid;
4092
4093 len = 0x2;
4094 bufptr = req->Buffer;
4095 memcpy(bufptr, &asteriks, len);
4096
4097 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004098 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004099 req->FileNameLength = cpu_to_le16(len);
4100 /*
4101 * BB could be 30 bytes or so longer if we used SMB2 specific
4102 * buffer lengths, but this is safe and close enough.
4103 */
4104 output_size = min_t(unsigned int, output_size, server->maxBuf);
4105 output_size = min_t(unsigned int, output_size, 2 << 15);
4106 req->OutputBufferLength = cpu_to_le32(output_size);
4107
4108 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004109 /* 1 for Buffer */
4110 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004111
4112 iov[1].iov_base = (char *)(req->Buffer);
4113 iov[1].iov_len = len;
4114
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004115 memset(&rqst, 0, sizeof(struct smb_rqst));
4116 rqst.rq_iov = iov;
4117 rqst.rq_nvec = 2;
4118
Steve Frenchd323c2462019-02-25 00:52:43 -06004119 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4120 tcon->ses->Suid, index, output_size);
4121
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004122 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004123 cifs_small_buf_release(req);
4124 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004125
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004126 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004127 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004128 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004129 trace_smb3_query_dir_done(xid, persistent_fid,
4130 tcon->tid, tcon->ses->Suid, index, 0);
Pavel Shilovsky52755802014-08-18 20:49:57 +04004131 srch_inf->endOfSearch = true;
4132 rc = 0;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004133 } else {
4134 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4135 tcon->ses->Suid, index, 0, rc);
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08004136 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004137 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004138 goto qdir_exit;
4139 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004140
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004141 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4142 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4143 info_buf_size);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004144 if (rc) {
4145 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4146 tcon->ses->Suid, index, 0, rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004147 goto qdir_exit;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004148 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004149
4150 srch_inf->unicode = true;
4151
4152 if (srch_inf->ntwrk_buf_start) {
4153 if (srch_inf->smallBuf)
4154 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4155 else
4156 cifs_buf_release(srch_inf->ntwrk_buf_start);
4157 }
4158 srch_inf->ntwrk_buf_start = (char *)rsp;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004159 srch_inf->srch_entries_start = srch_inf->last_entry =
4160 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4161 end_of_smb = rsp_iov.iov_len + (char *)rsp;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004162 srch_inf->entries_in_buffer =
4163 num_entries(srch_inf->srch_entries_start, end_of_smb,
4164 &srch_inf->last_entry, info_buf_size);
4165 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05004166 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4167 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4168 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004169 if (resp_buftype == CIFS_LARGE_BUFFER)
4170 srch_inf->smallBuf = false;
4171 else if (resp_buftype == CIFS_SMALL_BUFFER)
4172 srch_inf->smallBuf = true;
4173 else
Joe Perchesf96637b2013-05-04 22:12:25 -05004174 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004175
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004176 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4177 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004178 return rc;
4179
4180qdir_exit:
4181 free_rsp_buf(resp_buftype, rsp);
4182 return rc;
4183}
4184
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004185int
4186SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004187 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004188 u8 info_type, u32 additional_info,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004189 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004190{
4191 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004192 struct kvec *iov = rqst->rq_iov;
4193 unsigned int i, total_len;
4194 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004195
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004196 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004197 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004198 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004199
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004200 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004201 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004202 req->FileInfoClass = info_class;
4203 req->PersistentFileId = persistent_fid;
4204 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004205 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004206
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004207 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004208 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004209 req->BufferLength = cpu_to_le32(*size);
4210
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004211 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004212 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004213
4214 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004215 /* 1 for Buffer */
4216 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004217
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004218 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004219 le32_add_cpu(&req->BufferLength, size[i]);
4220 iov[i].iov_base = (char *)data[i];
4221 iov[i].iov_len = size[i];
4222 }
4223
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004224 return 0;
4225}
4226
4227void
4228SMB2_set_info_free(struct smb_rqst *rqst)
4229{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10004230 if (rqst && rqst->rq_iov)
4231 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004232}
4233
4234static int
4235send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4236 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4237 u8 info_type, u32 additional_info, unsigned int num,
4238 void **data, unsigned int *size)
4239{
4240 struct smb_rqst rqst;
4241 struct smb2_set_info_rsp *rsp = NULL;
4242 struct kvec *iov;
4243 struct kvec rsp_iov;
4244 int rc = 0;
4245 int resp_buftype;
4246 struct cifs_ses *ses = tcon->ses;
4247 int flags = 0;
4248
4249 if (!ses || !(ses->server))
4250 return -EIO;
4251
4252 if (!num)
4253 return -EINVAL;
4254
4255 if (smb3_encryption_required(tcon))
4256 flags |= CIFS_TRANSFORM_REQ;
4257
4258 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4259 if (!iov)
4260 return -ENOMEM;
4261
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004262 memset(&rqst, 0, sizeof(struct smb_rqst));
4263 rqst.rq_iov = iov;
4264 rqst.rq_nvec = num;
4265
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004266 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4267 info_class, info_type, additional_info,
4268 data, size);
4269 if (rc) {
4270 kfree(iov);
4271 return rc;
4272 }
4273
4274
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004275 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004276 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004277 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004278 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004279
Steve Frencheccb4422018-05-17 21:16:55 -05004280 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004281 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004282 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4283 ses->Suid, info_class, (__u32)info_type, rc);
4284 }
Steve French7d3fb242013-11-18 09:56:28 -06004285
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004286 free_rsp_buf(resp_buftype, rsp);
4287 kfree(iov);
4288 return rc;
4289}
4290
4291int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004292SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004293 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004294{
4295 struct smb2_file_eof_info info;
4296 void *data;
4297 unsigned int size;
4298
4299 info.EndOfFile = *eof;
4300
4301 data = &info;
4302 size = sizeof(struct smb2_file_eof_info);
4303
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004304 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004305 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4306 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004307}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004308
4309int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004310SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4311 u64 persistent_fid, u64 volatile_fid,
4312 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4313{
4314 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4315 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4316 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004317}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004318
4319int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004320SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4321 u64 persistent_fid, u64 volatile_fid,
4322 struct smb2_file_full_ea_info *buf, int len)
4323{
4324 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4325 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4326 0, 1, (void **)&buf, &len);
4327}
4328
4329int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004330SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4331 const u64 persistent_fid, const u64 volatile_fid,
4332 __u8 oplock_level)
4333{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004334 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004335 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10004336 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004337 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004338 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004339 unsigned int total_len;
4340 struct kvec iov[1];
4341 struct kvec rsp_iov;
4342 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004343
Joe Perchesf96637b2013-05-04 22:12:25 -05004344 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004345 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4346 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004347 if (rc)
4348 return rc;
4349
Steve French5a77e752018-05-09 17:43:08 -05004350 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004351 flags |= CIFS_TRANSFORM_REQ;
4352
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004353 req->VolatileFid = volatile_fid;
4354 req->PersistentFid = persistent_fid;
4355 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004356 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004357
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004358 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004359
4360 iov[0].iov_base = (char *)req;
4361 iov[0].iov_len = total_len;
4362
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004363 memset(&rqst, 0, sizeof(struct smb_rqst));
4364 rqst.rq_iov = iov;
4365 rqst.rq_nvec = 1;
4366
4367 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004368 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004369
4370 if (rc) {
4371 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004372 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004373 }
4374
4375 return rc;
4376}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004377
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004378void
4379smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4380 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004381{
4382 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4383 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4384 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05304385 kst->f_bfree = kst->f_bavail =
4386 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004387 return;
4388}
4389
Steve French2d304212018-06-24 23:28:12 -05004390static void
4391copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4392 struct kstatfs *kst)
4393{
4394 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4395 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4396 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4397 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4398 kst->f_bavail = kst->f_bfree;
4399 else
4400 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4401 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4402 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4403 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4404 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4405
4406 return;
4407}
Steve French2d304212018-06-24 23:28:12 -05004408
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004409static int
4410build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4411 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4412{
4413 int rc;
4414 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004415 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004416
Joe Perchesf96637b2013-05-04 22:12:25 -05004417 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004418
4419 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4420 return -EIO;
4421
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004422 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4423 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004424 if (rc)
4425 return rc;
4426
4427 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4428 req->FileInfoClass = level;
4429 req->PersistentFileId = persistent_fid;
4430 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004431 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004432 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004433 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004434 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004435 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004436
4437 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004438 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004439 return 0;
4440}
4441
Steve French2d304212018-06-24 23:28:12 -05004442int
4443SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4444 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4445{
4446 struct smb_rqst rqst;
4447 struct smb2_query_info_rsp *rsp = NULL;
4448 struct kvec iov;
4449 struct kvec rsp_iov;
4450 int rc = 0;
4451 int resp_buftype;
4452 struct cifs_ses *ses = tcon->ses;
4453 FILE_SYSTEM_POSIX_INFO *info = NULL;
4454 int flags = 0;
4455
4456 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4457 sizeof(FILE_SYSTEM_POSIX_INFO),
4458 persistent_fid, volatile_fid);
4459 if (rc)
4460 return rc;
4461
4462 if (smb3_encryption_required(tcon))
4463 flags |= CIFS_TRANSFORM_REQ;
4464
4465 memset(&rqst, 0, sizeof(struct smb_rqst));
4466 rqst.rq_iov = &iov;
4467 rqst.rq_nvec = 1;
4468
4469 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4470 cifs_small_buf_release(iov.iov_base);
4471 if (rc) {
4472 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4473 goto posix_qfsinf_exit;
4474 }
4475 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4476
4477 info = (FILE_SYSTEM_POSIX_INFO *)(
4478 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004479 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4480 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4481 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05004482 if (!rc)
4483 copy_posix_fs_info_to_kstatfs(info, fsdata);
4484
4485posix_qfsinf_exit:
4486 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4487 return rc;
4488}
Steve French2d304212018-06-24 23:28:12 -05004489
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004490int
4491SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4492 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4493{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004494 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004495 struct smb2_query_info_rsp *rsp = NULL;
4496 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004497 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004498 int rc = 0;
4499 int resp_buftype;
4500 struct cifs_ses *ses = tcon->ses;
4501 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004502 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004503
4504 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4505 sizeof(struct smb2_fs_full_size_info),
4506 persistent_fid, volatile_fid);
4507 if (rc)
4508 return rc;
4509
Steve French5a77e752018-05-09 17:43:08 -05004510 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004511 flags |= CIFS_TRANSFORM_REQ;
4512
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004513 memset(&rqst, 0, sizeof(struct smb_rqst));
4514 rqst.rq_iov = &iov;
4515 rqst.rq_nvec = 1;
4516
4517 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004518 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004519 if (rc) {
4520 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05004521 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004522 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004523 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004524
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004525 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004526 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004527 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4528 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4529 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004530 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004531 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004532
Steve French34f62642013-10-09 02:07:00 -05004533qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004534 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004535 return rc;
4536}
4537
4538int
4539SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05004540 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05004541{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004542 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05004543 struct smb2_query_info_rsp *rsp = NULL;
4544 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004545 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05004546 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05004547 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05004548 struct cifs_ses *ses = tcon->ses;
4549 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004550 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05004551
Steven French21671142013-10-09 13:36:35 -05004552 if (level == FS_DEVICE_INFORMATION) {
4553 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4554 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4555 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4556 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4557 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004558 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4559 max_len = sizeof(struct smb3_fs_ss_info);
4560 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05004561 } else if (level == FS_VOLUME_INFORMATION) {
4562 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4563 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05004564 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004565 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05004566 return -EINVAL;
4567 }
4568
4569 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05004570 persistent_fid, volatile_fid);
4571 if (rc)
4572 return rc;
4573
Steve French5a77e752018-05-09 17:43:08 -05004574 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004575 flags |= CIFS_TRANSFORM_REQ;
4576
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004577 memset(&rqst, 0, sizeof(struct smb_rqst));
4578 rqst.rq_iov = &iov;
4579 rqst.rq_nvec = 1;
4580
4581 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004582 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004583 if (rc) {
4584 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4585 goto qfsattr_exit;
4586 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004587 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05004588
4589 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4590 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004591 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05004592 if (rc)
4593 goto qfsattr_exit;
4594
4595 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004596 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004597 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05004598 rsp_len, max_len));
4599 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004600 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004601 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004602 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4603 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004604 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004605 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4606 tcon->perf_sector_size =
4607 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05004608 } else if (level == FS_VOLUME_INFORMATION) {
4609 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4610 (offset + (char *)rsp);
4611 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4612 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004613 }
Steve French34f62642013-10-09 02:07:00 -05004614
4615qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004616 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004617 return rc;
4618}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004619
4620int
4621smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4622 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4623 const __u32 num_lock, struct smb2_lock_element *buf)
4624{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004625 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004626 int rc = 0;
4627 struct smb2_lock_req *req = NULL;
4628 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004629 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004630 int resp_buf_type;
4631 unsigned int count;
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004632 int flags = CIFS_NO_RSP_BUF;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004633 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004634
Joe Perchesf96637b2013-05-04 22:12:25 -05004635 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004636
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004637 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004638 if (rc)
4639 return rc;
4640
Steve French5a77e752018-05-09 17:43:08 -05004641 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004642 flags |= CIFS_TRANSFORM_REQ;
4643
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004644 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004645 req->LockCount = cpu_to_le16(num_lock);
4646
4647 req->PersistentFileId = persist_fid;
4648 req->VolatileFileId = volatile_fid;
4649
4650 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004651
4652 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004653 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004654 iov[1].iov_base = (char *)buf;
4655 iov[1].iov_len = count;
4656
4657 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004658
4659 memset(&rqst, 0, sizeof(struct smb_rqst));
4660 rqst.rq_iov = iov;
4661 rqst.rq_nvec = 2;
4662
4663 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004664 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004665 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004666 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004667 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004668 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004669 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4670 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004671 }
4672
4673 return rc;
4674}
4675
4676int
4677SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4678 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4679 const __u64 length, const __u64 offset, const __u32 lock_flags,
4680 const bool wait)
4681{
4682 struct smb2_lock_element lock;
4683
4684 lock.Offset = cpu_to_le64(offset);
4685 lock.Length = cpu_to_le64(length);
4686 lock.Flags = cpu_to_le32(lock_flags);
4687 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4688 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4689
4690 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4691}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004692
4693int
4694SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4695 __u8 *lease_key, const __le32 lease_state)
4696{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004697 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004698 int rc;
4699 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004700 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004701 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004702 unsigned int total_len;
4703 struct kvec iov[1];
4704 struct kvec rsp_iov;
4705 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05004706 __u64 *please_key_high;
4707 __u64 *please_key_low;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004708
Joe Perchesf96637b2013-05-04 22:12:25 -05004709 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004710 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4711 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004712 if (rc)
4713 return rc;
4714
Steve French5a77e752018-05-09 17:43:08 -05004715 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004716 flags |= CIFS_TRANSFORM_REQ;
4717
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004718 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004719 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004720 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004721
4722 memcpy(req->LeaseKey, lease_key, 16);
4723 req->LeaseState = lease_state;
4724
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004725 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004726
4727 iov[0].iov_base = (char *)req;
4728 iov[0].iov_len = total_len;
4729
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004730 memset(&rqst, 0, sizeof(struct smb_rqst));
4731 rqst.rq_iov = iov;
4732 rqst.rq_nvec = 1;
4733
4734 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004735 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004736
Aurelien Apteld339adc2019-01-31 13:46:07 +01004737 please_key_low = (__u64 *)lease_key;
4738 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004739 if (rc) {
4740 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05004741 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4742 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05004743 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05004744 } else
4745 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4746 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004747
4748 return rc;
4749}