blob: 33efc5fb293c8b4625bcf427d751f86ca3611f03 [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
1199 req->Capabilities = 0;
1200 req->Channel = 0; /* MBZ */
1201
1202 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001203 /* 1 for pad */
1204 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001205 /*
1206 * This variable will be used to clear the buffer
1207 * allocated above in case of any error in the calling function.
1208 */
1209 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1210
1211 return 0;
1212}
1213
1214static void
1215SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1216{
1217 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1218 sess_data->buf0_type = CIFS_NO_BUFFER;
1219}
1220
1221static int
1222SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1223{
1224 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001225 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001226 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001227 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001228
1229 /* Testing shows that buffer offset must be at location of Buffer[0] */
1230 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001231 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001232 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1233
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001234 memset(&rqst, 0, sizeof(struct smb_rqst));
1235 rqst.rq_iov = sess_data->iov;
1236 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001237
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001238 /* BB add code to build os and lm fields */
1239 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1240 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001241 &sess_data->buf0_type,
1242 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001243 cifs_small_buf_release(sess_data->iov[0].iov_base);
1244 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001245
1246 return rc;
1247}
1248
1249static int
1250SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1251{
1252 int rc = 0;
1253 struct cifs_ses *ses = sess_data->ses;
1254
1255 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001256 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001257 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001258 if (rc) {
1259 cifs_dbg(FYI,
1260 "SMB3 session key generation failed\n");
1261 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001262 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001263 }
1264 }
1265 if (!ses->server->session_estab) {
1266 ses->server->sequence_number = 0x2;
1267 ses->server->session_estab = true;
1268 }
1269 mutex_unlock(&ses->server->srv_mutex);
1270
1271 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1272 spin_lock(&GlobalMid_Lock);
1273 ses->status = CifsGood;
1274 ses->need_reconnect = false;
1275 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001276 return rc;
1277}
1278
1279#ifdef CONFIG_CIFS_UPCALL
1280static void
1281SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1282{
1283 int rc;
1284 struct cifs_ses *ses = sess_data->ses;
1285 struct cifs_spnego_msg *msg;
1286 struct key *spnego_key = NULL;
1287 struct smb2_sess_setup_rsp *rsp = NULL;
1288
1289 rc = SMB2_sess_alloc_buffer(sess_data);
1290 if (rc)
1291 goto out;
1292
1293 spnego_key = cifs_get_spnego_key(ses);
1294 if (IS_ERR(spnego_key)) {
1295 rc = PTR_ERR(spnego_key);
1296 spnego_key = NULL;
1297 goto out;
1298 }
1299
1300 msg = spnego_key->payload.data[0];
1301 /*
1302 * check version field to make sure that cifs.upcall is
1303 * sending us a response in an expected form
1304 */
1305 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1306 cifs_dbg(VFS,
1307 "bad cifs.upcall version. Expected %d got %d",
1308 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1309 rc = -EKEYREJECTED;
1310 goto out_put_spnego_key;
1311 }
1312
1313 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1314 GFP_KERNEL);
1315 if (!ses->auth_key.response) {
1316 cifs_dbg(VFS,
1317 "Kerberos can't allocate (%u bytes) memory",
1318 msg->sesskey_len);
1319 rc = -ENOMEM;
1320 goto out_put_spnego_key;
1321 }
1322 ses->auth_key.len = msg->sesskey_len;
1323
1324 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1325 sess_data->iov[1].iov_len = msg->secblob_len;
1326
1327 rc = SMB2_sess_sendreceive(sess_data);
1328 if (rc)
1329 goto out_put_spnego_key;
1330
1331 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001332 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001333
1334 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001335
1336 rc = SMB2_sess_establish_session(sess_data);
1337out_put_spnego_key:
1338 key_invalidate(spnego_key);
1339 key_put(spnego_key);
1340out:
1341 sess_data->result = rc;
1342 sess_data->func = NULL;
1343 SMB2_sess_free_buffer(sess_data);
1344}
1345#else
1346static void
1347SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1348{
1349 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1350 sess_data->result = -EOPNOTSUPP;
1351 sess_data->func = NULL;
1352}
1353#endif
1354
Sachin Prabhu166cea42016-10-07 19:11:22 +01001355static void
1356SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1357
1358static void
1359SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1360{
1361 int rc;
1362 struct cifs_ses *ses = sess_data->ses;
1363 struct smb2_sess_setup_rsp *rsp = NULL;
1364 char *ntlmssp_blob = NULL;
1365 bool use_spnego = false; /* else use raw ntlmssp */
1366 u16 blob_length = 0;
1367
1368 /*
1369 * If memory allocation is successful, caller of this function
1370 * frees it.
1371 */
1372 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1373 if (!ses->ntlmssp) {
1374 rc = -ENOMEM;
1375 goto out_err;
1376 }
1377 ses->ntlmssp->sesskey_per_smbsess = true;
1378
1379 rc = SMB2_sess_alloc_buffer(sess_data);
1380 if (rc)
1381 goto out_err;
1382
1383 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1384 GFP_KERNEL);
1385 if (ntlmssp_blob == NULL) {
1386 rc = -ENOMEM;
1387 goto out;
1388 }
1389
1390 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1391 if (use_spnego) {
1392 /* BB eventually need to add this */
1393 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1394 rc = -EOPNOTSUPP;
1395 goto out;
1396 } else {
1397 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1398 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1399 }
1400 sess_data->iov[1].iov_base = ntlmssp_blob;
1401 sess_data->iov[1].iov_len = blob_length;
1402
1403 rc = SMB2_sess_sendreceive(sess_data);
1404 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1405
1406 /* If true, rc here is expected and not an error */
1407 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001408 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001409 rc = 0;
1410
1411 if (rc)
1412 goto out;
1413
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001414 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001415 le16_to_cpu(rsp->SecurityBufferOffset)) {
1416 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1417 le16_to_cpu(rsp->SecurityBufferOffset));
1418 rc = -EIO;
1419 goto out;
1420 }
1421 rc = decode_ntlmssp_challenge(rsp->Buffer,
1422 le16_to_cpu(rsp->SecurityBufferLength), ses);
1423 if (rc)
1424 goto out;
1425
1426 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1427
1428
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001429 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001430 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001431
1432out:
1433 kfree(ntlmssp_blob);
1434 SMB2_sess_free_buffer(sess_data);
1435 if (!rc) {
1436 sess_data->result = 0;
1437 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1438 return;
1439 }
1440out_err:
1441 kfree(ses->ntlmssp);
1442 ses->ntlmssp = NULL;
1443 sess_data->result = rc;
1444 sess_data->func = NULL;
1445}
1446
1447static void
1448SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1449{
1450 int rc;
1451 struct cifs_ses *ses = sess_data->ses;
1452 struct smb2_sess_setup_req *req;
1453 struct smb2_sess_setup_rsp *rsp = NULL;
1454 unsigned char *ntlmssp_blob = NULL;
1455 bool use_spnego = false; /* else use raw ntlmssp */
1456 u16 blob_length = 0;
1457
1458 rc = SMB2_sess_alloc_buffer(sess_data);
1459 if (rc)
1460 goto out;
1461
1462 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001463 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001464
1465 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1466 sess_data->nls_cp);
1467 if (rc) {
1468 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1469 goto out;
1470 }
1471
1472 if (use_spnego) {
1473 /* BB eventually need to add this */
1474 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1475 rc = -EOPNOTSUPP;
1476 goto out;
1477 }
1478 sess_data->iov[1].iov_base = ntlmssp_blob;
1479 sess_data->iov[1].iov_len = blob_length;
1480
1481 rc = SMB2_sess_sendreceive(sess_data);
1482 if (rc)
1483 goto out;
1484
1485 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1486
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001487 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001488 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001489
1490 rc = SMB2_sess_establish_session(sess_data);
1491out:
1492 kfree(ntlmssp_blob);
1493 SMB2_sess_free_buffer(sess_data);
1494 kfree(ses->ntlmssp);
1495 ses->ntlmssp = NULL;
1496 sess_data->result = rc;
1497 sess_data->func = NULL;
1498}
1499
1500static int
1501SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1502{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301503 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001504
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301505 type = smb2_select_sectype(ses->server, ses->sectype);
1506 cifs_dbg(FYI, "sess setup type %d\n", type);
1507 if (type == Unspecified) {
1508 cifs_dbg(VFS,
1509 "Unable to select appropriate authentication method!");
1510 return -EINVAL;
1511 }
1512
1513 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001514 case Kerberos:
1515 sess_data->func = SMB2_auth_kerberos;
1516 break;
1517 case RawNTLMSSP:
1518 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1519 break;
1520 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301521 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001522 return -EOPNOTSUPP;
1523 }
1524
1525 return 0;
1526}
1527
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001528int
1529SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1530 const struct nls_table *nls_cp)
1531{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001532 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001533 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001534 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001535
Joe Perchesf96637b2013-05-04 22:12:25 -05001536 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001537
Jeff Layton3534b852013-05-24 07:41:01 -04001538 if (!server) {
1539 WARN(1, "%s: server is NULL!\n", __func__);
1540 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001541 }
1542
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001543 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1544 if (!sess_data)
1545 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001546
1547 rc = SMB2_select_sec(ses, sess_data);
1548 if (rc)
1549 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001550 sess_data->xid = xid;
1551 sess_data->ses = ses;
1552 sess_data->buf0_type = CIFS_NO_BUFFER;
1553 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001554 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001555
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001556 /*
1557 * Initialize the session hash with the server one.
1558 */
1559 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1560 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001561
Sachin Prabhu166cea42016-10-07 19:11:22 +01001562 while (sess_data->func)
1563 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001564
Steve Frenchc721c382017-09-19 18:40:03 -05001565 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1566 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001567 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001568out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001569 kfree(sess_data);
1570 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001571}
1572
1573int
1574SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1575{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001576 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001577 struct smb2_logoff_req *req; /* response is also trivial struct */
1578 int rc = 0;
1579 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001580 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001581 unsigned int total_len;
1582 struct kvec iov[1];
1583 struct kvec rsp_iov;
1584 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001585
Joe Perchesf96637b2013-05-04 22:12:25 -05001586 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001587
1588 if (ses && (ses->server))
1589 server = ses->server;
1590 else
1591 return -EIO;
1592
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001593 /* no need to send SMB logoff if uid already closed due to reconnect */
1594 if (ses->need_reconnect)
1595 goto smb2_session_already_dead;
1596
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001597 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001598 if (rc)
1599 return rc;
1600
1601 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001602 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001603
1604 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1605 flags |= CIFS_TRANSFORM_REQ;
1606 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001607 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001608
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001609 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001610
1611 iov[0].iov_base = (char *)req;
1612 iov[0].iov_len = total_len;
1613
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001614 memset(&rqst, 0, sizeof(struct smb_rqst));
1615 rqst.rq_iov = iov;
1616 rqst.rq_nvec = 1;
1617
1618 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001619 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001620 /*
1621 * No tcon so can't do
1622 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1623 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001624
1625smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001626 return rc;
1627}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001628
1629static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1630{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001631 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001632}
1633
1634#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1635
Steve Frenchde9f68df2013-11-15 11:26:24 -06001636/* These are similar values to what Windows uses */
1637static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1638{
1639 tcon->max_chunks = 256;
1640 tcon->max_bytes_chunk = 1048576;
1641 tcon->max_bytes_copy = 16777216;
1642}
1643
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001644int
1645SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1646 struct cifs_tcon *tcon, const struct nls_table *cp)
1647{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001648 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001649 struct smb2_tree_connect_req *req;
1650 struct smb2_tree_connect_rsp *rsp = NULL;
1651 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001652 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001653 int rc = 0;
1654 int resp_buftype;
1655 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001656 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001657 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001658 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001659
Joe Perchesf96637b2013-05-04 22:12:25 -05001660 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001661
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001662 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001663 return -EIO;
1664
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001665 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1666 if (unc_path == NULL)
1667 return -ENOMEM;
1668
1669 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1670 unc_path_len *= 2;
1671 if (unc_path_len < 2) {
1672 kfree(unc_path);
1673 return -EINVAL;
1674 }
1675
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001676 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001677 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001678 atomic_set(&tcon->num_remote_opens, 0);
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001679 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1680 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001681 if (rc) {
1682 kfree(unc_path);
1683 return rc;
1684 }
1685
Steve French5a77e752018-05-09 17:43:08 -05001686 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001687 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001688
1689 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001690 /* 1 for pad */
1691 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001692
1693 /* Testing shows that buffer offset must be at location of Buffer[0] */
1694 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001695 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001696 req->PathLength = cpu_to_le16(unc_path_len - 2);
1697 iov[1].iov_base = unc_path;
1698 iov[1].iov_len = unc_path_len;
1699
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001700 /*
1701 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1702 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
Steve French8c11a602019-03-22 22:31:17 -05001703 * (Samba servers don't always set the flag so also check if null user)
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001704 */
Steve French6188f282018-03-13 02:29:36 -05001705 if ((ses->server->dialect == SMB311_PROT_ID) &&
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001706 !smb3_encryption_required(tcon) &&
Steve French8c11a602019-03-22 22:31:17 -05001707 !(ses->session_flags &
1708 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1709 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
Steve French6188f282018-03-13 02:29:36 -05001710 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1711
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001712 memset(&rqst, 0, sizeof(struct smb_rqst));
1713 rqst.rq_iov = iov;
1714 rqst.rq_nvec = 2;
1715
Steve French4fe75c42019-02-14 01:19:02 -06001716 /* Need 64 for max size write so ask for more in case not there yet */
1717 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1718
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001719 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001720 cifs_small_buf_release(req);
1721 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001722 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001723 if (rc != 0) {
1724 if (tcon) {
1725 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1726 tcon->need_reconnect = true;
1727 }
1728 goto tcon_error_exit;
1729 }
1730
Christophe JAILLETcd123002017-05-12 17:59:32 +02001731 switch (rsp->ShareType) {
1732 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001733 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001734 break;
1735 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001736 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001737 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001738 break;
1739 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001740 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001741 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001742 break;
1743 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001744 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001745 rc = -EOPNOTSUPP;
1746 goto tcon_error_exit;
1747 }
1748
1749 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001750 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001751 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1752 tcon->tidStatus = CifsGood;
1753 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001754 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001755 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001756
1757 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1758 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001759 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001760
1761 if (tcon->seal &&
1762 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1763 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1764
Steve Frenchde9f68df2013-11-15 11:26:24 -06001765 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001766 if (tcon->ses->server->ops->validate_negotiate)
1767 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001768tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001769
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001770 free_rsp_buf(resp_buftype, rsp);
1771 kfree(unc_path);
1772 return rc;
1773
1774tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001775 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001776 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001777 }
1778 goto tcon_exit;
1779}
1780
1781int
1782SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1783{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001784 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001785 struct smb2_tree_disconnect_req *req; /* response is trivial */
1786 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001787 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001788 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001789 unsigned int total_len;
1790 struct kvec iov[1];
1791 struct kvec rsp_iov;
1792 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001793
Joe Perchesf96637b2013-05-04 22:12:25 -05001794 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001795
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001796 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001797 return -EIO;
1798
1799 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1800 return 0;
1801
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001802 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1803 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001804 if (rc)
1805 return rc;
1806
Steve French5a77e752018-05-09 17:43:08 -05001807 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001808 flags |= CIFS_TRANSFORM_REQ;
1809
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001810 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001811
1812 iov[0].iov_base = (char *)req;
1813 iov[0].iov_len = total_len;
1814
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001815 memset(&rqst, 0, sizeof(struct smb_rqst));
1816 rqst.rq_iov = iov;
1817 rqst.rq_nvec = 1;
1818
1819 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001820 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001821 if (rc)
1822 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1823
1824 return rc;
1825}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001826
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001827
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001828static struct create_durable *
1829create_durable_buf(void)
1830{
1831 struct create_durable *buf;
1832
1833 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1834 if (!buf)
1835 return NULL;
1836
1837 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001838 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001839 buf->ccontext.DataLength = cpu_to_le32(16);
1840 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1841 (struct create_durable, Name));
1842 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001843 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001844 buf->Name[0] = 'D';
1845 buf->Name[1] = 'H';
1846 buf->Name[2] = 'n';
1847 buf->Name[3] = 'Q';
1848 return buf;
1849}
1850
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001851static struct create_durable *
1852create_reconnect_durable_buf(struct cifs_fid *fid)
1853{
1854 struct create_durable *buf;
1855
1856 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1857 if (!buf)
1858 return NULL;
1859
1860 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1861 (struct create_durable, Data));
1862 buf->ccontext.DataLength = cpu_to_le32(16);
1863 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1864 (struct create_durable, Name));
1865 buf->ccontext.NameLength = cpu_to_le16(4);
1866 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1867 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001868 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001869 buf->Name[0] = 'D';
1870 buf->Name[1] = 'H';
1871 buf->Name[2] = 'n';
1872 buf->Name[3] = 'C';
1873 return buf;
1874}
1875
Steve French89a5bfa2019-07-18 17:22:18 -05001876static void
1877parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1878{
1879 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1880
1881 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1882 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1883 buf->IndexNumber = pdisk_id->DiskFileId;
1884}
1885
1886void
1887smb2_parse_contexts(struct TCP_Server_Info *server,
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +10001888 struct smb2_create_rsp *rsp,
Steve French89a5bfa2019-07-18 17:22:18 -05001889 unsigned int *epoch, char *lease_key, __u8 *oplock,
1890 struct smb2_file_all_info *buf)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001891{
1892 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001893 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001894 unsigned int next;
1895 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001896 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001897
Steve French89a5bfa2019-07-18 17:22:18 -05001898 *oplock = 0;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001899 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001900 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001901 cc = (struct create_context *)data_offset;
Steve French89a5bfa2019-07-18 17:22:18 -05001902
1903 /* Initialize inode number to 0 in case no valid data in qfid context */
1904 if (buf)
1905 buf->IndexNumber = 0;
1906
Justin Maggarddeb7def2016-02-09 15:52:08 -08001907 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001908 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001909 if (le16_to_cpu(cc->NameLength) == 4 &&
Steve French89a5bfa2019-07-18 17:22:18 -05001910 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1911 *oplock = server->ops->parse_lease_buf(cc, epoch,
1912 lease_key);
1913 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1914 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1915 parse_query_id_ctxt(cc, buf);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001916
1917 next = le32_to_cpu(cc->Next);
1918 if (!next)
1919 break;
1920 remaining -= next;
1921 cc = (struct create_context *)((char *)cc + next);
1922 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001923
Steve French89a5bfa2019-07-18 17:22:18 -05001924 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1925 *oplock = rsp->OplockLevel;
1926
1927 return;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001928}
1929
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001930static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001931add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02001932 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001933{
1934 struct smb2_create_req *req = iov[0].iov_base;
1935 unsigned int num = *num_iovec;
1936
Stefano Brivio729c0c92018-07-05 15:10:02 +02001937 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001938 if (iov[num].iov_base == NULL)
1939 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001940 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001941 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1942 if (!req->CreateContextsOffset)
1943 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001944 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001945 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001946 le32_add_cpu(&req->CreateContextsLength,
1947 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001948 *num_iovec = num + 1;
1949 return 0;
1950}
1951
Steve Frenchb56eae42015-11-03 09:26:27 -06001952static struct create_durable_v2 *
Steve Frenchca567eb2019-03-29 16:31:07 -05001953create_durable_v2_buf(struct cifs_open_parms *oparms)
Steve Frenchb56eae42015-11-03 09:26:27 -06001954{
Steve Frenchca567eb2019-03-29 16:31:07 -05001955 struct cifs_fid *pfid = oparms->fid;
Steve Frenchb56eae42015-11-03 09:26:27 -06001956 struct create_durable_v2 *buf;
1957
1958 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1959 if (!buf)
1960 return NULL;
1961
1962 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1963 (struct create_durable_v2, dcontext));
1964 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1965 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1966 (struct create_durable_v2, Name));
1967 buf->ccontext.NameLength = cpu_to_le16(4);
1968
Steve Frenchca567eb2019-03-29 16:31:07 -05001969 /*
1970 * NB: Handle timeout defaults to 0, which allows server to choose
1971 * (most servers default to 120 seconds) and most clients default to 0.
1972 * This can be overridden at mount ("handletimeout=") if the user wants
1973 * a different persistent (or resilient) handle timeout for all opens
1974 * opens on a particular SMB3 mount.
1975 */
1976 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
Steve Frenchb56eae42015-11-03 09:26:27 -06001977 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001978 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001979 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1980
1981 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1982 buf->Name[0] = 'D';
1983 buf->Name[1] = 'H';
1984 buf->Name[2] = '2';
1985 buf->Name[3] = 'Q';
1986 return buf;
1987}
1988
1989static struct create_durable_handle_reconnect_v2 *
1990create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1991{
1992 struct create_durable_handle_reconnect_v2 *buf;
1993
1994 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1995 GFP_KERNEL);
1996 if (!buf)
1997 return NULL;
1998
1999 buf->ccontext.DataOffset =
2000 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2001 dcontext));
2002 buf->ccontext.DataLength =
2003 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2004 buf->ccontext.NameOffset =
2005 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2006 Name));
2007 buf->ccontext.NameLength = cpu_to_le16(4);
2008
2009 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2010 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2011 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2012 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2013
2014 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2015 buf->Name[0] = 'D';
2016 buf->Name[1] = 'H';
2017 buf->Name[2] = '2';
2018 buf->Name[3] = 'C';
2019 return buf;
2020}
2021
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002022static int
Steve Frenchb56eae42015-11-03 09:26:27 -06002023add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002024 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002025{
2026 struct smb2_create_req *req = iov[0].iov_base;
2027 unsigned int num = *num_iovec;
2028
Steve Frenchca567eb2019-03-29 16:31:07 -05002029 iov[num].iov_base = create_durable_v2_buf(oparms);
Steve Frenchb56eae42015-11-03 09:26:27 -06002030 if (iov[num].iov_base == NULL)
2031 return -ENOMEM;
2032 iov[num].iov_len = sizeof(struct create_durable_v2);
2033 if (!req->CreateContextsOffset)
2034 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002035 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002036 iov[1].iov_len);
2037 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002038 *num_iovec = num + 1;
2039 return 0;
2040}
2041
2042static int
2043add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2044 struct cifs_open_parms *oparms)
2045{
2046 struct smb2_create_req *req = iov[0].iov_base;
2047 unsigned int num = *num_iovec;
2048
2049 /* indicate that we don't need to relock the file */
2050 oparms->reconnect = false;
2051
2052 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2053 if (iov[num].iov_base == NULL)
2054 return -ENOMEM;
2055 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2056 if (!req->CreateContextsOffset)
2057 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002058 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002059 iov[1].iov_len);
2060 le32_add_cpu(&req->CreateContextsLength,
2061 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002062 *num_iovec = num + 1;
2063 return 0;
2064}
2065
2066static int
2067add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2068 struct cifs_open_parms *oparms, bool use_persistent)
2069{
2070 struct smb2_create_req *req = iov[0].iov_base;
2071 unsigned int num = *num_iovec;
2072
2073 if (use_persistent) {
2074 if (oparms->reconnect)
2075 return add_durable_reconnect_v2_context(iov, num_iovec,
2076 oparms);
2077 else
2078 return add_durable_v2_context(iov, num_iovec, oparms);
2079 }
2080
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002081 if (oparms->reconnect) {
2082 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2083 /* indicate that we don't need to relock the file */
2084 oparms->reconnect = false;
2085 } else
2086 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002087 if (iov[num].iov_base == NULL)
2088 return -ENOMEM;
2089 iov[num].iov_len = sizeof(struct create_durable);
2090 if (!req->CreateContextsOffset)
2091 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002092 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002093 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08002094 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002095 *num_iovec = num + 1;
2096 return 0;
2097}
2098
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002099/* See MS-SMB2 2.2.13.2.7 */
2100static struct crt_twarp_ctxt *
2101create_twarp_buf(__u64 timewarp)
2102{
2103 struct crt_twarp_ctxt *buf;
2104
2105 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2106 if (!buf)
2107 return NULL;
2108
2109 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2110 (struct crt_twarp_ctxt, Timestamp));
2111 buf->ccontext.DataLength = cpu_to_le32(8);
2112 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2113 (struct crt_twarp_ctxt, Name));
2114 buf->ccontext.NameLength = cpu_to_le16(4);
2115 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2116 buf->Name[0] = 'T';
2117 buf->Name[1] = 'W';
2118 buf->Name[2] = 'r';
2119 buf->Name[3] = 'p';
2120 buf->Timestamp = cpu_to_le64(timewarp);
2121 return buf;
2122}
2123
2124/* See MS-SMB2 2.2.13.2.7 */
2125static int
2126add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2127{
2128 struct smb2_create_req *req = iov[0].iov_base;
2129 unsigned int num = *num_iovec;
2130
2131 iov[num].iov_base = create_twarp_buf(timewarp);
2132 if (iov[num].iov_base == NULL)
2133 return -ENOMEM;
2134 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2135 if (!req->CreateContextsOffset)
2136 req->CreateContextsOffset = cpu_to_le32(
2137 sizeof(struct smb2_create_req) +
2138 iov[num - 1].iov_len);
2139 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2140 *num_iovec = num + 1;
2141 return 0;
2142}
2143
Steve Frenchff2a09e2019-07-06 14:41:38 -05002144static struct crt_query_id_ctxt *
2145create_query_id_buf(void)
2146{
2147 struct crt_query_id_ctxt *buf;
2148
2149 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2150 if (!buf)
2151 return NULL;
2152
2153 buf->ccontext.DataOffset = cpu_to_le16(0);
2154 buf->ccontext.DataLength = cpu_to_le32(0);
2155 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2156 (struct crt_query_id_ctxt, Name));
2157 buf->ccontext.NameLength = cpu_to_le16(4);
2158 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2159 buf->Name[0] = 'Q';
2160 buf->Name[1] = 'F';
2161 buf->Name[2] = 'i';
2162 buf->Name[3] = 'd';
2163 return buf;
2164}
2165
2166/* See MS-SMB2 2.2.13.2.9 */
2167static int
2168add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2169{
2170 struct smb2_create_req *req = iov[0].iov_base;
2171 unsigned int num = *num_iovec;
2172
2173 iov[num].iov_base = create_query_id_buf();
2174 if (iov[num].iov_base == NULL)
2175 return -ENOMEM;
2176 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2177 if (!req->CreateContextsOffset)
2178 req->CreateContextsOffset = cpu_to_le32(
2179 sizeof(struct smb2_create_req) +
2180 iov[num - 1].iov_len);
2181 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2182 *num_iovec = num + 1;
2183 return 0;
2184}
2185
Aurelien Aptelf0712922017-02-22 14:47:17 +01002186static int
2187alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2188 const char *treename, const __le16 *path)
2189{
2190 int treename_len, path_len;
2191 struct nls_table *cp;
2192 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2193
2194 /*
2195 * skip leading "\\"
2196 */
2197 treename_len = strlen(treename);
2198 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2199 return -EINVAL;
2200
2201 treename += 2;
2202 treename_len -= 2;
2203
2204 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2205
2206 /*
2207 * make room for one path separator between the treename and
2208 * path
2209 */
2210 *out_len = treename_len + 1 + path_len;
2211
2212 /*
2213 * final path needs to be null-terminated UTF16 with a
2214 * size aligned to 8
2215 */
2216
2217 *out_size = roundup((*out_len+1)*2, 8);
2218 *out_path = kzalloc(*out_size, GFP_KERNEL);
2219 if (!*out_path)
2220 return -ENOMEM;
2221
2222 cp = load_nls_default();
2223 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2224 UniStrcat(*out_path, sep);
2225 UniStrcat(*out_path, path);
2226 unload_nls(cp);
2227
2228 return 0;
2229}
2230
Steve Frenchbea851b2018-06-14 21:56:32 -05002231int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2232 umode_t mode, struct cifs_tcon *tcon,
2233 const char *full_path,
2234 struct cifs_sb_info *cifs_sb)
2235{
2236 struct smb_rqst rqst;
2237 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002238 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002239 struct cifs_ses *ses = tcon->ses;
2240 struct kvec iov[3]; /* make sure at least one for each open context */
2241 struct kvec rsp_iov = {NULL, 0};
2242 int resp_buftype;
2243 int uni_path_len;
2244 __le16 *copy_path = NULL;
2245 int copy_size;
2246 int rc = 0;
2247 unsigned int n_iov = 2;
2248 __u32 file_attributes = 0;
2249 char *pc_buf = NULL;
2250 int flags = 0;
2251 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002252 __le16 *utf16_path = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002253
2254 cifs_dbg(FYI, "mkdir\n");
2255
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002256 /* resource #1: path allocation */
2257 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2258 if (!utf16_path)
2259 return -ENOMEM;
2260
YueHaibing29cbfa12018-12-18 02:51:51 +00002261 if (!ses || !(ses->server)) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002262 rc = -EIO;
2263 goto err_free_path;
2264 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002265
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002266 /* resource #2: request */
Steve Frenchbea851b2018-06-14 21:56:32 -05002267 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002268 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002269 goto err_free_path;
2270
Steve Frenchbea851b2018-06-14 21:56:32 -05002271
2272 if (smb3_encryption_required(tcon))
2273 flags |= CIFS_TRANSFORM_REQ;
2274
Steve Frenchbea851b2018-06-14 21:56:32 -05002275 req->ImpersonationLevel = IL_IMPERSONATION;
2276 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2277 /* File attributes ignored on open (used in create though) */
2278 req->FileAttributes = cpu_to_le32(file_attributes);
2279 req->ShareAccess = FILE_SHARE_ALL_LE;
2280 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2281 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2282
2283 iov[0].iov_base = (char *)req;
2284 /* -1 since last byte is buf[0] which is sent below (path) */
2285 iov[0].iov_len = total_len - 1;
2286
2287 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2288
2289 /* [MS-SMB2] 2.2.13 NameOffset:
2290 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2291 * the SMB2 header, the file name includes a prefix that will
2292 * be processed during DFS name normalization as specified in
2293 * section 3.3.5.9. Otherwise, the file name is relative to
2294 * the share that is identified by the TreeId in the SMB2
2295 * header.
2296 */
2297 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2298 int name_len;
2299
2300 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2301 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2302 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002303 tcon->treeName, utf16_path);
2304 if (rc)
2305 goto err_free_req;
2306
Steve Frenchbea851b2018-06-14 21:56:32 -05002307 req->NameLength = cpu_to_le16(name_len * 2);
2308 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002309 /* free before overwriting resource */
2310 kfree(utf16_path);
2311 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002312 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002313 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002314 /* MUST set path len (NameLength) to 0 opening root of share */
2315 req->NameLength = cpu_to_le16(uni_path_len - 2);
2316 if (uni_path_len % 8 != 0) {
2317 copy_size = roundup(uni_path_len, 8);
2318 copy_path = kzalloc(copy_size, GFP_KERNEL);
2319 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002320 rc = -ENOMEM;
2321 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002322 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002323 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002324 uni_path_len);
2325 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002326 /* free before overwriting resource */
2327 kfree(utf16_path);
2328 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002329 }
2330 }
2331
2332 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002333 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002334 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2335
2336 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002337 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002338 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002339 if (rc)
2340 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002341 pc_buf = iov[n_iov-1].iov_base;
2342 }
2343
2344
2345 memset(&rqst, 0, sizeof(struct smb_rqst));
2346 rqst.rq_iov = iov;
2347 rqst.rq_nvec = n_iov;
2348
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002349 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2350 FILE_WRITE_ATTRIBUTES);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002351 /* resource #4: response buffer */
2352 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2353 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002354 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2355 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002356 CREATE_NOT_FILE,
2357 FILE_WRITE_ATTRIBUTES, rc);
2358 goto err_free_rsp_buf;
2359 }
2360
2361 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2362 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2363 ses->Suid, CREATE_NOT_FILE,
2364 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002365
2366 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2367
2368 /* Eventually save off posix specific response info and timestaps */
2369
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002370err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002371 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002372 kfree(pc_buf);
2373err_free_req:
2374 cifs_small_buf_release(req);
2375err_free_path:
2376 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002377 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002378}
Steve Frenchbea851b2018-06-14 21:56:32 -05002379
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002380int
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002381SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2382 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002383{
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002384 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002385 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002386 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002387 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002388 int copy_size;
2389 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002390 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002391 struct kvec *iov = rqst->rq_iov;
2392 __le16 *copy_path;
2393 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002394
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002395 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002396 if (rc)
2397 return rc;
2398
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002399 iov[0].iov_base = (char *)req;
2400 /* -1 since last byte is buf[0] which is sent below (path) */
2401 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002402
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002403 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002404 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002405 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2406 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002407
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002408 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002409 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002410 /* File attributes ignored on open (used in create though) */
2411 req->FileAttributes = cpu_to_le32(file_attributes);
2412 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002413 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2414 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002415 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002416
2417 /* [MS-SMB2] 2.2.13 NameOffset:
2418 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2419 * the SMB2 header, the file name includes a prefix that will
2420 * be processed during DFS name normalization as specified in
2421 * section 3.3.5.9. Otherwise, the file name is relative to
2422 * the share that is identified by the TreeId in the SMB2
2423 * header.
2424 */
2425 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2426 int name_len;
2427
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002428 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002429 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2430 &name_len,
2431 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002432 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002433 return rc;
2434 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002435 uni_path_len = copy_size;
2436 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002437 } else {
2438 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2439 /* MUST set path len (NameLength) to 0 opening root of share */
2440 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002441 copy_size = uni_path_len;
2442 if (copy_size % 8 != 0)
2443 copy_size = roundup(copy_size, 8);
2444 copy_path = kzalloc(copy_size, GFP_KERNEL);
2445 if (!copy_path)
2446 return -ENOMEM;
2447 memcpy((char *)copy_path, (const char *)path,
2448 uni_path_len);
2449 uni_path_len = copy_size;
2450 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002451 }
2452
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002453 iov[1].iov_len = uni_path_len;
2454 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002455
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002456 if (!server->oplocks)
2457 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2458
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002459 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002460 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2461 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002462 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2463 (oparms->create_options & CREATE_NOT_FILE))
2464 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002465 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002466 rc = add_lease_context(server, iov, &n_iov,
2467 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002468 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002469 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002470 }
2471
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002472 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2473 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002474 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002475 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002476 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002477 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002478 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002479 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002480
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002481 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002482 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002483 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002484 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002485 }
2486
Steve Frenchce558b02018-05-31 19:16:54 -05002487 if (tcon->posix_extensions) {
2488 if (n_iov > 2) {
2489 struct create_context *ccontext =
2490 (struct create_context *)iov[n_iov-1].iov_base;
2491 ccontext->Next =
2492 cpu_to_le32(iov[n_iov-1].iov_len);
2493 }
2494
2495 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002496 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002497 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002498 }
Steve Frenchce558b02018-05-31 19:16:54 -05002499
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002500 if (tcon->snapshot_time) {
2501 cifs_dbg(FYI, "adding snapshot context\n");
2502 if (n_iov > 2) {
2503 struct create_context *ccontext =
2504 (struct create_context *)iov[n_iov-1].iov_base;
2505 ccontext->Next =
2506 cpu_to_le32(iov[n_iov-1].iov_len);
2507 }
2508
2509 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2510 if (rc)
2511 return rc;
2512 }
2513
Steve Frenchff2a09e2019-07-06 14:41:38 -05002514 if (n_iov > 2) {
2515 struct create_context *ccontext =
2516 (struct create_context *)iov[n_iov-1].iov_base;
2517 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2518 }
2519 add_query_id_context(iov, &n_iov);
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002520
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002521 rqst->rq_nvec = n_iov;
2522 return 0;
2523}
2524
2525/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2526 * All other vectors are freed by kfree().
2527 */
2528void
2529SMB2_open_free(struct smb_rqst *rqst)
2530{
2531 int i;
2532
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002533 if (rqst && rqst->rq_iov) {
2534 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2535 for (i = 1; i < rqst->rq_nvec; i++)
2536 if (rqst->rq_iov[i].iov_base != smb2_padding)
2537 kfree(rqst->rq_iov[i].iov_base);
2538 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002539}
2540
2541int
2542SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2543 __u8 *oplock, struct smb2_file_all_info *buf,
2544 struct kvec *err_iov, int *buftype)
2545{
2546 struct smb_rqst rqst;
2547 struct smb2_create_rsp *rsp = NULL;
2548 struct TCP_Server_Info *server;
2549 struct cifs_tcon *tcon = oparms->tcon;
2550 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002551 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002552 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002553 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002554 int rc = 0;
2555 int flags = 0;
2556
2557 cifs_dbg(FYI, "create/open\n");
2558 if (ses && (ses->server))
2559 server = ses->server;
2560 else
2561 return -EIO;
2562
2563 if (smb3_encryption_required(tcon))
2564 flags |= CIFS_TRANSFORM_REQ;
2565
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002566 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002567 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002568 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002569 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002570
2571 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2572 if (rc)
2573 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002574
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002575 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2576 oparms->create_options, oparms->desired_access);
2577
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002578 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002579 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002580 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002581
2582 if (rc != 0) {
2583 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002584 if (err_iov && rsp) {
2585 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002586 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002587 resp_buftype = CIFS_NO_BUFFER;
2588 rsp = NULL;
2589 }
Steve French28d59362018-05-30 21:42:34 -05002590 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2591 oparms->create_options, oparms->desired_access, rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002592 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002593 } else
2594 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2595 ses->Suid, oparms->create_options,
2596 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002597
Steve Frenchfae80442018-10-19 17:14:32 -05002598 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002599 oparms->fid->persistent_fid = rsp->PersistentFileId;
2600 oparms->fid->volatile_fid = rsp->VolatileFileId;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002601#ifdef CONFIG_CIFS_DEBUG2
2602 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2603#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002604
2605 if (buf) {
2606 memcpy(buf, &rsp->CreationTime, 32);
2607 buf->AllocationSize = rsp->AllocationSize;
2608 buf->EndOfFile = rsp->EndofFile;
2609 buf->Attributes = rsp->FileAttributes;
2610 buf->NumberOfLinks = cpu_to_le32(1);
2611 buf->DeletePending = 0;
2612 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002613
Steve French89a5bfa2019-07-18 17:22:18 -05002614
2615 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2616 oparms->fid->lease_key, oplock, buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002617creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002618 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002619 free_rsp_buf(resp_buftype, rsp);
2620 return rc;
2621}
2622
Steve French4a72daf2013-06-25 00:20:49 -05002623int
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002624SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2625 u64 persistent_fid, u64 volatile_fid, u32 opcode,
Steve French153322f2019-03-28 22:32:49 -05002626 bool is_fsctl, char *in_data, u32 indatalen,
2627 __u32 max_response_size)
Steve French4a72daf2013-06-25 00:20:49 -05002628{
2629 struct smb2_ioctl_req *req;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002630 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002631 unsigned int total_len;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002632 int rc;
Long Li2c87d6a2019-05-15 14:09:05 -07002633 char *in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002634
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002635 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002636 if (rc)
2637 return rc;
2638
Long Li2c87d6a2019-05-15 14:09:05 -07002639 if (indatalen) {
2640 /*
2641 * indatalen is usually small at a couple of bytes max, so
2642 * just allocate through generic pool
2643 */
YueHaibingd81f0972019-06-01 03:31:10 +00002644 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
Long Li2c87d6a2019-05-15 14:09:05 -07002645 if (!in_data_buf) {
2646 cifs_small_buf_release(req);
2647 return -ENOMEM;
2648 }
Long Li2c87d6a2019-05-15 14:09:05 -07002649 }
2650
Steve French4a72daf2013-06-25 00:20:49 -05002651 req->CtlCode = cpu_to_le32(opcode);
2652 req->PersistentFileId = persistent_fid;
2653 req->VolatileFileId = volatile_fid;
2654
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002655 iov[0].iov_base = (char *)req;
2656 /*
2657 * If no input data, the size of ioctl struct in
2658 * protocol spec still includes a 1 byte data buffer,
2659 * but if input data passed to ioctl, we do not
2660 * want to double count this, so we do not send
2661 * the dummy one byte of data in iovec[0] if sending
2662 * input data (in iovec[1]).
2663 */
Steve French4a72daf2013-06-25 00:20:49 -05002664 if (indatalen) {
2665 req->InputCount = cpu_to_le32(indatalen);
2666 /* do not set InputOffset if no input data */
2667 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002668 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002669 rqst->rq_nvec = 2;
2670 iov[0].iov_len = total_len - 1;
Long Li2c87d6a2019-05-15 14:09:05 -07002671 iov[1].iov_base = in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002672 iov[1].iov_len = indatalen;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002673 } else {
2674 rqst->rq_nvec = 1;
2675 iov[0].iov_len = total_len;
2676 }
Steve French4a72daf2013-06-25 00:20:49 -05002677
2678 req->OutputOffset = 0;
2679 req->OutputCount = 0; /* MBZ */
2680
2681 /*
Steve French153322f2019-03-28 22:32:49 -05002682 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2683 * We Could increase default MaxOutputResponse, but that could require
2684 * more credits. Windows typically sets this smaller, but for some
Steve French4a72daf2013-06-25 00:20:49 -05002685 * ioctls it may be useful to allow server to send more. No point
2686 * limiting what the server can send as long as fits in one credit
Steve French153322f2019-03-28 22:32:49 -05002687 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2688 * to increase this limit up in the future.
2689 * Note that for snapshot queries that servers like Azure expect that
2690 * the first query be minimal size (and just used to get the number/size
2691 * of previous versions) so response size must be specified as EXACTLY
2692 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2693 * of eight bytes. Currently that is the only case where we set max
2694 * response size smaller.
Steve French4a72daf2013-06-25 00:20:49 -05002695 */
Steve French153322f2019-03-28 22:32:49 -05002696 req->MaxOutputResponse = cpu_to_le32(max_response_size);
Steve French4a72daf2013-06-25 00:20:49 -05002697
2698 if (is_fsctl)
2699 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2700 else
2701 req->Flags = 0;
2702
Steve French4587eee2017-10-25 15:58:31 -05002703 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2704 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002705 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002706
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002707 return 0;
2708}
2709
2710void
2711SMB2_ioctl_free(struct smb_rqst *rqst)
2712{
Murphy Zhou6457c202019-05-23 12:12:43 +08002713 int i;
Long Li2c87d6a2019-05-15 14:09:05 -07002714 if (rqst && rqst->rq_iov) {
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002715 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Murphy Zhou6457c202019-05-23 12:12:43 +08002716 for (i = 1; i < rqst->rq_nvec; i++)
2717 if (rqst->rq_iov[i].iov_base != smb2_padding)
2718 kfree(rqst->rq_iov[i].iov_base);
Long Li2c87d6a2019-05-15 14:09:05 -07002719 }
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002720}
2721
Steve French153322f2019-03-28 22:32:49 -05002722
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002723/*
2724 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2725 */
2726int
2727SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2728 u64 volatile_fid, u32 opcode, bool is_fsctl,
Steve French153322f2019-03-28 22:32:49 -05002729 char *in_data, u32 indatalen, u32 max_out_data_len,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002730 char **out_data, u32 *plen /* returned data len */)
2731{
2732 struct smb_rqst rqst;
2733 struct smb2_ioctl_rsp *rsp = NULL;
2734 struct cifs_ses *ses;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002735 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002736 struct kvec rsp_iov = {NULL, 0};
2737 int resp_buftype = CIFS_NO_BUFFER;
2738 int rc = 0;
2739 int flags = 0;
2740
2741 cifs_dbg(FYI, "SMB2 IOCTL\n");
2742
2743 if (out_data != NULL)
2744 *out_data = NULL;
2745
2746 /* zero out returned data len, in case of error */
2747 if (plen)
2748 *plen = 0;
2749
2750 if (tcon)
2751 ses = tcon->ses;
2752 else
2753 return -EIO;
2754
2755 if (!ses || !(ses->server))
2756 return -EIO;
2757
2758 if (smb3_encryption_required(tcon))
2759 flags |= CIFS_TRANSFORM_REQ;
2760
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002761 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002762 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002763 rqst.rq_iov = iov;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002764 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002765
Steve French153322f2019-03-28 22:32:49 -05002766 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2767 is_fsctl, in_data, indatalen, max_out_data_len);
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002768 if (rc)
2769 goto ioctl_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002770
2771 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002772 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002773 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002774
Steve Frencheccb4422018-05-17 21:16:55 -05002775 if (rc != 0)
2776 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2777 ses->Suid, 0, opcode, rc);
2778
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002779 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
Steve French8e353102015-03-26 19:47:02 -05002780 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002781 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002782 } else if (rc == -EINVAL) {
2783 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2784 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002785 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002786 goto ioctl_exit;
2787 }
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002788 } else if (rc == -E2BIG) {
2789 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2790 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2791 goto ioctl_exit;
2792 }
Steve French4a72daf2013-06-25 00:20:49 -05002793 }
2794
2795 /* check if caller wants to look at return data or just return rc */
2796 if ((plen == NULL) || (out_data == NULL))
2797 goto ioctl_exit;
2798
2799 *plen = le32_to_cpu(rsp->OutputCount);
2800
2801 /* We check for obvious errors in the output buffer length and offset */
2802 if (*plen == 0)
2803 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002804 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Steve French4a72daf2013-06-25 00:20:49 -05002805 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2806 *plen = 0;
2807 rc = -EIO;
2808 goto ioctl_exit;
2809 }
2810
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002811 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Steve French4a72daf2013-06-25 00:20:49 -05002812 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2813 le32_to_cpu(rsp->OutputOffset));
2814 *plen = 0;
2815 rc = -EIO;
2816 goto ioctl_exit;
2817 }
2818
YueHaibingd034fee2018-09-10 01:33:06 +00002819 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2820 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05002821 if (*out_data == NULL) {
2822 rc = -ENOMEM;
2823 goto ioctl_exit;
2824 }
2825
Steve French4a72daf2013-06-25 00:20:49 -05002826ioctl_exit:
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002827 SMB2_ioctl_free(&rqst);
Steve French4a72daf2013-06-25 00:20:49 -05002828 free_rsp_buf(resp_buftype, rsp);
2829 return rc;
2830}
2831
Steve French64a5cfa2013-10-14 15:31:32 -05002832/*
2833 * Individual callers to ioctl worker function follow
2834 */
2835
2836int
2837SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2838 u64 persistent_fid, u64 volatile_fid)
2839{
2840 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002841 struct compress_ioctl fsctl_input;
2842 char *ret_data = NULL;
2843
2844 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002845 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002846
2847 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2848 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2849 (char *)&fsctl_input /* data input */,
Steve French153322f2019-03-28 22:32:49 -05002850 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2851 &ret_data /* out data */, NULL);
Steve French64a5cfa2013-10-14 15:31:32 -05002852
2853 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002854
2855 return rc;
2856}
2857
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002858int
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002859SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2860 u64 persistent_fid, u64 volatile_fid)
2861{
2862 struct smb2_close_req *req;
2863 struct kvec *iov = rqst->rq_iov;
2864 unsigned int total_len;
2865 int rc;
2866
2867 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2868 if (rc)
2869 return rc;
2870
2871 req->PersistentFileId = persistent_fid;
2872 req->VolatileFileId = volatile_fid;
2873 iov[0].iov_base = (char *)req;
2874 iov[0].iov_len = total_len;
2875
2876 return 0;
2877}
2878
2879void
2880SMB2_close_free(struct smb_rqst *rqst)
2881{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002882 if (rqst && rqst->rq_iov)
2883 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002884}
2885
2886int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002887SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2888 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002889{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002890 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002891 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002892 struct cifs_ses *ses = tcon->ses;
2893 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002894 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01002895 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002896 int rc = 0;
2897
Joe Perchesf96637b2013-05-04 22:12:25 -05002898 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002899
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002900 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002901 return -EIO;
2902
Steve French5a77e752018-05-09 17:43:08 -05002903 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002904 flags |= CIFS_TRANSFORM_REQ;
2905
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002906 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002907 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002908 rqst.rq_iov = iov;
2909 rqst.rq_nvec = 1;
2910
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002911 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2912 if (rc)
2913 goto close_exit;
2914
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002915 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002916 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002917
2918 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002919 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002920 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2921 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002922 goto close_exit;
2923 }
2924
Steve Frenchfae80442018-10-19 17:14:32 -05002925 atomic_dec(&tcon->num_remote_opens);
2926
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002927 /* BB FIXME - decode close response, update inode for caching */
2928
2929close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002930 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002931 free_rsp_buf(resp_buftype, rsp);
2932 return rc;
2933}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002934
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002935int
2936SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2937 u64 persistent_fid, u64 volatile_fid)
2938{
2939 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2940}
2941
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002942int
2943smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2944 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002945{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002946 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002947 char *end_of_smb = smb_len + (char *)iov->iov_base;
2948 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002949 char *end_of_buf = begin_of_buf + buffer_length;
2950
2951
2952 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002953 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2954 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002955 return -EINVAL;
2956 }
2957
2958 /* check if beyond RFC1001 maximum length */
2959 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002960 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2961 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002962 return -EINVAL;
2963 }
2964
2965 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002966 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002967 return -EINVAL;
2968 }
2969
2970 return 0;
2971}
2972
2973/*
2974 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2975 * Caller must free buffer.
2976 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002977int
2978smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2979 struct kvec *iov, unsigned int minbufsize,
2980 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002981{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002982 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002983 int rc;
2984
2985 if (!data)
2986 return -EINVAL;
2987
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002988 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002989 if (rc)
2990 return rc;
2991
2992 memcpy(data, begin_of_buf, buffer_length);
2993
2994 return 0;
2995}
2996
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002997int
2998SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2999 u64 persistent_fid, u64 volatile_fid,
3000 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003001 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003002{
3003 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003004 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003005 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003006 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003007
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003008 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3009 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003010 if (rc)
3011 return rc;
3012
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003013 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003014 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003015 req->PersistentFileId = persistent_fid;
3016 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003017 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02003018
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003019 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003020 if (input_len) {
3021 req->InputBufferLength = cpu_to_le32(input_len);
3022 /* total_len for smb query request never close to le16 max */
3023 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3024 memcpy(req->Buffer, input, input_len);
3025 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003026
3027 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003028 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003029 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003030 return 0;
3031}
3032
3033void
3034SMB2_query_info_free(struct smb_rqst *rqst)
3035{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003036 if (rqst && rqst->rq_iov)
3037 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003038}
3039
3040static int
3041query_info(const unsigned int xid, struct cifs_tcon *tcon,
3042 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3043 u32 additional_info, size_t output_len, size_t min_len, void **data,
3044 u32 *dlen)
3045{
3046 struct smb_rqst rqst;
3047 struct smb2_query_info_rsp *rsp = NULL;
3048 struct kvec iov[1];
3049 struct kvec rsp_iov;
3050 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01003051 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003052 struct cifs_ses *ses = tcon->ses;
3053 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003054 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003055
3056 cifs_dbg(FYI, "Query Info\n");
3057
3058 if (!ses || !(ses->server))
3059 return -EIO;
3060
3061 if (smb3_encryption_required(tcon))
3062 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003063
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003064 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003065 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003066 rqst.rq_iov = iov;
3067 rqst.rq_nvec = 1;
3068
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003069 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3070 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003071 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003072 if (rc)
3073 goto qinf_exit;
3074
Steve Frenchd42043a2019-02-26 21:58:30 -06003075 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3076 ses->Suid, info_class, (__u32)info_type);
3077
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003078 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003079 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003080
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003081 if (rc) {
3082 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003083 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3084 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003085 goto qinf_exit;
3086 }
3087
Steve Frenchd42043a2019-02-26 21:58:30 -06003088 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3089 ses->Suid, info_class, (__u32)info_type);
3090
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003091 if (dlen) {
3092 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3093 if (!*data) {
3094 *data = kmalloc(*dlen, GFP_KERNEL);
3095 if (!*data) {
3096 cifs_dbg(VFS,
3097 "Error %d allocating memory for acl\n",
3098 rc);
3099 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003100 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003101 goto qinf_exit;
3102 }
Colin Ian King73aaf922019-01-16 16:28:59 +00003103 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003104 }
3105 }
3106
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003107 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3108 le32_to_cpu(rsp->OutputBufferLength),
3109 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00003110 if (rc && allocated) {
3111 kfree(*data);
3112 *data = NULL;
3113 *dlen = 0;
3114 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003115
3116qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003117 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003118 free_rsp_buf(resp_buftype, rsp);
3119 return rc;
3120}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003121
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003122int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3123 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003124{
3125 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003126 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04003127 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003128 sizeof(struct smb2_file_all_info), (void **)&data,
3129 NULL);
3130}
3131
3132int
3133SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3134 u64 persistent_fid, u64 volatile_fid,
3135 void **data, u32 *plen)
3136{
3137 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3138 *plen = 0;
3139
3140 return query_info(xid, tcon, persistent_fid, volatile_fid,
3141 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05003142 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003143}
3144
3145int
3146SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3147 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3148{
3149 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003150 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003151 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003152 sizeof(struct smb2_file_internal_info),
3153 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003154}
3155
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003156/*
3157 * This is a no-op for now. We're not really interested in the reply, but
3158 * rather in the fact that the server sent one and that server->lstrp
3159 * gets updated.
3160 *
3161 * FIXME: maybe we should consider checking that the reply matches request?
3162 */
3163static void
3164smb2_echo_callback(struct mid_q_entry *mid)
3165{
3166 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003167 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003168 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003169
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003170 if (mid->mid_state == MID_RESPONSE_RECEIVED
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003171 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3172 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3173 credits.instance = server->reconnect_instance;
3174 }
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003175
3176 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003177 add_credits(server, &credits, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003178}
3179
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003180void smb2_reconnect_server(struct work_struct *work)
3181{
3182 struct TCP_Server_Info *server = container_of(work,
3183 struct TCP_Server_Info, reconnect.work);
3184 struct cifs_ses *ses;
3185 struct cifs_tcon *tcon, *tcon2;
3186 struct list_head tmp_list;
3187 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01003188 int rc;
3189 int resched = false;
3190
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003191
3192 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3193 mutex_lock(&server->reconnect_mutex);
3194
3195 INIT_LIST_HEAD(&tmp_list);
3196 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3197
3198 spin_lock(&cifs_tcp_ses_lock);
3199 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3200 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003201 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003202 tcon->tc_count++;
3203 list_add_tail(&tcon->rlist, &tmp_list);
3204 tcon_exist = true;
3205 }
3206 }
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003207 /*
3208 * IPC has the same lifetime as its session and uses its
3209 * refcount.
3210 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003211 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3212 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3213 tcon_exist = true;
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003214 ses->ses_count++;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003215 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003216 }
3217 /*
3218 * Get the reference to server struct to be sure that the last call of
3219 * cifs_put_tcon() in the loop below won't release the server pointer.
3220 */
3221 if (tcon_exist)
3222 server->srv_count++;
3223
3224 spin_unlock(&cifs_tcp_ses_lock);
3225
3226 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01003227 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3228 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003229 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01003230 else
3231 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003232 list_del_init(&tcon->rlist);
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003233 if (tcon->ipc)
3234 cifs_put_smb_ses(tcon->ses);
3235 else
3236 cifs_put_tcon(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003237 }
3238
3239 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01003240 if (resched)
3241 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003242 mutex_unlock(&server->reconnect_mutex);
3243
3244 /* now we can safely release srv struct */
3245 if (tcon_exist)
3246 cifs_put_tcp_session(server, 1);
3247}
3248
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003249int
3250SMB2_echo(struct TCP_Server_Info *server)
3251{
3252 struct smb2_echo_req *req;
3253 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003254 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003255 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003256 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003257 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003258
Joe Perchesf96637b2013-05-04 22:12:25 -05003259 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003260
Steve French4fcd1812016-06-22 20:12:05 -05003261 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003262 /* No need to send echo on newly established connections */
3263 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3264 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003265 }
3266
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003267 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003268 if (rc)
3269 return rc;
3270
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003271 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003272
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003273 iov[0].iov_len = total_len;
3274 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003275
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003276 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003277 server, CIFS_ECHO_OP, NULL);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003278 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003279 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003280
3281 cifs_small_buf_release(req);
3282 return rc;
3283}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003284
3285int
3286SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3287 u64 volatile_fid)
3288{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003289 struct smb_rqst rqst;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003290 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003291 struct cifs_ses *ses = tcon->ses;
3292 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003293 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003294 int resp_buftype;
3295 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003296 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003297 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003298
Joe Perchesf96637b2013-05-04 22:12:25 -05003299 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003300
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003301 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003302 return -EIO;
3303
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003304 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003305 if (rc)
3306 return rc;
3307
Steve French5a77e752018-05-09 17:43:08 -05003308 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003309 flags |= CIFS_TRANSFORM_REQ;
3310
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003311 req->PersistentFileId = persistent_fid;
3312 req->VolatileFileId = volatile_fid;
3313
3314 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003315 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003316
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003317 memset(&rqst, 0, sizeof(struct smb_rqst));
3318 rqst.rq_iov = iov;
3319 rqst.rq_nvec = 1;
3320
3321 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003322 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003323
Steve Frencheccb4422018-05-17 21:16:55 -05003324 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003325 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003326 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3327 rc);
3328 }
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003329
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003330 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003331 return rc;
3332}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003333
3334/*
3335 * To form a chain of read requests, any read requests after the first should
3336 * have the end_of_chain boolean set to true.
3337 */
3338static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003339smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003340 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3341 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003342{
3343 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003344 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003345 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07003346 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003347
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003348 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3349 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003350 if (rc)
3351 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003352
3353 server = io_parms->tcon->ses->server;
3354 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003355 return -ECONNABORTED;
3356
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003357 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003358 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003359
3360 req->PersistentFileId = io_parms->persistent_fid;
3361 req->VolatileFileId = io_parms->volatile_fid;
3362 req->ReadChannelInfoOffset = 0; /* reserved */
3363 req->ReadChannelInfoLength = 0; /* reserved */
3364 req->Channel = 0; /* reserved */
3365 req->MinimumCount = 0;
3366 req->Length = cpu_to_le32(io_parms->length);
3367 req->Offset = cpu_to_le64(io_parms->offset);
Steve Frenchd323c2462019-02-25 00:52:43 -06003368
3369 trace_smb3_read_enter(0 /* xid */,
3370 io_parms->persistent_fid,
3371 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3372 io_parms->offset, io_parms->length);
Long Libd3dcc62017-11-22 17:38:47 -07003373#ifdef CONFIG_CIFS_SMB_DIRECT
3374 /*
3375 * If we want to do a RDMA write, fill in and append
3376 * smbd_buffer_descriptor_v1 to the end of read request
3377 */
Long Libb4c0412018-04-17 12:17:08 -07003378 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003379 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003380
Long Libd3dcc62017-11-22 17:38:47 -07003381 struct smbd_buffer_descriptor_v1 *v1;
3382 bool need_invalidate =
3383 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3384
3385 rdata->mr = smbd_register_mr(
3386 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003387 rdata->nr_pages, rdata->page_offset,
3388 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003389 if (!rdata->mr)
Long Lib7972092019-04-05 21:36:34 +00003390 return -EAGAIN;
Long Libd3dcc62017-11-22 17:38:47 -07003391
3392 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3393 if (need_invalidate)
3394 req->Channel = SMB2_CHANNEL_RDMA_V1;
3395 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003396 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003397 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003398 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003399 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003400 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3401 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3402 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003403
3404 *total_len += sizeof(*v1) - 1;
3405 }
3406#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003407 if (request_type & CHAINED_REQUEST) {
3408 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003409 /* next 8-byte aligned request */
3410 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3411 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003412 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003413 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003414 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003415 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003416 /*
3417 * Related requests use info from previous read request
3418 * in chain.
3419 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003420 shdr->SessionId = 0xFFFFFFFF;
3421 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003422 req->PersistentFileId = 0xFFFFFFFF;
3423 req->VolatileFileId = 0xFFFFFFFF;
3424 }
3425 }
3426 if (remaining_bytes > io_parms->length)
3427 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3428 else
3429 req->RemainingBytes = 0;
3430
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003431 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003432 return rc;
3433}
3434
3435static void
3436smb2_readv_callback(struct mid_q_entry *mid)
3437{
3438 struct cifs_readdata *rdata = mid->callback_data;
3439 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3440 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003441 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003442 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003443 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003444 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3445 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07003446 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003447 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003448 .rq_npages = rdata->nr_pages,
3449 .rq_pagesz = rdata->pagesz,
3450 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003451
Joe Perchesf96637b2013-05-04 22:12:25 -05003452 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3453 __func__, mid->mid, mid->mid_state, rdata->result,
3454 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003455
3456 switch (mid->mid_state) {
3457 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003458 credits.value = le16_to_cpu(shdr->CreditRequest);
3459 credits.instance = server->reconnect_instance;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003460 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003461 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003462 int rc;
3463
Jeff Layton0b688cf2012-09-18 16:20:34 -07003464 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003465 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003466 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
3467 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003468 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003469 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003470 task_io_account_read(rdata->got_bytes);
3471 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003472 break;
3473 case MID_REQUEST_SUBMITTED:
3474 case MID_RETRY_NEEDED:
3475 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003476 if (server->sign && rdata->got_bytes)
3477 /* reset bytes number since we can not check a sign */
3478 rdata->got_bytes = 0;
3479 /* FIXME: should this be counted toward the initiating task? */
3480 task_io_account_read(rdata->got_bytes);
3481 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003482 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003483 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003484 credits.value = le16_to_cpu(shdr->CreditRequest);
3485 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003486 /* fall through */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003487 default:
Pavel Shilovsky6b15eb12019-01-18 15:46:14 -08003488 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003489 }
Long Libd3dcc62017-11-22 17:38:47 -07003490#ifdef CONFIG_CIFS_SMB_DIRECT
3491 /*
3492 * If this rdata has a memmory registered, the MR can be freed
3493 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3494 * because they have limited number and are used for future I/Os
3495 */
3496 if (rdata->mr) {
3497 smbd_deregister_mr(rdata->mr);
3498 rdata->mr = NULL;
3499 }
3500#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08003501 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003502 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003503 trace_smb3_read_err(0 /* xid */,
3504 rdata->cfile->fid.persistent_fid,
3505 tcon->tid, tcon->ses->Suid, rdata->offset,
3506 rdata->bytes, rdata->result);
3507 } else
3508 trace_smb3_read_done(0 /* xid */,
3509 rdata->cfile->fid.persistent_fid,
3510 tcon->tid, tcon->ses->Suid,
3511 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003512
3513 queue_work(cifsiod_wq, &rdata->work);
3514 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003515 add_credits(server, &credits, 0);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003516}
3517
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003518/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003519int
3520smb2_async_readv(struct cifs_readdata *rdata)
3521{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003522 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003523 char *buf;
3524 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003525 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003526 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003527 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003528 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003529 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003530
Joe Perchesf96637b2013-05-04 22:12:25 -05003531 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3532 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003533
3534 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3535 io_parms.offset = rdata->offset;
3536 io_parms.length = rdata->bytes;
3537 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3538 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3539 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003540
3541 server = io_parms.tcon->ses->server;
3542
Long Li2dabfd52017-11-07 01:54:53 -07003543 rc = smb2_new_read_req(
3544 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003545 if (rc)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003546 return rc;
3547
Steve French5a77e752018-05-09 17:43:08 -05003548 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003549 flags |= CIFS_TRANSFORM_REQ;
3550
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003551 rdata->iov[0].iov_base = buf;
3552 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003553
3554 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003555
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003556 if (rdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003557 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003558 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003559 shdr->CreditRequest =
3560 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003561
3562 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3563 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003564 goto async_readv_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003565
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003566 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003567 }
3568
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003569 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07003570 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003571 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003572 smb3_handle_read_data, rdata, flags,
3573 &rdata->credits);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003574 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003575 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003576 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003577 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3578 io_parms.tcon->tid,
3579 io_parms.tcon->ses->Suid,
3580 io_parms.offset, io_parms.length, rc);
3581 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003582
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003583async_readv_out:
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003584 cifs_small_buf_release(buf);
3585 return rc;
3586}
Pavel Shilovsky33319142012-09-18 16:20:29 -07003587
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003588int
3589SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3590 unsigned int *nbytes, char **buf, int *buf_type)
3591{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003592 struct smb_rqst rqst;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003593 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003594 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003595 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003596 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003597 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003598 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003599 int flags = CIFS_LOG_ERROR;
3600 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003601
3602 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07003603 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003604 if (rc)
3605 return rc;
3606
Steve French5a77e752018-05-09 17:43:08 -05003607 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003608 flags |= CIFS_TRANSFORM_REQ;
3609
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003610 iov[0].iov_base = (char *)req;
3611 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003612
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003613 memset(&rqst, 0, sizeof(struct smb_rqst));
3614 rqst.rq_iov = iov;
3615 rqst.rq_nvec = 1;
3616
3617 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003618 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003619
3620 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003621 if (rc != -ENODATA) {
3622 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3623 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003624 trace_smb3_read_err(xid, req->PersistentFileId,
3625 io_parms->tcon->tid, ses->Suid,
3626 io_parms->offset, io_parms->length,
3627 rc);
Steve Frenchb0a42f22019-02-25 15:02:58 -06003628 } else
3629 trace_smb3_read_done(xid, req->PersistentFileId,
3630 io_parms->tcon->tid, ses->Suid,
3631 io_parms->offset, 0);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003632 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Ronnie Sahlberg05fd5c22019-04-23 16:39:45 +10003633 cifs_small_buf_release(req);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003634 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05003635 } else
3636 trace_smb3_read_done(xid, req->PersistentFileId,
3637 io_parms->tcon->tid, ses->Suid,
3638 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003639
ZhangXiaoxu088aaf12019-04-06 15:47:39 +08003640 cifs_small_buf_release(req);
3641
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003642 *nbytes = le32_to_cpu(rsp->DataLength);
3643 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3644 (*nbytes > io_parms->length)) {
3645 cifs_dbg(FYI, "bad length %d for count %d\n",
3646 *nbytes, io_parms->length);
3647 rc = -EIO;
3648 *nbytes = 0;
3649 }
3650
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003651 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003652 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003653 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003654 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003655 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003656 if (resp_buftype == CIFS_SMALL_BUFFER)
3657 *buf_type = CIFS_SMALL_BUFFER;
3658 else if (resp_buftype == CIFS_LARGE_BUFFER)
3659 *buf_type = CIFS_LARGE_BUFFER;
3660 }
3661 return rc;
3662}
3663
Pavel Shilovsky33319142012-09-18 16:20:29 -07003664/*
3665 * Check the mid_state and signature on received buffer (if any), and queue the
3666 * workqueue completion task.
3667 */
3668static void
3669smb2_writev_callback(struct mid_q_entry *mid)
3670{
3671 struct cifs_writedata *wdata = mid->callback_data;
3672 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003673 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003674 unsigned int written;
3675 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003676 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky33319142012-09-18 16:20:29 -07003677
3678 switch (mid->mid_state) {
3679 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003680 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3681 credits.instance = server->reconnect_instance;
3682 wdata->result = smb2_check_receive(mid, server, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003683 if (wdata->result != 0)
3684 break;
3685
3686 written = le32_to_cpu(rsp->DataLength);
3687 /*
3688 * Mask off high 16 bits when bytes written as returned
3689 * by the server is greater than bytes requested by the
3690 * client. OS/2 servers are known to set incorrect
3691 * CountHigh values.
3692 */
3693 if (written > wdata->bytes)
3694 written &= 0xFFFF;
3695
3696 if (written < wdata->bytes)
3697 wdata->result = -ENOSPC;
3698 else
3699 wdata->bytes = written;
3700 break;
3701 case MID_REQUEST_SUBMITTED:
3702 case MID_RETRY_NEEDED:
3703 wdata->result = -EAGAIN;
3704 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003705 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003706 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3707 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003708 /* fall through */
Pavel Shilovsky33319142012-09-18 16:20:29 -07003709 default:
3710 wdata->result = -EIO;
3711 break;
3712 }
Long Lidb223a52017-11-22 17:38:45 -07003713#ifdef CONFIG_CIFS_SMB_DIRECT
3714 /*
3715 * If this wdata has a memory registered, the MR can be freed
3716 * The number of MRs available is limited, it's important to recover
3717 * used MR as soon as I/O is finished. Hold MR longer in the later
3718 * I/O process can possibly result in I/O deadlock due to lack of MR
3719 * to send request on I/O retry
3720 */
3721 if (wdata->mr) {
3722 smbd_deregister_mr(wdata->mr);
3723 wdata->mr = NULL;
3724 }
3725#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003726 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003727 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003728 trace_smb3_write_err(0 /* no xid */,
3729 wdata->cfile->fid.persistent_fid,
3730 tcon->tid, tcon->ses->Suid, wdata->offset,
3731 wdata->bytes, wdata->result);
3732 } else
3733 trace_smb3_write_done(0 /* no xid */,
3734 wdata->cfile->fid.persistent_fid,
3735 tcon->tid, tcon->ses->Suid,
3736 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003737
3738 queue_work(cifsiod_wq, &wdata->work);
3739 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003740 add_credits(server, &credits, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003741}
3742
3743/* smb2_async_writev - send an async write, and set up mid to handle result */
3744int
Steve French4a5c80d2014-02-07 20:45:12 -06003745smb2_async_writev(struct cifs_writedata *wdata,
3746 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07003747{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003748 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003749 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003750 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003751 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003752 struct TCP_Server_Info *server = tcon->ses->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003753 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003754 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003755 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003756
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003757 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003758 if (rc)
3759 return rc;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003760
Steve French5a77e752018-05-09 17:43:08 -05003761 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003762 flags |= CIFS_TRANSFORM_REQ;
3763
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003764 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003765 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003766
3767 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3768 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3769 req->WriteChannelInfoOffset = 0;
3770 req->WriteChannelInfoLength = 0;
3771 req->Channel = 0;
3772 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003773 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003774 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003775 req->RemainingBytes = 0;
Steve Frenchd323c2462019-02-25 00:52:43 -06003776
3777 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3778 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003779#ifdef CONFIG_CIFS_SMB_DIRECT
3780 /*
3781 * If we want to do a server RDMA read, fill in and append
3782 * smbd_buffer_descriptor_v1 to the end of write request
3783 */
Long Libb4c0412018-04-17 12:17:08 -07003784 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003785 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003786
Long Lidb223a52017-11-22 17:38:45 -07003787 struct smbd_buffer_descriptor_v1 *v1;
3788 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3789
3790 wdata->mr = smbd_register_mr(
3791 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003792 wdata->nr_pages, wdata->page_offset,
3793 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07003794 if (!wdata->mr) {
Long Lib7972092019-04-05 21:36:34 +00003795 rc = -EAGAIN;
Long Lidb223a52017-11-22 17:38:45 -07003796 goto async_writev_out;
3797 }
3798 req->Length = 0;
3799 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07003800 if (wdata->nr_pages > 1)
3801 req->RemainingBytes =
3802 cpu_to_le32(
3803 (wdata->nr_pages - 1) * wdata->pagesz -
3804 wdata->page_offset + wdata->tailsz
3805 );
3806 else
3807 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003808 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3809 if (need_invalidate)
3810 req->Channel = SMB2_CHANNEL_RDMA_V1;
3811 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003812 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003813 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003814 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003815 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003816 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3817 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3818 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003819 }
3820#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003821 iov[0].iov_len = total_len - 1;
3822 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003823
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003824 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003825 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003826 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07003827 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003828 rqst.rq_npages = wdata->nr_pages;
3829 rqst.rq_pagesz = wdata->pagesz;
3830 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003831#ifdef CONFIG_CIFS_SMB_DIRECT
3832 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003833 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07003834 rqst.rq_npages = 0;
3835 }
3836#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003837 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3838 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003839
Long Lidb223a52017-11-22 17:38:45 -07003840#ifdef CONFIG_CIFS_SMB_DIRECT
3841 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3842 if (!wdata->mr)
3843 req->Length = cpu_to_le32(wdata->bytes);
3844#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003845 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003846#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003847
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003848 if (wdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003849 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003850 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003851 shdr->CreditRequest =
3852 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003853
3854 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3855 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003856 goto async_writev_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003857
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003858 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003859 }
3860
Pavel Shilovsky33319142012-09-18 16:20:29 -07003861 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003862 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003863 wdata, flags, &wdata->credits);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003864
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003865 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003866 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3867 tcon->tid, tcon->ses->Suid, wdata->offset,
3868 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06003869 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003870 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003871 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003872
Pavel Shilovsky33319142012-09-18 16:20:29 -07003873async_writev_out:
3874 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003875 return rc;
3876}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003877
3878/*
3879 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3880 * The length field from io_parms must be at least 1 and indicates a number of
3881 * elements with data to write that begins with position 1 in iov array. All
3882 * data length is specified by count.
3883 */
3884int
3885SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3886 unsigned int *nbytes, struct kvec *iov, int n_vec)
3887{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003888 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003889 int rc = 0;
3890 struct smb2_write_req *req = NULL;
3891 struct smb2_write_rsp *rsp = NULL;
3892 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003893 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003894 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003895 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003896
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003897 *nbytes = 0;
3898
3899 if (n_vec < 1)
3900 return rc;
3901
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003902 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3903 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003904 if (rc)
3905 return rc;
3906
3907 if (io_parms->tcon->ses->server == NULL)
3908 return -ECONNABORTED;
3909
Steve French5a77e752018-05-09 17:43:08 -05003910 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003911 flags |= CIFS_TRANSFORM_REQ;
3912
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003913 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003914
3915 req->PersistentFileId = io_parms->persistent_fid;
3916 req->VolatileFileId = io_parms->volatile_fid;
3917 req->WriteChannelInfoOffset = 0;
3918 req->WriteChannelInfoLength = 0;
3919 req->Channel = 0;
3920 req->Length = cpu_to_le32(io_parms->length);
3921 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003922 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003923 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003924 req->RemainingBytes = 0;
3925
Steve Frenchd323c2462019-02-25 00:52:43 -06003926 trace_smb3_write_enter(xid, io_parms->persistent_fid,
3927 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3928 io_parms->offset, io_parms->length);
3929
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003930 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003931 /* 1 for Buffer */
3932 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003933
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003934 memset(&rqst, 0, sizeof(struct smb_rqst));
3935 rqst.rq_iov = iov;
3936 rqst.rq_nvec = n_vec + 1;
3937
3938 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003939 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003940 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003941
3942 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003943 trace_smb3_write_err(xid, req->PersistentFileId,
3944 io_parms->tcon->tid,
3945 io_parms->tcon->ses->Suid,
3946 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003947 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003948 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05003949 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003950 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05003951 trace_smb3_write_done(xid, req->PersistentFileId,
3952 io_parms->tcon->tid,
3953 io_parms->tcon->ses->Suid,
3954 io_parms->offset, *nbytes);
3955 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003956
ZhangXiaoxu6a3eb332019-04-06 15:47:38 +08003957 cifs_small_buf_release(req);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003958 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003959 return rc;
3960}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003961
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003962static unsigned int
3963num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3964{
3965 int len;
3966 unsigned int entrycount = 0;
3967 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03003968 char *entryptr;
3969 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003970
3971 if (bufstart == NULL)
3972 return 0;
3973
Dan Carpenter56446f22018-09-06 12:48:22 +03003974 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003975
3976 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03003977 if (entryptr + next_offset < entryptr ||
3978 entryptr + next_offset > end_of_buf ||
3979 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003980 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003981 break;
3982 }
3983
Dan Carpenter56446f22018-09-06 12:48:22 +03003984 entryptr = entryptr + next_offset;
3985 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
3986
3987 len = le32_to_cpu(dir_info->FileNameLength);
3988 if (entryptr + len < entryptr ||
3989 entryptr + len > end_of_buf ||
3990 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003991 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3992 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003993 break;
3994 }
3995
Dan Carpenter56446f22018-09-06 12:48:22 +03003996 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003997 entrycount++;
3998
Dan Carpenter56446f22018-09-06 12:48:22 +03003999 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004000 if (!next_offset)
4001 break;
4002 }
4003
4004 return entrycount;
4005}
4006
4007/*
4008 * Readdir/FindFirst
4009 */
4010int
4011SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4012 u64 persistent_fid, u64 volatile_fid, int index,
4013 struct cifs_search_info *srch_inf)
4014{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004015 struct smb_rqst rqst;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004016 struct smb2_query_directory_req *req;
4017 struct smb2_query_directory_rsp *rsp = NULL;
4018 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004019 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004020 int rc = 0;
4021 int len;
Steve French75fdfc82015-03-25 18:51:57 -05004022 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004023 unsigned char *bufptr;
4024 struct TCP_Server_Info *server;
4025 struct cifs_ses *ses = tcon->ses;
4026 __le16 asteriks = cpu_to_le16('*');
4027 char *end_of_smb;
4028 unsigned int output_size = CIFSMaxBufSize;
4029 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004030 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004031 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004032
4033 if (ses && (ses->server))
4034 server = ses->server;
4035 else
4036 return -EIO;
4037
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004038 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4039 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004040 if (rc)
4041 return rc;
4042
Steve French5a77e752018-05-09 17:43:08 -05004043 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004044 flags |= CIFS_TRANSFORM_REQ;
4045
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004046 switch (srch_inf->info_level) {
4047 case SMB_FIND_FILE_DIRECTORY_INFO:
4048 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4049 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4050 break;
4051 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4052 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4053 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4054 break;
4055 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05004056 cifs_dbg(VFS, "info level %u isn't supported\n",
4057 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004058 rc = -EINVAL;
4059 goto qdir_exit;
4060 }
4061
4062 req->FileIndex = cpu_to_le32(index);
4063 req->PersistentFileId = persistent_fid;
4064 req->VolatileFileId = volatile_fid;
4065
4066 len = 0x2;
4067 bufptr = req->Buffer;
4068 memcpy(bufptr, &asteriks, len);
4069
4070 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004071 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004072 req->FileNameLength = cpu_to_le16(len);
4073 /*
4074 * BB could be 30 bytes or so longer if we used SMB2 specific
4075 * buffer lengths, but this is safe and close enough.
4076 */
4077 output_size = min_t(unsigned int, output_size, server->maxBuf);
4078 output_size = min_t(unsigned int, output_size, 2 << 15);
4079 req->OutputBufferLength = cpu_to_le32(output_size);
4080
4081 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004082 /* 1 for Buffer */
4083 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004084
4085 iov[1].iov_base = (char *)(req->Buffer);
4086 iov[1].iov_len = len;
4087
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004088 memset(&rqst, 0, sizeof(struct smb_rqst));
4089 rqst.rq_iov = iov;
4090 rqst.rq_nvec = 2;
4091
Steve Frenchd323c2462019-02-25 00:52:43 -06004092 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4093 tcon->ses->Suid, index, output_size);
4094
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004095 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004096 cifs_small_buf_release(req);
4097 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004098
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004099 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004100 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004101 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004102 trace_smb3_query_dir_done(xid, persistent_fid,
4103 tcon->tid, tcon->ses->Suid, index, 0);
Pavel Shilovsky52755802014-08-18 20:49:57 +04004104 srch_inf->endOfSearch = true;
4105 rc = 0;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004106 } else {
4107 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4108 tcon->ses->Suid, index, 0, rc);
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08004109 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004110 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004111 goto qdir_exit;
4112 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004113
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004114 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4115 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4116 info_buf_size);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004117 if (rc) {
4118 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4119 tcon->ses->Suid, index, 0, rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004120 goto qdir_exit;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004121 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004122
4123 srch_inf->unicode = true;
4124
4125 if (srch_inf->ntwrk_buf_start) {
4126 if (srch_inf->smallBuf)
4127 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4128 else
4129 cifs_buf_release(srch_inf->ntwrk_buf_start);
4130 }
4131 srch_inf->ntwrk_buf_start = (char *)rsp;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004132 srch_inf->srch_entries_start = srch_inf->last_entry =
4133 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4134 end_of_smb = rsp_iov.iov_len + (char *)rsp;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004135 srch_inf->entries_in_buffer =
4136 num_entries(srch_inf->srch_entries_start, end_of_smb,
4137 &srch_inf->last_entry, info_buf_size);
4138 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05004139 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4140 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4141 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004142 if (resp_buftype == CIFS_LARGE_BUFFER)
4143 srch_inf->smallBuf = false;
4144 else if (resp_buftype == CIFS_SMALL_BUFFER)
4145 srch_inf->smallBuf = true;
4146 else
Joe Perchesf96637b2013-05-04 22:12:25 -05004147 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004148
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004149 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4150 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004151 return rc;
4152
4153qdir_exit:
4154 free_rsp_buf(resp_buftype, rsp);
4155 return rc;
4156}
4157
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004158int
4159SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004160 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004161 u8 info_type, u32 additional_info,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004162 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004163{
4164 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004165 struct kvec *iov = rqst->rq_iov;
4166 unsigned int i, total_len;
4167 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004168
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004169 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004170 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004171 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004172
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004173 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004174 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004175 req->FileInfoClass = info_class;
4176 req->PersistentFileId = persistent_fid;
4177 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004178 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004179
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004180 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004181 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004182 req->BufferLength = cpu_to_le32(*size);
4183
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004184 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004185 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004186
4187 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004188 /* 1 for Buffer */
4189 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004190
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004191 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004192 le32_add_cpu(&req->BufferLength, size[i]);
4193 iov[i].iov_base = (char *)data[i];
4194 iov[i].iov_len = size[i];
4195 }
4196
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004197 return 0;
4198}
4199
4200void
4201SMB2_set_info_free(struct smb_rqst *rqst)
4202{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10004203 if (rqst && rqst->rq_iov)
4204 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004205}
4206
4207static int
4208send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4209 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4210 u8 info_type, u32 additional_info, unsigned int num,
4211 void **data, unsigned int *size)
4212{
4213 struct smb_rqst rqst;
4214 struct smb2_set_info_rsp *rsp = NULL;
4215 struct kvec *iov;
4216 struct kvec rsp_iov;
4217 int rc = 0;
4218 int resp_buftype;
4219 struct cifs_ses *ses = tcon->ses;
4220 int flags = 0;
4221
4222 if (!ses || !(ses->server))
4223 return -EIO;
4224
4225 if (!num)
4226 return -EINVAL;
4227
4228 if (smb3_encryption_required(tcon))
4229 flags |= CIFS_TRANSFORM_REQ;
4230
4231 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4232 if (!iov)
4233 return -ENOMEM;
4234
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004235 memset(&rqst, 0, sizeof(struct smb_rqst));
4236 rqst.rq_iov = iov;
4237 rqst.rq_nvec = num;
4238
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004239 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4240 info_class, info_type, additional_info,
4241 data, size);
4242 if (rc) {
4243 kfree(iov);
4244 return rc;
4245 }
4246
4247
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004248 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004249 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004250 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004251 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004252
Steve Frencheccb4422018-05-17 21:16:55 -05004253 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004254 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004255 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4256 ses->Suid, info_class, (__u32)info_type, rc);
4257 }
Steve French7d3fb242013-11-18 09:56:28 -06004258
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004259 free_rsp_buf(resp_buftype, rsp);
4260 kfree(iov);
4261 return rc;
4262}
4263
4264int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004265SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004266 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004267{
4268 struct smb2_file_eof_info info;
4269 void *data;
4270 unsigned int size;
4271
4272 info.EndOfFile = *eof;
4273
4274 data = &info;
4275 size = sizeof(struct smb2_file_eof_info);
4276
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004277 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004278 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4279 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004280}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004281
4282int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004283SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4284 u64 persistent_fid, u64 volatile_fid,
4285 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4286{
4287 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4288 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4289 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004290}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004291
4292int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004293SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4294 u64 persistent_fid, u64 volatile_fid,
4295 struct smb2_file_full_ea_info *buf, int len)
4296{
4297 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4298 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4299 0, 1, (void **)&buf, &len);
4300}
4301
4302int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004303SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4304 const u64 persistent_fid, const u64 volatile_fid,
4305 __u8 oplock_level)
4306{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004307 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004308 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10004309 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004310 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004311 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004312 unsigned int total_len;
4313 struct kvec iov[1];
4314 struct kvec rsp_iov;
4315 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004316
Joe Perchesf96637b2013-05-04 22:12:25 -05004317 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004318 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4319 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004320 if (rc)
4321 return rc;
4322
Steve French5a77e752018-05-09 17:43:08 -05004323 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004324 flags |= CIFS_TRANSFORM_REQ;
4325
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004326 req->VolatileFid = volatile_fid;
4327 req->PersistentFid = persistent_fid;
4328 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004329 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004330
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004331 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004332
4333 iov[0].iov_base = (char *)req;
4334 iov[0].iov_len = total_len;
4335
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004336 memset(&rqst, 0, sizeof(struct smb_rqst));
4337 rqst.rq_iov = iov;
4338 rqst.rq_nvec = 1;
4339
4340 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004341 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004342
4343 if (rc) {
4344 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004345 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004346 }
4347
4348 return rc;
4349}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004350
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004351void
4352smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4353 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004354{
4355 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4356 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4357 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05304358 kst->f_bfree = kst->f_bavail =
4359 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004360 return;
4361}
4362
Steve French2d304212018-06-24 23:28:12 -05004363static void
4364copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4365 struct kstatfs *kst)
4366{
4367 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4368 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4369 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4370 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4371 kst->f_bavail = kst->f_bfree;
4372 else
4373 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4374 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4375 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4376 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4377 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4378
4379 return;
4380}
Steve French2d304212018-06-24 23:28:12 -05004381
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004382static int
4383build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4384 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4385{
4386 int rc;
4387 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004388 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004389
Joe Perchesf96637b2013-05-04 22:12:25 -05004390 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004391
4392 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4393 return -EIO;
4394
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004395 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4396 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004397 if (rc)
4398 return rc;
4399
4400 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4401 req->FileInfoClass = level;
4402 req->PersistentFileId = persistent_fid;
4403 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004404 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004405 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004406 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004407 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004408 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004409
4410 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004411 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004412 return 0;
4413}
4414
Steve French2d304212018-06-24 23:28:12 -05004415int
4416SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4417 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4418{
4419 struct smb_rqst rqst;
4420 struct smb2_query_info_rsp *rsp = NULL;
4421 struct kvec iov;
4422 struct kvec rsp_iov;
4423 int rc = 0;
4424 int resp_buftype;
4425 struct cifs_ses *ses = tcon->ses;
4426 FILE_SYSTEM_POSIX_INFO *info = NULL;
4427 int flags = 0;
4428
4429 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4430 sizeof(FILE_SYSTEM_POSIX_INFO),
4431 persistent_fid, volatile_fid);
4432 if (rc)
4433 return rc;
4434
4435 if (smb3_encryption_required(tcon))
4436 flags |= CIFS_TRANSFORM_REQ;
4437
4438 memset(&rqst, 0, sizeof(struct smb_rqst));
4439 rqst.rq_iov = &iov;
4440 rqst.rq_nvec = 1;
4441
4442 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4443 cifs_small_buf_release(iov.iov_base);
4444 if (rc) {
4445 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4446 goto posix_qfsinf_exit;
4447 }
4448 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4449
4450 info = (FILE_SYSTEM_POSIX_INFO *)(
4451 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004452 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4453 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4454 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05004455 if (!rc)
4456 copy_posix_fs_info_to_kstatfs(info, fsdata);
4457
4458posix_qfsinf_exit:
4459 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4460 return rc;
4461}
Steve French2d304212018-06-24 23:28:12 -05004462
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004463int
4464SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4465 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4466{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004467 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004468 struct smb2_query_info_rsp *rsp = NULL;
4469 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004470 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004471 int rc = 0;
4472 int resp_buftype;
4473 struct cifs_ses *ses = tcon->ses;
4474 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004475 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004476
4477 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4478 sizeof(struct smb2_fs_full_size_info),
4479 persistent_fid, volatile_fid);
4480 if (rc)
4481 return rc;
4482
Steve French5a77e752018-05-09 17:43:08 -05004483 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004484 flags |= CIFS_TRANSFORM_REQ;
4485
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004486 memset(&rqst, 0, sizeof(struct smb_rqst));
4487 rqst.rq_iov = &iov;
4488 rqst.rq_nvec = 1;
4489
4490 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004491 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004492 if (rc) {
4493 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05004494 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004495 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004496 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004497
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004498 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004499 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004500 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4501 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4502 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004503 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004504 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004505
Steve French34f62642013-10-09 02:07:00 -05004506qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004507 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004508 return rc;
4509}
4510
4511int
4512SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05004513 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05004514{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004515 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05004516 struct smb2_query_info_rsp *rsp = NULL;
4517 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004518 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05004519 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05004520 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05004521 struct cifs_ses *ses = tcon->ses;
4522 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004523 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05004524
Steven French21671142013-10-09 13:36:35 -05004525 if (level == FS_DEVICE_INFORMATION) {
4526 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4527 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4528 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4529 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4530 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004531 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4532 max_len = sizeof(struct smb3_fs_ss_info);
4533 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05004534 } else if (level == FS_VOLUME_INFORMATION) {
4535 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4536 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05004537 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004538 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05004539 return -EINVAL;
4540 }
4541
4542 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05004543 persistent_fid, volatile_fid);
4544 if (rc)
4545 return rc;
4546
Steve French5a77e752018-05-09 17:43:08 -05004547 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004548 flags |= CIFS_TRANSFORM_REQ;
4549
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004550 memset(&rqst, 0, sizeof(struct smb_rqst));
4551 rqst.rq_iov = &iov;
4552 rqst.rq_nvec = 1;
4553
4554 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004555 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004556 if (rc) {
4557 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4558 goto qfsattr_exit;
4559 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004560 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05004561
4562 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4563 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004564 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05004565 if (rc)
4566 goto qfsattr_exit;
4567
4568 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004569 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004570 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05004571 rsp_len, max_len));
4572 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004573 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004574 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004575 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4576 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004577 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004578 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4579 tcon->perf_sector_size =
4580 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05004581 } else if (level == FS_VOLUME_INFORMATION) {
4582 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4583 (offset + (char *)rsp);
4584 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4585 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004586 }
Steve French34f62642013-10-09 02:07:00 -05004587
4588qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004589 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004590 return rc;
4591}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004592
4593int
4594smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4595 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4596 const __u32 num_lock, struct smb2_lock_element *buf)
4597{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004598 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004599 int rc = 0;
4600 struct smb2_lock_req *req = NULL;
4601 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004602 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004603 int resp_buf_type;
4604 unsigned int count;
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004605 int flags = CIFS_NO_RSP_BUF;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004606 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004607
Joe Perchesf96637b2013-05-04 22:12:25 -05004608 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004609
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004610 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004611 if (rc)
4612 return rc;
4613
Steve French5a77e752018-05-09 17:43:08 -05004614 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004615 flags |= CIFS_TRANSFORM_REQ;
4616
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004617 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004618 req->LockCount = cpu_to_le16(num_lock);
4619
4620 req->PersistentFileId = persist_fid;
4621 req->VolatileFileId = volatile_fid;
4622
4623 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004624
4625 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004626 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004627 iov[1].iov_base = (char *)buf;
4628 iov[1].iov_len = count;
4629
4630 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004631
4632 memset(&rqst, 0, sizeof(struct smb_rqst));
4633 rqst.rq_iov = iov;
4634 rqst.rq_nvec = 2;
4635
4636 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004637 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004638 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004639 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004640 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004641 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004642 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4643 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004644 }
4645
4646 return rc;
4647}
4648
4649int
4650SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4651 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4652 const __u64 length, const __u64 offset, const __u32 lock_flags,
4653 const bool wait)
4654{
4655 struct smb2_lock_element lock;
4656
4657 lock.Offset = cpu_to_le64(offset);
4658 lock.Length = cpu_to_le64(length);
4659 lock.Flags = cpu_to_le32(lock_flags);
4660 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4661 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4662
4663 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4664}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004665
4666int
4667SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4668 __u8 *lease_key, const __le32 lease_state)
4669{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004670 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004671 int rc;
4672 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004673 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004674 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004675 unsigned int total_len;
4676 struct kvec iov[1];
4677 struct kvec rsp_iov;
4678 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05004679 __u64 *please_key_high;
4680 __u64 *please_key_low;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004681
Joe Perchesf96637b2013-05-04 22:12:25 -05004682 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004683 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4684 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004685 if (rc)
4686 return rc;
4687
Steve French5a77e752018-05-09 17:43:08 -05004688 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004689 flags |= CIFS_TRANSFORM_REQ;
4690
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004691 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004692 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004693 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004694
4695 memcpy(req->LeaseKey, lease_key, 16);
4696 req->LeaseState = lease_state;
4697
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004698 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004699
4700 iov[0].iov_base = (char *)req;
4701 iov[0].iov_len = total_len;
4702
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004703 memset(&rqst, 0, sizeof(struct smb_rqst));
4704 rqst.rq_iov = iov;
4705 rqst.rq_nvec = 1;
4706
4707 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004708 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004709
Aurelien Apteld339adc2019-01-31 13:46:07 +01004710 please_key_low = (__u64 *)lease_key;
4711 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004712 if (rc) {
4713 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05004714 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4715 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05004716 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05004717 } else
4718 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4719 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004720
4721 return rc;
4722}