blob: 4c9c34cdf05f9383cb02d8cd11a8704d05002c71 [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020036#include <linux/uuid.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070037#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040038#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070048#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070049#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050050#include "cifs_spnego.h"
Long Lidb223a52017-11-22 17:38:45 -070051#include "smbdirect.h"
Steve Frencheccb4422018-05-17 21:16:55 -050052#include "trace.h"
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -020053#ifdef CONFIG_CIFS_DFS_UPCALL
54#include "dfs_cache.h"
55#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040056
57/*
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
60 *
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
63 */
64static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
67 /* SMB2_LOGOFF */ 4,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
70 /* SMB2_CREATE */ 57,
71 /* SMB2_CLOSE */ 24,
72 /* SMB2_FLUSH */ 24,
73 /* SMB2_READ */ 49,
74 /* SMB2_WRITE */ 49,
75 /* SMB2_LOCK */ 48,
76 /* SMB2_IOCTL */ 57,
77 /* SMB2_CANCEL */ 4,
78 /* SMB2_ECHO */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
84};
85
Ronnie Sahlberg730928c2018-08-08 15:07:49 +100086int smb3_encryption_required(const struct cifs_tcon *tcon)
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070087{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080088 if (!tcon)
89 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070090 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
91 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
92 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080093 if (tcon->seal &&
94 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
95 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070096 return 0;
97}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040098
99static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700100smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400101 const struct cifs_tcon *tcon)
102{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700103 shdr->ProtocolId = SMB2_PROTO_NUMBER;
104 shdr->StructureSize = cpu_to_le16(64);
105 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
108
109 spin_lock(&server->req_lock);
Steve French69dc4b12019-03-05 21:04:56 -0600110 /* Request up to 10 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500111 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700112 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100113 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700114 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500115 min_t(int, server->max_credits -
Steve French69dc4b12019-03-05 21:04:56 -0600116 server->credits, 10));
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100117 spin_unlock(&server->req_lock);
118 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700119 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100120 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400122
123 if (!tcon)
124 goto out;
125
Steve French2b80d042013-06-23 18:43:37 -0500126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500128 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700130 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500131 /* else CreditCharge MBZ */
132
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700133 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400134 /* Uid is not converted */
135 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700136 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500137
138 /*
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
147 */
148/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500150
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
Steve French5a77e752018-05-09 17:43:08 -0500152 !smb3_encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700153 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400154out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400155 return;
156}
157
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200158#ifdef CONFIG_CIFS_DFS_UPCALL
159static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
161{
162 int rc;
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100165 char *tree;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200166 const char *tcp_host;
167 size_t tcp_host_len;
168 const char *dfs_host;
169 size_t dfs_host_len;
170
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
172 if (!tree)
173 return -ENOMEM;
174
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200175 if (tcon->ipc) {
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000176 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
179 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200180 }
181
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
184 goto out;
185 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200186
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
188 if (rc)
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100189 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200190
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
192 &tcp_host_len);
193
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
196 const char *tgt = dfs_cache_get_tgt_name(it);
197
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
199
200 if (dfs_host_len != tcp_host_len
201 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
202 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
203 __func__,
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
206 continue;
207 }
208
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000209 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200210
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
212 if (!rc)
213 break;
214 if (rc == -EREMOTE)
215 break;
216 }
217
218 if (!rc) {
219 if (it)
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
221 it);
222 else
223 rc = -ENOENT;
224 }
225 dfs_cache_free_tgts(&tl);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100226out:
227 kfree(tree);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200228 return rc;
229}
230#else
231static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
233{
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
235}
236#endif
237
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400238static int
239smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
240{
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300241 int rc;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200245 int retries;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400246
247 /*
248 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
249 * check for tcp and smb session status done differently
250 * for those three - in the calling routine.
251 */
252 if (tcon == NULL)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300253 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400254
Pavel Shilovskye99c63e2019-07-22 11:34:59 -0700255 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300256 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400257
258 if (tcon->tidStatus == CifsExiting) {
259 /*
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
263 */
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
268 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400269 return -ENODEV;
270 }
271 }
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
274 return -EIO;
275
276 ses = tcon->ses;
277 server = ses->server;
278
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200279 retries = server->nr_targets;
280
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400281 /*
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200282 * Give demultiplex thread up to 10 seconds to each target available for
283 * reconnect -- should be greater than cifs socket timeout which is 7
284 * seconds.
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400285 */
286 while (server->tcpStatus == CifsNeedReconnect) {
287 /*
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
290 */
291 switch (smb2_command) {
292 /*
293 * BB Should we keep oplock break and add flush to exceptions?
294 */
295 case SMB2_TREE_DISCONNECT:
296 case SMB2_CANCEL:
297 case SMB2_CLOSE:
298 case SMB2_OPLOCK_BREAK:
299 return -EAGAIN;
300 }
301
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
304 10 * HZ);
305 if (rc < 0) {
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
308 return -ERESTARTSYS;
309 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400310
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
313 break;
314
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200315 if (--retries)
316 continue;
317
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400318 /*
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
321 * back on-line
322 */
323 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400325 return -EHOSTDOWN;
326 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200327 retries = server->nr_targets;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400328 }
329
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300331 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400332
333 nls_codepage = load_nls_default();
334
335 /*
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
338 */
339 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200340
341 /*
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
345 */
346 if (server->tcpStatus == CifsNeedReconnect) {
347 rc = -EHOSTDOWN;
348 mutex_unlock(&tcon->ses->session_mutex);
349 goto out;
350 }
351
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400352 rc = cifs_negotiate_protocol(0, tcon->ses);
353 if (!rc && tcon->ses->need_reconnect)
354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
355
356 if (rc || !tcon->need_reconnect) {
357 mutex_unlock(&tcon->ses->session_mutex);
358 goto out;
359 }
360
361 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800362 if (tcon->use_persistent)
363 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500364
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200365 rc = __smb2_reconnect(nls_codepage, tcon);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400366 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500367
Joe Perchesf96637b2013-05-04 22:12:25 -0500368 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500369 if (rc) {
370 /* If sess reconnected but tcon didn't, something strange ... */
371 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400372 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500373 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800374
375 if (smb2_command != SMB2_INTERNAL_CMD)
376 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
377
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400378 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400379out:
380 /*
381 * Check if handle based operation so we know whether we can continue
382 * or not without returning to caller to reset file handle.
383 */
384 /*
385 * BB Is flush done by server on drop of tcp session? Should we special
386 * case it and skip above?
387 */
388 switch (smb2_command) {
389 case SMB2_FLUSH:
390 case SMB2_READ:
391 case SMB2_WRITE:
392 case SMB2_LOCK:
393 case SMB2_IOCTL:
394 case SMB2_QUERY_DIRECTORY:
395 case SMB2_CHANGE_NOTIFY:
396 case SMB2_QUERY_INFO:
397 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800398 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400399 }
400 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400401 return rc;
402}
403
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700404static void
405fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
406 unsigned int *total_len)
407{
408 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
409 /* lookup word count ie StructureSize from table */
410 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
411
412 /*
413 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
414 * largest operations (Create)
415 */
416 memset(buf, 0, 256);
417
418 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
419 spdu->StructureSize2 = cpu_to_le16(parmsize);
420
421 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
422}
423
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100424/*
425 * Allocate and return pointer to an SMB request hdr, and set basic
426 * SMB information in the SMB header. If the return code is zero, this
427 * function must have filled in request_buf pointer.
428 */
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800429static int
430smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
431 void **request_buf, unsigned int *total_len)
432{
433 int rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800434
435 rc = smb2_reconnect(smb2_command, tcon);
436 if (rc)
437 return rc;
438
439 /* BB eventually switch this to SMB2 specific small buf size */
Stefano Briviof46ecbd2018-07-05 11:46:42 +0200440 if (smb2_command == SMB2_SET_INFO)
441 *request_buf = cifs_buf_get();
442 else
443 *request_buf = cifs_small_buf_get();
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800444 if (*request_buf == NULL) {
445 /* BB should we add a retry in here if not a writepage? */
446 return -ENOMEM;
447 }
448
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100449 fill_small_buf(smb2_command, tcon,
450 (struct smb2_sync_hdr *)(*request_buf),
451 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400452
453 if (tcon != NULL) {
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400454 uint16_t com_code = le16_to_cpu(smb2_command);
455 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400456 cifs_stats_inc(&tcon->num_smbs_sent);
457 }
458
459 return rc;
460}
461
Steve Frenchd7bef4c2019-04-18 11:03:58 -0500462/* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500463
464static void
465build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
466{
467 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
468 pneg_ctxt->DataLength = cpu_to_le16(38);
469 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
470 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
471 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
472 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
473}
474
475static void
Steve French26ea8882019-04-26 20:36:08 -0700476build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
477{
478 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
479 pneg_ctxt->DataLength =
480 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
481 - sizeof(struct smb2_neg_context));
482 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
483 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
484 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
485 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
486}
487
488static void
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500489build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
490{
491 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French9ac63ec2019-06-07 08:59:40 -0500492 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
493 pneg_ctxt->CipherCount = cpu_to_le16(2);
494 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
495 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500496}
497
Steve French96d3cca2019-06-25 04:39:51 -0500498static unsigned int
499build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
500{
501 struct nls_table *cp = load_nls_default();
502
503 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
504
505 /* copy up to max of first 100 bytes of server name to NetName field */
Steve Frenchdf58fae2019-08-05 17:07:26 -0500506 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
Steve French96d3cca2019-06-25 04:39:51 -0500507 /* context size is DataLength + minimal smb2_neg_context */
508 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
509 sizeof(struct smb2_neg_context), 8) * 8;
510}
511
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500512static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500513build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
514{
515 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
516 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
Steve French0d481322019-02-24 17:56:33 -0600517 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
518 pneg_ctxt->Name[0] = 0x93;
519 pneg_ctxt->Name[1] = 0xAD;
520 pneg_ctxt->Name[2] = 0x25;
521 pneg_ctxt->Name[3] = 0x50;
522 pneg_ctxt->Name[4] = 0x9C;
523 pneg_ctxt->Name[5] = 0xB4;
524 pneg_ctxt->Name[6] = 0x11;
525 pneg_ctxt->Name[7] = 0xE7;
526 pneg_ctxt->Name[8] = 0xB4;
527 pneg_ctxt->Name[9] = 0x23;
528 pneg_ctxt->Name[10] = 0x83;
529 pneg_ctxt->Name[11] = 0xDE;
530 pneg_ctxt->Name[12] = 0x96;
531 pneg_ctxt->Name[13] = 0x8B;
532 pneg_ctxt->Name[14] = 0xCD;
533 pneg_ctxt->Name[15] = 0x7C;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500534}
535
536static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100537assemble_neg_contexts(struct smb2_negotiate_req *req,
Steve French9fe5ff12019-06-24 20:39:04 -0500538 struct TCP_Server_Info *server, unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500539{
Steve Frenchd5c70762019-01-03 02:37:21 -0600540 char *pneg_ctxt = (char *)req;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500541 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500542
Steve Frenchd5c70762019-01-03 02:37:21 -0600543 if (*total_len > 200) {
544 /* In case length corrupted don't want to overrun smb buffer */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000545 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
Steve Frenchd5c70762019-01-03 02:37:21 -0600546 return;
547 }
548
549 /*
550 * round up total_len of fixed part of SMB3 negotiate request to 8
551 * byte boundary before adding negotiate contexts
552 */
553 *total_len = roundup(*total_len, 8);
554
555 pneg_ctxt = (*total_len) + (char *)req;
556 req->NegotiateContextOffset = cpu_to_le32(*total_len);
557
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500558 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500559 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
560 *total_len += ctxt_len;
561 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100562
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500563 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500564 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
565 *total_len += ctxt_len;
566 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100567
Steve French9fe5ff12019-06-24 20:39:04 -0500568 if (server->compress_algorithm) {
569 build_compression_ctxt((struct smb2_compression_capabilities_context *)
Steve French26ea8882019-04-26 20:36:08 -0700570 pneg_ctxt);
Steve French9fe5ff12019-06-24 20:39:04 -0500571 ctxt_len = DIV_ROUND_UP(
572 sizeof(struct smb2_compression_capabilities_context),
573 8) * 8;
574 *total_len += ctxt_len;
575 pneg_ctxt += ctxt_len;
Steve French96d3cca2019-06-25 04:39:51 -0500576 req->NegotiateContextCount = cpu_to_le16(5);
Steve French9fe5ff12019-06-24 20:39:04 -0500577 } else
Steve French96d3cca2019-06-25 04:39:51 -0500578 req->NegotiateContextCount = cpu_to_le16(4);
579
580 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
581 server->hostname);
582 *total_len += ctxt_len;
583 pneg_ctxt += ctxt_len;
584
Steve Frenchfcef0db2018-05-19 20:45:27 -0500585 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
586 *total_len += sizeof(struct smb2_posix_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500587}
Steve French5100d8a2018-04-09 10:47:14 -0500588
589static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
590{
591 unsigned int len = le16_to_cpu(ctxt->DataLength);
592
593 /* If invalid preauth context warn but use what we requested, SHA-512 */
594 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
595 printk_once(KERN_WARNING "server sent bad preauth context\n");
596 return;
597 }
598 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
599 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
600 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
601 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
602}
603
Steve French26ea8882019-04-26 20:36:08 -0700604static void decode_compress_ctx(struct TCP_Server_Info *server,
605 struct smb2_compression_capabilities_context *ctxt)
606{
607 unsigned int len = le16_to_cpu(ctxt->DataLength);
608
609 /* sizeof compress context is a one element compression capbility struct */
610 if (len < 10) {
611 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
612 return;
613 }
614 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
615 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
616 return;
617 }
618 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
619 printk_once(KERN_WARNING "unknown compression algorithm\n");
620 return;
621 }
622 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
623}
624
Steve French5100d8a2018-04-09 10:47:14 -0500625static int decode_encrypt_ctx(struct TCP_Server_Info *server,
626 struct smb2_encryption_neg_context *ctxt)
627{
628 unsigned int len = le16_to_cpu(ctxt->DataLength);
629
630 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
631 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
632 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
633 return -EINVAL;
634 }
635
636 if (le16_to_cpu(ctxt->CipherCount) != 1) {
637 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
638 return -EINVAL;
639 }
640 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
641 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
642 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
643 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
644 return -EINVAL;
645 }
646 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500647 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500648 return 0;
649}
650
651static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000652 struct TCP_Server_Info *server,
653 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500654{
655 struct smb2_neg_context *pctx;
656 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
657 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500658 unsigned int len_of_ctxts, i;
659 int rc = 0;
660
661 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
662 if (len_of_smb <= offset) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000663 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
Steve French5100d8a2018-04-09 10:47:14 -0500664 return -EINVAL;
665 }
666
667 len_of_ctxts = len_of_smb - offset;
668
669 for (i = 0; i < ctxt_cnt; i++) {
670 int clen;
671 /* check that offset is not beyond end of SMB */
672 if (len_of_ctxts == 0)
673 break;
674
675 if (len_of_ctxts < sizeof(struct smb2_neg_context))
676 break;
677
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000678 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500679 clen = le16_to_cpu(pctx->DataLength);
680 if (clen > len_of_ctxts)
681 break;
682
683 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
684 decode_preauth_context(
685 (struct smb2_preauth_neg_context *)pctx);
686 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
687 rc = decode_encrypt_ctx(server,
688 (struct smb2_encryption_neg_context *)pctx);
Steve French26ea8882019-04-26 20:36:08 -0700689 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
690 decode_compress_ctx(server,
691 (struct smb2_compression_capabilities_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500692 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
693 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500694 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000695 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
Steve French5100d8a2018-04-09 10:47:14 -0500696 le16_to_cpu(pctx->ContextType));
697
698 if (rc)
699 break;
700 /* offsets must be 8 byte aligned */
701 clen = (clen + 7) & ~0x7;
702 offset += clen + sizeof(struct smb2_neg_context);
703 len_of_ctxts -= clen;
704 }
705 return rc;
706}
707
Steve Frenchce558b02018-05-31 19:16:54 -0500708static struct create_posix *
709create_posix_buf(umode_t mode)
710{
711 struct create_posix *buf;
712
713 buf = kzalloc(sizeof(struct create_posix),
714 GFP_KERNEL);
715 if (!buf)
716 return NULL;
717
718 buf->ccontext.DataOffset =
719 cpu_to_le16(offsetof(struct create_posix, Mode));
720 buf->ccontext.DataLength = cpu_to_le32(4);
721 buf->ccontext.NameOffset =
722 cpu_to_le16(offsetof(struct create_posix, Name));
723 buf->ccontext.NameLength = cpu_to_le16(16);
724
725 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
726 buf->Name[0] = 0x93;
727 buf->Name[1] = 0xAD;
728 buf->Name[2] = 0x25;
729 buf->Name[3] = 0x50;
730 buf->Name[4] = 0x9C;
731 buf->Name[5] = 0xB4;
732 buf->Name[6] = 0x11;
733 buf->Name[7] = 0xE7;
734 buf->Name[8] = 0xB4;
735 buf->Name[9] = 0x23;
736 buf->Name[10] = 0x83;
737 buf->Name[11] = 0xDE;
738 buf->Name[12] = 0x96;
739 buf->Name[13] = 0x8B;
740 buf->Name[14] = 0xCD;
741 buf->Name[15] = 0x7C;
742 buf->Mode = cpu_to_le32(mode);
743 cifs_dbg(FYI, "mode on posix create 0%o", mode);
744 return buf;
745}
746
747static int
748add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
749{
750 struct smb2_create_req *req = iov[0].iov_base;
751 unsigned int num = *num_iovec;
752
753 iov[num].iov_base = create_posix_buf(mode);
754 if (iov[num].iov_base == NULL)
755 return -ENOMEM;
756 iov[num].iov_len = sizeof(struct create_posix);
757 if (!req->CreateContextsOffset)
758 req->CreateContextsOffset = cpu_to_le32(
759 sizeof(struct smb2_create_req) +
760 iov[num - 1].iov_len);
761 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
762 *num_iovec = num + 1;
763 return 0;
764}
765
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500766
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400767/*
768 *
769 * SMB2 Worker functions follow:
770 *
771 * The general structure of the worker functions is:
772 * 1) Call smb2_init (assembles SMB2 header)
773 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
774 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
775 * 4) Decode SMB2 command specific fields in the fixed length area
776 * 5) Decode variable length data area (if any for this SMB2 command type)
777 * 6) Call free smb buffer
778 * 7) return
779 *
780 */
781
782int
783SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
784{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000785 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400786 struct smb2_negotiate_req *req;
787 struct smb2_negotiate_rsp *rsp;
788 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700789 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400790 int rc = 0;
791 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400792 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400793 int blob_offset, blob_length;
794 char *security_blob;
795 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100796 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400797
Joe Perchesf96637b2013-05-04 22:12:25 -0500798 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400799
Jeff Layton3534b852013-05-24 07:41:01 -0400800 if (!server) {
801 WARN(1, "%s: server is NULL!\n", __func__);
802 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400803 }
804
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100805 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400806 if (rc)
807 return rc;
808
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100809 req->sync_hdr.SessionId = 0;
Steve French0fdfef92018-06-28 19:30:23 -0500810
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100811 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
812 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400813
Steve French9764c022017-09-17 10:41:35 -0500814 if (strcmp(ses->server->vals->version_string,
815 SMB3ANY_VERSION_STRING) == 0) {
816 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
817 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
818 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100819 total_len += 4;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000820 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500821 SMBDEFAULT_VERSION_STRING) == 0) {
822 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
823 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
824 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600825 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
826 req->DialectCount = cpu_to_le16(4);
827 total_len += 8;
Steve French9764c022017-09-17 10:41:35 -0500828 } else {
829 /* otherwise send specific dialect */
830 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
831 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100832 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500833 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400834
835 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400836 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500837 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400838 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500839 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400840 else
841 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400842
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000843 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400844
Steve French3c5f9be12014-05-13 13:37:45 -0700845 /* ClientGUID must be zero for SMB2.02 dialect */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000846 if (server->vals->protocol_id == SMB20_PROT_ID)
Steve French3c5f9be12014-05-13 13:37:45 -0700847 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500848 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700849 memcpy(req->ClientGUID, server->client_guid,
850 SMB2_CLIENT_GUID_SIZE);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000851 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
852 (strcmp(server->vals->version_string,
Steve Frenchd5c70762019-01-03 02:37:21 -0600853 SMBDEFAULT_VERSION_STRING) == 0))
Steve French9fe5ff12019-06-24 20:39:04 -0500854 assemble_neg_contexts(req, server, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500855 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400856 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100857 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400858
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000859 memset(&rqst, 0, sizeof(struct smb_rqst));
860 rqst.rq_iov = iov;
861 rqst.rq_nvec = 1;
862
863 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700864 cifs_small_buf_release(req);
865 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400866 /*
867 * No tcon so can't do
868 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
869 */
Steve French7e682f72017-08-31 21:34:24 -0500870 if (rc == -EOPNOTSUPP) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000871 cifs_server_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500872 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500873 " older servers\n");
874 goto neg_exit;
875 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400876 goto neg_exit;
877
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000878 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500879 SMB3ANY_VERSION_STRING) == 0) {
880 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000881 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500882 "SMB2 dialect returned but not requested\n");
883 return -EIO;
884 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000885 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500886 "SMB2.1 dialect returned but not requested\n");
887 return -EIO;
888 }
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000889 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500890 SMBDEFAULT_VERSION_STRING) == 0) {
891 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000892 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500893 "SMB2 dialect returned but not requested\n");
894 return -EIO;
895 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
896 /* ops set to 3.0 by default for default so update */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000897 server->ops = &smb21_operations;
898 server->vals = &smb21_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800899 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000900 server->ops = &smb311_operations;
901 server->vals = &smb311_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800902 }
Steve French590d08d2017-09-19 11:43:47 -0500903 } else if (le16_to_cpu(rsp->DialectRevision) !=
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000904 server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500905 /* if requested single dialect ensure returned dialect matched */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000906 cifs_server_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500907 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500908 return -EIO;
909 }
910
Joe Perchesf96637b2013-05-04 22:12:25 -0500911 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400912
Steve Frenche4aa25e2012-10-01 12:26:22 -0500913 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500914 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500915 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500916 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500917 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500918 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500919 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
920 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600921 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
922 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400923 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000924 cifs_server_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500925 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400926 rc = -EIO;
927 goto neg_exit;
928 }
929 server->dialect = le16_to_cpu(rsp->DialectRevision);
930
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100931 /*
932 * Keep a copy of the hash after negprot. This hash will be
933 * the starting hash value for all sessions made from this
934 * server.
935 */
936 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
937 SMB2_PREAUTH_HASH_SIZE);
Steve French0fdfef92018-06-28 19:30:23 -0500938
Jeff Laytone598d1d82013-05-26 07:00:59 -0400939 /* SMB2 only has an extended negflavor */
940 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400941 /* set it to the maximum buffer size value we can send with 1 credit */
942 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
943 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400944 server->max_read = le32_to_cpu(rsp->MaxReadSize);
945 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400946 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500947 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
948 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
949 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400950 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400951 /* Internal types */
952 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400953
954 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000955 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500956 /*
957 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
958 * for us will be
959 * ses->sectype = RawNTLMSSP;
960 * but for time being this is our only auth choice so doesn't matter.
961 * We just found a server which sets blob length to zero expecting raw.
962 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700963 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500964 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700965 server->sec_ntlmssp = true;
966 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700967
Jeff Layton38d77c52013-05-26 07:01:00 -0400968 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400969 if (rc)
970 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500971 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500972 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500973 if (rc == 1)
974 rc = 0;
975 else if (rc == 0)
976 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400977 }
Steve French5100d8a2018-04-09 10:47:14 -0500978
Steve French5100d8a2018-04-09 10:47:14 -0500979 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
980 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000981 rc = smb311_decode_neg_context(rsp, server,
982 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -0500983 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000984 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
Steve French5100d8a2018-04-09 10:47:14 -0500985 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400986neg_exit:
987 free_rsp_buf(resp_buftype, rsp);
988 return rc;
989}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400990
Steve Frenchff1c0382013-11-19 23:44:46 -0600991int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
992{
Long Li2796d302018-04-25 11:30:04 -0700993 int rc;
994 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200995 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600996 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500997 u32 inbuflen; /* max of 4 dialects */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000998 struct TCP_Server_Info *server = tcon->ses->server;
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 */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001003 if (server->dialect == SMB311_PROT_ID)
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001004 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)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001022 cifs_server_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
Steve French0603c962017-09-20 19:57:18 -05001023
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 =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001029 cpu_to_le32(server->vals->req_capabilities);
1030 memcpy(pneg_inbuf->Guid, 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
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001043 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001044 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]));
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001051 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001052 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] =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001063 cpu_to_le16(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 */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001079 cifs_server_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) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001083 cifs_server_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)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001090 cifs_server_dbg(VFS, "invalid protocol negotiate response size: %d\n",
Steve French7db0a6e2017-05-03 21:12:20 -05001091 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 */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001099 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -06001100 goto vneg_out;
1101
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001102 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
Steve Frenchff1c0382013-11-19 23:44:46 -06001103 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 |
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001108 SMB2_LARGE_FILES) != server->capabilities)
Steve Frenchff1c0382013-11-19 23:44:46 -06001109 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:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001117 cifs_server_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -07001118out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001119 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -07001120out_free_inbuf:
1121 kfree(pneg_inbuf);
1122 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -06001123}
1124
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301125enum securityEnum
1126smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1127{
1128 switch (requested) {
1129 case Kerberos:
1130 case RawNTLMSSP:
1131 return requested;
1132 case NTLMv2:
1133 return RawNTLMSSP;
1134 case Unspecified:
1135 if (server->sec_ntlmssp &&
1136 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1137 return RawNTLMSSP;
1138 if ((server->sec_kerberos || server->sec_mskerberos) &&
1139 (global_secflags & CIFSSEC_MAY_KRB5))
1140 return Kerberos;
1141 /* Fallthrough */
1142 default:
1143 return Unspecified;
1144 }
1145}
1146
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001147struct SMB2_sess_data {
1148 unsigned int xid;
1149 struct cifs_ses *ses;
1150 struct nls_table *nls_cp;
1151 void (*func)(struct SMB2_sess_data *);
1152 int result;
1153 u64 previous_session;
1154
1155 /* we will send the SMB in three pieces:
1156 * a fixed length beginning part, an optional
1157 * SPNEGO blob (which can be zero length), and a
1158 * last part which will include the strings
1159 * and rest of bcc area. This allows us to avoid
1160 * a large buffer 17K allocation
1161 */
1162 int buf0_type;
1163 struct kvec iov[2];
1164};
1165
1166static int
1167SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1168{
1169 int rc;
1170 struct cifs_ses *ses = sess_data->ses;
1171 struct smb2_sess_setup_req *req;
1172 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001173 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001174
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001175 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1176 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001177 if (rc)
1178 return rc;
1179
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001180 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001181 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001182
1183 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1184 req->PreviousSessionId = sess_data->previous_session;
1185
1186 req->Flags = 0; /* MBZ */
Steve Frenchd4090142018-06-13 17:05:58 -05001187
1188 /* enough to enable echos and oplocks and one max size write */
1189 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001190
1191 /* only one of SMB2 signing flags may be set in SMB2 request */
1192 if (server->sign)
1193 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1194 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1195 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1196 else
1197 req->SecurityMode = 0;
1198
Steve French8d330962019-07-25 18:13:10 -05001199#ifdef CONFIG_CIFS_DFS_UPCALL
1200 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1201#else
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001202 req->Capabilities = 0;
Steve French8d330962019-07-25 18:13:10 -05001203#endif /* DFS_UPCALL */
1204
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001205 req->Channel = 0; /* MBZ */
1206
1207 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001208 /* 1 for pad */
1209 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001210 /*
1211 * This variable will be used to clear the buffer
1212 * allocated above in case of any error in the calling function.
1213 */
1214 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1215
1216 return 0;
1217}
1218
1219static void
1220SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1221{
1222 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1223 sess_data->buf0_type = CIFS_NO_BUFFER;
1224}
1225
1226static int
1227SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1228{
1229 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001230 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001231 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001232 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001233
1234 /* Testing shows that buffer offset must be at location of Buffer[0] */
1235 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001236 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001237 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1238
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001239 memset(&rqst, 0, sizeof(struct smb_rqst));
1240 rqst.rq_iov = sess_data->iov;
1241 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001242
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001243 /* BB add code to build os and lm fields */
1244 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1245 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001246 &sess_data->buf0_type,
1247 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001248 cifs_small_buf_release(sess_data->iov[0].iov_base);
1249 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001250
1251 return rc;
1252}
1253
1254static int
1255SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1256{
1257 int rc = 0;
1258 struct cifs_ses *ses = sess_data->ses;
1259
1260 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001261 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001262 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001263 if (rc) {
1264 cifs_dbg(FYI,
1265 "SMB3 session key generation failed\n");
1266 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001267 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001268 }
1269 }
1270 if (!ses->server->session_estab) {
1271 ses->server->sequence_number = 0x2;
1272 ses->server->session_estab = true;
1273 }
1274 mutex_unlock(&ses->server->srv_mutex);
1275
1276 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1277 spin_lock(&GlobalMid_Lock);
1278 ses->status = CifsGood;
1279 ses->need_reconnect = false;
1280 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001281 return rc;
1282}
1283
1284#ifdef CONFIG_CIFS_UPCALL
1285static void
1286SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1287{
1288 int rc;
1289 struct cifs_ses *ses = sess_data->ses;
1290 struct cifs_spnego_msg *msg;
1291 struct key *spnego_key = NULL;
1292 struct smb2_sess_setup_rsp *rsp = NULL;
1293
1294 rc = SMB2_sess_alloc_buffer(sess_data);
1295 if (rc)
1296 goto out;
1297
1298 spnego_key = cifs_get_spnego_key(ses);
1299 if (IS_ERR(spnego_key)) {
1300 rc = PTR_ERR(spnego_key);
1301 spnego_key = NULL;
1302 goto out;
1303 }
1304
1305 msg = spnego_key->payload.data[0];
1306 /*
1307 * check version field to make sure that cifs.upcall is
1308 * sending us a response in an expected form
1309 */
1310 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1311 cifs_dbg(VFS,
1312 "bad cifs.upcall version. Expected %d got %d",
1313 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1314 rc = -EKEYREJECTED;
1315 goto out_put_spnego_key;
1316 }
1317
1318 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1319 GFP_KERNEL);
1320 if (!ses->auth_key.response) {
1321 cifs_dbg(VFS,
1322 "Kerberos can't allocate (%u bytes) memory",
1323 msg->sesskey_len);
1324 rc = -ENOMEM;
1325 goto out_put_spnego_key;
1326 }
1327 ses->auth_key.len = msg->sesskey_len;
1328
1329 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1330 sess_data->iov[1].iov_len = msg->secblob_len;
1331
1332 rc = SMB2_sess_sendreceive(sess_data);
1333 if (rc)
1334 goto out_put_spnego_key;
1335
1336 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001337 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001338
1339 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001340
1341 rc = SMB2_sess_establish_session(sess_data);
1342out_put_spnego_key:
1343 key_invalidate(spnego_key);
1344 key_put(spnego_key);
1345out:
1346 sess_data->result = rc;
1347 sess_data->func = NULL;
1348 SMB2_sess_free_buffer(sess_data);
1349}
1350#else
1351static void
1352SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1353{
1354 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1355 sess_data->result = -EOPNOTSUPP;
1356 sess_data->func = NULL;
1357}
1358#endif
1359
Sachin Prabhu166cea42016-10-07 19:11:22 +01001360static void
1361SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1362
1363static void
1364SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1365{
1366 int rc;
1367 struct cifs_ses *ses = sess_data->ses;
1368 struct smb2_sess_setup_rsp *rsp = NULL;
1369 char *ntlmssp_blob = NULL;
1370 bool use_spnego = false; /* else use raw ntlmssp */
1371 u16 blob_length = 0;
1372
1373 /*
1374 * If memory allocation is successful, caller of this function
1375 * frees it.
1376 */
1377 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1378 if (!ses->ntlmssp) {
1379 rc = -ENOMEM;
1380 goto out_err;
1381 }
1382 ses->ntlmssp->sesskey_per_smbsess = true;
1383
1384 rc = SMB2_sess_alloc_buffer(sess_data);
1385 if (rc)
1386 goto out_err;
1387
1388 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1389 GFP_KERNEL);
1390 if (ntlmssp_blob == NULL) {
1391 rc = -ENOMEM;
1392 goto out;
1393 }
1394
1395 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1396 if (use_spnego) {
1397 /* BB eventually need to add this */
1398 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1399 rc = -EOPNOTSUPP;
1400 goto out;
1401 } else {
1402 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1403 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1404 }
1405 sess_data->iov[1].iov_base = ntlmssp_blob;
1406 sess_data->iov[1].iov_len = blob_length;
1407
1408 rc = SMB2_sess_sendreceive(sess_data);
1409 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1410
1411 /* If true, rc here is expected and not an error */
1412 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001413 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001414 rc = 0;
1415
1416 if (rc)
1417 goto out;
1418
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001419 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001420 le16_to_cpu(rsp->SecurityBufferOffset)) {
1421 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1422 le16_to_cpu(rsp->SecurityBufferOffset));
1423 rc = -EIO;
1424 goto out;
1425 }
1426 rc = decode_ntlmssp_challenge(rsp->Buffer,
1427 le16_to_cpu(rsp->SecurityBufferLength), ses);
1428 if (rc)
1429 goto out;
1430
1431 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1432
1433
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001434 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001435 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001436
1437out:
1438 kfree(ntlmssp_blob);
1439 SMB2_sess_free_buffer(sess_data);
1440 if (!rc) {
1441 sess_data->result = 0;
1442 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1443 return;
1444 }
1445out_err:
1446 kfree(ses->ntlmssp);
1447 ses->ntlmssp = NULL;
1448 sess_data->result = rc;
1449 sess_data->func = NULL;
1450}
1451
1452static void
1453SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1454{
1455 int rc;
1456 struct cifs_ses *ses = sess_data->ses;
1457 struct smb2_sess_setup_req *req;
1458 struct smb2_sess_setup_rsp *rsp = NULL;
1459 unsigned char *ntlmssp_blob = NULL;
1460 bool use_spnego = false; /* else use raw ntlmssp */
1461 u16 blob_length = 0;
1462
1463 rc = SMB2_sess_alloc_buffer(sess_data);
1464 if (rc)
1465 goto out;
1466
1467 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001468 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001469
1470 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1471 sess_data->nls_cp);
1472 if (rc) {
1473 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1474 goto out;
1475 }
1476
1477 if (use_spnego) {
1478 /* BB eventually need to add this */
1479 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1480 rc = -EOPNOTSUPP;
1481 goto out;
1482 }
1483 sess_data->iov[1].iov_base = ntlmssp_blob;
1484 sess_data->iov[1].iov_len = blob_length;
1485
1486 rc = SMB2_sess_sendreceive(sess_data);
1487 if (rc)
1488 goto out;
1489
1490 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1491
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001492 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001493 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001494
1495 rc = SMB2_sess_establish_session(sess_data);
1496out:
1497 kfree(ntlmssp_blob);
1498 SMB2_sess_free_buffer(sess_data);
1499 kfree(ses->ntlmssp);
1500 ses->ntlmssp = NULL;
1501 sess_data->result = rc;
1502 sess_data->func = NULL;
1503}
1504
1505static int
1506SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1507{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301508 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001509
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301510 type = smb2_select_sectype(ses->server, ses->sectype);
1511 cifs_dbg(FYI, "sess setup type %d\n", type);
1512 if (type == Unspecified) {
1513 cifs_dbg(VFS,
1514 "Unable to select appropriate authentication method!");
1515 return -EINVAL;
1516 }
1517
1518 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001519 case Kerberos:
1520 sess_data->func = SMB2_auth_kerberos;
1521 break;
1522 case RawNTLMSSP:
1523 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1524 break;
1525 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301526 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001527 return -EOPNOTSUPP;
1528 }
1529
1530 return 0;
1531}
1532
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001533int
1534SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1535 const struct nls_table *nls_cp)
1536{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001537 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001538 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001539 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001540
Joe Perchesf96637b2013-05-04 22:12:25 -05001541 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001542
Jeff Layton3534b852013-05-24 07:41:01 -04001543 if (!server) {
1544 WARN(1, "%s: server is NULL!\n", __func__);
1545 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001546 }
1547
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001548 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1549 if (!sess_data)
1550 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001551
1552 rc = SMB2_select_sec(ses, sess_data);
1553 if (rc)
1554 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001555 sess_data->xid = xid;
1556 sess_data->ses = ses;
1557 sess_data->buf0_type = CIFS_NO_BUFFER;
1558 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001559 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001560
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001561 /*
1562 * Initialize the session hash with the server one.
1563 */
1564 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1565 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001566
Sachin Prabhu166cea42016-10-07 19:11:22 +01001567 while (sess_data->func)
1568 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001569
Steve Frenchc721c382017-09-19 18:40:03 -05001570 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001571 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001572 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001573out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001574 kfree(sess_data);
1575 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001576}
1577
1578int
1579SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1580{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001581 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001582 struct smb2_logoff_req *req; /* response is also trivial struct */
1583 int rc = 0;
1584 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001585 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001586 unsigned int total_len;
1587 struct kvec iov[1];
1588 struct kvec rsp_iov;
1589 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001590
Joe Perchesf96637b2013-05-04 22:12:25 -05001591 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001592
1593 if (ses && (ses->server))
1594 server = ses->server;
1595 else
1596 return -EIO;
1597
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001598 /* no need to send SMB logoff if uid already closed due to reconnect */
1599 if (ses->need_reconnect)
1600 goto smb2_session_already_dead;
1601
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001602 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001603 if (rc)
1604 return rc;
1605
1606 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001607 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001608
1609 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1610 flags |= CIFS_TRANSFORM_REQ;
1611 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001612 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001613
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001614 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001615
1616 iov[0].iov_base = (char *)req;
1617 iov[0].iov_len = total_len;
1618
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001619 memset(&rqst, 0, sizeof(struct smb_rqst));
1620 rqst.rq_iov = iov;
1621 rqst.rq_nvec = 1;
1622
1623 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001624 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001625 /*
1626 * No tcon so can't do
1627 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1628 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001629
1630smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001631 return rc;
1632}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001633
1634static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1635{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001636 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001637}
1638
1639#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1640
Steve Frenchde9f68df2013-11-15 11:26:24 -06001641/* These are similar values to what Windows uses */
1642static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1643{
1644 tcon->max_chunks = 256;
1645 tcon->max_bytes_chunk = 1048576;
1646 tcon->max_bytes_copy = 16777216;
1647}
1648
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001649int
1650SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1651 struct cifs_tcon *tcon, const struct nls_table *cp)
1652{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001653 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001654 struct smb2_tree_connect_req *req;
1655 struct smb2_tree_connect_rsp *rsp = NULL;
1656 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001657 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001658 int rc = 0;
1659 int resp_buftype;
1660 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001661 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001662 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001663 unsigned int total_len;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001664 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001665
Joe Perchesf96637b2013-05-04 22:12:25 -05001666 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001667
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001668 if (!server || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001669 return -EIO;
1670
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001671 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1672 if (unc_path == NULL)
1673 return -ENOMEM;
1674
1675 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1676 unc_path_len *= 2;
1677 if (unc_path_len < 2) {
1678 kfree(unc_path);
1679 return -EINVAL;
1680 }
1681
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001682 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001683 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001684 atomic_set(&tcon->num_remote_opens, 0);
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001685 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1686 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001687 if (rc) {
1688 kfree(unc_path);
1689 return rc;
1690 }
1691
Steve French5a77e752018-05-09 17:43:08 -05001692 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001693 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001694
1695 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001696 /* 1 for pad */
1697 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001698
1699 /* Testing shows that buffer offset must be at location of Buffer[0] */
1700 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001701 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001702 req->PathLength = cpu_to_le16(unc_path_len - 2);
1703 iov[1].iov_base = unc_path;
1704 iov[1].iov_len = unc_path_len;
1705
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001706 /*
1707 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1708 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
Steve French8c11a602019-03-22 22:31:17 -05001709 * (Samba servers don't always set the flag so also check if null user)
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001710 */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001711 if ((server->dialect == SMB311_PROT_ID) &&
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001712 !smb3_encryption_required(tcon) &&
Steve French8c11a602019-03-22 22:31:17 -05001713 !(ses->session_flags &
1714 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1715 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
Steve French6188f282018-03-13 02:29:36 -05001716 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1717
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001718 memset(&rqst, 0, sizeof(struct smb_rqst));
1719 rqst.rq_iov = iov;
1720 rqst.rq_nvec = 2;
1721
Steve French4fe75c42019-02-14 01:19:02 -06001722 /* Need 64 for max size write so ask for more in case not there yet */
1723 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1724
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001725 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001726 cifs_small_buf_release(req);
1727 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001728 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001729 if (rc != 0) {
1730 if (tcon) {
1731 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1732 tcon->need_reconnect = true;
1733 }
1734 goto tcon_error_exit;
1735 }
1736
Christophe JAILLETcd123002017-05-12 17:59:32 +02001737 switch (rsp->ShareType) {
1738 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001739 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001740 break;
1741 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001742 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001743 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001744 break;
1745 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001746 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001747 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001748 break;
1749 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001750 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001751 rc = -EOPNOTSUPP;
1752 goto tcon_error_exit;
1753 }
1754
1755 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001756 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001757 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1758 tcon->tidStatus = CifsGood;
1759 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001760 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001761 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001762
1763 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1764 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001765 cifs_server_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001766
1767 if (tcon->seal &&
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001768 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1769 cifs_server_dbg(VFS, "Encryption is requested but not supported\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001770
Steve Frenchde9f68df2013-11-15 11:26:24 -06001771 init_copy_chunk_defaults(tcon);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001772 if (server->ops->validate_negotiate)
1773 rc = server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001774tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001775
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001776 free_rsp_buf(resp_buftype, rsp);
1777 kfree(unc_path);
1778 return rc;
1779
1780tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001781 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001782 cifs_server_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001783 }
1784 goto tcon_exit;
1785}
1786
1787int
1788SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1789{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001790 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001791 struct smb2_tree_disconnect_req *req; /* response is trivial */
1792 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001793 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001794 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001795 unsigned int total_len;
1796 struct kvec iov[1];
1797 struct kvec rsp_iov;
1798 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001799
Joe Perchesf96637b2013-05-04 22:12:25 -05001800 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001801
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001802 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001803 return -EIO;
1804
1805 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1806 return 0;
1807
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001808 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1809 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001810 if (rc)
1811 return rc;
1812
Steve French5a77e752018-05-09 17:43:08 -05001813 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001814 flags |= CIFS_TRANSFORM_REQ;
1815
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001816 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001817
1818 iov[0].iov_base = (char *)req;
1819 iov[0].iov_len = total_len;
1820
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001821 memset(&rqst, 0, sizeof(struct smb_rqst));
1822 rqst.rq_iov = iov;
1823 rqst.rq_nvec = 1;
1824
1825 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001826 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001827 if (rc)
1828 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1829
1830 return rc;
1831}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001832
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001833
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001834static struct create_durable *
1835create_durable_buf(void)
1836{
1837 struct create_durable *buf;
1838
1839 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1840 if (!buf)
1841 return NULL;
1842
1843 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001844 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001845 buf->ccontext.DataLength = cpu_to_le32(16);
1846 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1847 (struct create_durable, Name));
1848 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001849 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001850 buf->Name[0] = 'D';
1851 buf->Name[1] = 'H';
1852 buf->Name[2] = 'n';
1853 buf->Name[3] = 'Q';
1854 return buf;
1855}
1856
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001857static struct create_durable *
1858create_reconnect_durable_buf(struct cifs_fid *fid)
1859{
1860 struct create_durable *buf;
1861
1862 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1863 if (!buf)
1864 return NULL;
1865
1866 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1867 (struct create_durable, Data));
1868 buf->ccontext.DataLength = cpu_to_le32(16);
1869 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1870 (struct create_durable, Name));
1871 buf->ccontext.NameLength = cpu_to_le16(4);
1872 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1873 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001874 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001875 buf->Name[0] = 'D';
1876 buf->Name[1] = 'H';
1877 buf->Name[2] = 'n';
1878 buf->Name[3] = 'C';
1879 return buf;
1880}
1881
Steve French89a5bfa2019-07-18 17:22:18 -05001882static void
1883parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1884{
1885 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1886
1887 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1888 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1889 buf->IndexNumber = pdisk_id->DiskFileId;
1890}
1891
1892void
1893smb2_parse_contexts(struct TCP_Server_Info *server,
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +10001894 struct smb2_create_rsp *rsp,
Steve French89a5bfa2019-07-18 17:22:18 -05001895 unsigned int *epoch, char *lease_key, __u8 *oplock,
1896 struct smb2_file_all_info *buf)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001897{
1898 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001899 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001900 unsigned int next;
1901 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001902 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001903
Steve French89a5bfa2019-07-18 17:22:18 -05001904 *oplock = 0;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001905 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001906 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001907 cc = (struct create_context *)data_offset;
Steve French89a5bfa2019-07-18 17:22:18 -05001908
1909 /* Initialize inode number to 0 in case no valid data in qfid context */
1910 if (buf)
1911 buf->IndexNumber = 0;
1912
Justin Maggarddeb7def2016-02-09 15:52:08 -08001913 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001914 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001915 if (le16_to_cpu(cc->NameLength) == 4 &&
Steve French89a5bfa2019-07-18 17:22:18 -05001916 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1917 *oplock = server->ops->parse_lease_buf(cc, epoch,
1918 lease_key);
1919 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1920 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1921 parse_query_id_ctxt(cc, buf);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001922
1923 next = le32_to_cpu(cc->Next);
1924 if (!next)
1925 break;
1926 remaining -= next;
1927 cc = (struct create_context *)((char *)cc + next);
1928 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001929
Steve French89a5bfa2019-07-18 17:22:18 -05001930 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1931 *oplock = rsp->OplockLevel;
1932
1933 return;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001934}
1935
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001936static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001937add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02001938 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001939{
1940 struct smb2_create_req *req = iov[0].iov_base;
1941 unsigned int num = *num_iovec;
1942
Stefano Brivio729c0c92018-07-05 15:10:02 +02001943 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001944 if (iov[num].iov_base == NULL)
1945 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001946 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001947 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1948 if (!req->CreateContextsOffset)
1949 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001950 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001951 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001952 le32_add_cpu(&req->CreateContextsLength,
1953 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001954 *num_iovec = num + 1;
1955 return 0;
1956}
1957
Steve Frenchb56eae42015-11-03 09:26:27 -06001958static struct create_durable_v2 *
Steve Frenchca567eb2019-03-29 16:31:07 -05001959create_durable_v2_buf(struct cifs_open_parms *oparms)
Steve Frenchb56eae42015-11-03 09:26:27 -06001960{
Steve Frenchca567eb2019-03-29 16:31:07 -05001961 struct cifs_fid *pfid = oparms->fid;
Steve Frenchb56eae42015-11-03 09:26:27 -06001962 struct create_durable_v2 *buf;
1963
1964 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1965 if (!buf)
1966 return NULL;
1967
1968 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1969 (struct create_durable_v2, dcontext));
1970 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1971 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1972 (struct create_durable_v2, Name));
1973 buf->ccontext.NameLength = cpu_to_le16(4);
1974
Steve Frenchca567eb2019-03-29 16:31:07 -05001975 /*
1976 * NB: Handle timeout defaults to 0, which allows server to choose
1977 * (most servers default to 120 seconds) and most clients default to 0.
1978 * This can be overridden at mount ("handletimeout=") if the user wants
1979 * a different persistent (or resilient) handle timeout for all opens
1980 * opens on a particular SMB3 mount.
1981 */
1982 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
Steve Frenchb56eae42015-11-03 09:26:27 -06001983 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001984 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001985 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1986
1987 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1988 buf->Name[0] = 'D';
1989 buf->Name[1] = 'H';
1990 buf->Name[2] = '2';
1991 buf->Name[3] = 'Q';
1992 return buf;
1993}
1994
1995static struct create_durable_handle_reconnect_v2 *
1996create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1997{
1998 struct create_durable_handle_reconnect_v2 *buf;
1999
2000 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2001 GFP_KERNEL);
2002 if (!buf)
2003 return NULL;
2004
2005 buf->ccontext.DataOffset =
2006 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2007 dcontext));
2008 buf->ccontext.DataLength =
2009 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2010 buf->ccontext.NameOffset =
2011 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2012 Name));
2013 buf->ccontext.NameLength = cpu_to_le16(4);
2014
2015 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2016 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2017 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2018 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2019
2020 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2021 buf->Name[0] = 'D';
2022 buf->Name[1] = 'H';
2023 buf->Name[2] = '2';
2024 buf->Name[3] = 'C';
2025 return buf;
2026}
2027
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002028static int
Steve Frenchb56eae42015-11-03 09:26:27 -06002029add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002030 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002031{
2032 struct smb2_create_req *req = iov[0].iov_base;
2033 unsigned int num = *num_iovec;
2034
Steve Frenchca567eb2019-03-29 16:31:07 -05002035 iov[num].iov_base = create_durable_v2_buf(oparms);
Steve Frenchb56eae42015-11-03 09:26:27 -06002036 if (iov[num].iov_base == NULL)
2037 return -ENOMEM;
2038 iov[num].iov_len = sizeof(struct create_durable_v2);
2039 if (!req->CreateContextsOffset)
2040 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002041 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002042 iov[1].iov_len);
2043 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002044 *num_iovec = num + 1;
2045 return 0;
2046}
2047
2048static int
2049add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2050 struct cifs_open_parms *oparms)
2051{
2052 struct smb2_create_req *req = iov[0].iov_base;
2053 unsigned int num = *num_iovec;
2054
2055 /* indicate that we don't need to relock the file */
2056 oparms->reconnect = false;
2057
2058 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2059 if (iov[num].iov_base == NULL)
2060 return -ENOMEM;
2061 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2062 if (!req->CreateContextsOffset)
2063 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002064 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002065 iov[1].iov_len);
2066 le32_add_cpu(&req->CreateContextsLength,
2067 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002068 *num_iovec = num + 1;
2069 return 0;
2070}
2071
2072static int
2073add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2074 struct cifs_open_parms *oparms, bool use_persistent)
2075{
2076 struct smb2_create_req *req = iov[0].iov_base;
2077 unsigned int num = *num_iovec;
2078
2079 if (use_persistent) {
2080 if (oparms->reconnect)
2081 return add_durable_reconnect_v2_context(iov, num_iovec,
2082 oparms);
2083 else
2084 return add_durable_v2_context(iov, num_iovec, oparms);
2085 }
2086
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002087 if (oparms->reconnect) {
2088 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2089 /* indicate that we don't need to relock the file */
2090 oparms->reconnect = false;
2091 } else
2092 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002093 if (iov[num].iov_base == NULL)
2094 return -ENOMEM;
2095 iov[num].iov_len = sizeof(struct create_durable);
2096 if (!req->CreateContextsOffset)
2097 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002098 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002099 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08002100 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002101 *num_iovec = num + 1;
2102 return 0;
2103}
2104
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002105/* See MS-SMB2 2.2.13.2.7 */
2106static struct crt_twarp_ctxt *
2107create_twarp_buf(__u64 timewarp)
2108{
2109 struct crt_twarp_ctxt *buf;
2110
2111 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2112 if (!buf)
2113 return NULL;
2114
2115 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2116 (struct crt_twarp_ctxt, Timestamp));
2117 buf->ccontext.DataLength = cpu_to_le32(8);
2118 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2119 (struct crt_twarp_ctxt, Name));
2120 buf->ccontext.NameLength = cpu_to_le16(4);
2121 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2122 buf->Name[0] = 'T';
2123 buf->Name[1] = 'W';
2124 buf->Name[2] = 'r';
2125 buf->Name[3] = 'p';
2126 buf->Timestamp = cpu_to_le64(timewarp);
2127 return buf;
2128}
2129
2130/* See MS-SMB2 2.2.13.2.7 */
2131static int
2132add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2133{
2134 struct smb2_create_req *req = iov[0].iov_base;
2135 unsigned int num = *num_iovec;
2136
2137 iov[num].iov_base = create_twarp_buf(timewarp);
2138 if (iov[num].iov_base == NULL)
2139 return -ENOMEM;
2140 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2141 if (!req->CreateContextsOffset)
2142 req->CreateContextsOffset = cpu_to_le32(
2143 sizeof(struct smb2_create_req) +
2144 iov[num - 1].iov_len);
2145 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2146 *num_iovec = num + 1;
2147 return 0;
2148}
2149
Steve Frenchff2a09e2019-07-06 14:41:38 -05002150static struct crt_query_id_ctxt *
2151create_query_id_buf(void)
2152{
2153 struct crt_query_id_ctxt *buf;
2154
2155 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2156 if (!buf)
2157 return NULL;
2158
2159 buf->ccontext.DataOffset = cpu_to_le16(0);
2160 buf->ccontext.DataLength = cpu_to_le32(0);
2161 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2162 (struct crt_query_id_ctxt, Name));
2163 buf->ccontext.NameLength = cpu_to_le16(4);
2164 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2165 buf->Name[0] = 'Q';
2166 buf->Name[1] = 'F';
2167 buf->Name[2] = 'i';
2168 buf->Name[3] = 'd';
2169 return buf;
2170}
2171
2172/* See MS-SMB2 2.2.13.2.9 */
2173static int
2174add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2175{
2176 struct smb2_create_req *req = iov[0].iov_base;
2177 unsigned int num = *num_iovec;
2178
2179 iov[num].iov_base = create_query_id_buf();
2180 if (iov[num].iov_base == NULL)
2181 return -ENOMEM;
2182 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2183 if (!req->CreateContextsOffset)
2184 req->CreateContextsOffset = cpu_to_le32(
2185 sizeof(struct smb2_create_req) +
2186 iov[num - 1].iov_len);
2187 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2188 *num_iovec = num + 1;
2189 return 0;
2190}
2191
Aurelien Aptelf0712922017-02-22 14:47:17 +01002192static int
2193alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2194 const char *treename, const __le16 *path)
2195{
2196 int treename_len, path_len;
2197 struct nls_table *cp;
2198 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2199
2200 /*
2201 * skip leading "\\"
2202 */
2203 treename_len = strlen(treename);
2204 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2205 return -EINVAL;
2206
2207 treename += 2;
2208 treename_len -= 2;
2209
2210 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2211
2212 /*
2213 * make room for one path separator between the treename and
2214 * path
2215 */
2216 *out_len = treename_len + 1 + path_len;
2217
2218 /*
2219 * final path needs to be null-terminated UTF16 with a
2220 * size aligned to 8
2221 */
2222
2223 *out_size = roundup((*out_len+1)*2, 8);
2224 *out_path = kzalloc(*out_size, GFP_KERNEL);
2225 if (!*out_path)
2226 return -ENOMEM;
2227
2228 cp = load_nls_default();
2229 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2230 UniStrcat(*out_path, sep);
2231 UniStrcat(*out_path, path);
2232 unload_nls(cp);
2233
2234 return 0;
2235}
2236
Steve Frenchbea851b2018-06-14 21:56:32 -05002237int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2238 umode_t mode, struct cifs_tcon *tcon,
2239 const char *full_path,
2240 struct cifs_sb_info *cifs_sb)
2241{
2242 struct smb_rqst rqst;
2243 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002244 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002245 struct cifs_ses *ses = tcon->ses;
2246 struct kvec iov[3]; /* make sure at least one for each open context */
2247 struct kvec rsp_iov = {NULL, 0};
2248 int resp_buftype;
2249 int uni_path_len;
2250 __le16 *copy_path = NULL;
2251 int copy_size;
2252 int rc = 0;
2253 unsigned int n_iov = 2;
2254 __u32 file_attributes = 0;
2255 char *pc_buf = NULL;
2256 int flags = 0;
2257 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002258 __le16 *utf16_path = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002259
2260 cifs_dbg(FYI, "mkdir\n");
2261
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002262 /* resource #1: path allocation */
2263 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2264 if (!utf16_path)
2265 return -ENOMEM;
2266
YueHaibing29cbfa12018-12-18 02:51:51 +00002267 if (!ses || !(ses->server)) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002268 rc = -EIO;
2269 goto err_free_path;
2270 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002271
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002272 /* resource #2: request */
Steve Frenchbea851b2018-06-14 21:56:32 -05002273 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002274 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002275 goto err_free_path;
2276
Steve Frenchbea851b2018-06-14 21:56:32 -05002277
2278 if (smb3_encryption_required(tcon))
2279 flags |= CIFS_TRANSFORM_REQ;
2280
Steve Frenchbea851b2018-06-14 21:56:32 -05002281 req->ImpersonationLevel = IL_IMPERSONATION;
2282 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2283 /* File attributes ignored on open (used in create though) */
2284 req->FileAttributes = cpu_to_le32(file_attributes);
2285 req->ShareAccess = FILE_SHARE_ALL_LE;
2286 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2287 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2288
2289 iov[0].iov_base = (char *)req;
2290 /* -1 since last byte is buf[0] which is sent below (path) */
2291 iov[0].iov_len = total_len - 1;
2292
2293 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2294
2295 /* [MS-SMB2] 2.2.13 NameOffset:
2296 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2297 * the SMB2 header, the file name includes a prefix that will
2298 * be processed during DFS name normalization as specified in
2299 * section 3.3.5.9. Otherwise, the file name is relative to
2300 * the share that is identified by the TreeId in the SMB2
2301 * header.
2302 */
2303 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2304 int name_len;
2305
2306 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2307 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2308 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002309 tcon->treeName, utf16_path);
2310 if (rc)
2311 goto err_free_req;
2312
Steve Frenchbea851b2018-06-14 21:56:32 -05002313 req->NameLength = cpu_to_le16(name_len * 2);
2314 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002315 /* free before overwriting resource */
2316 kfree(utf16_path);
2317 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002318 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002319 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002320 /* MUST set path len (NameLength) to 0 opening root of share */
2321 req->NameLength = cpu_to_le16(uni_path_len - 2);
2322 if (uni_path_len % 8 != 0) {
2323 copy_size = roundup(uni_path_len, 8);
2324 copy_path = kzalloc(copy_size, GFP_KERNEL);
2325 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002326 rc = -ENOMEM;
2327 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002328 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002329 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002330 uni_path_len);
2331 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002332 /* free before overwriting resource */
2333 kfree(utf16_path);
2334 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002335 }
2336 }
2337
2338 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002339 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002340 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2341
2342 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002343 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002344 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002345 if (rc)
2346 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002347 pc_buf = iov[n_iov-1].iov_base;
2348 }
2349
2350
2351 memset(&rqst, 0, sizeof(struct smb_rqst));
2352 rqst.rq_iov = iov;
2353 rqst.rq_nvec = n_iov;
2354
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002355 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2356 FILE_WRITE_ATTRIBUTES);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002357 /* resource #4: response buffer */
2358 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2359 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002360 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2361 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002362 CREATE_NOT_FILE,
2363 FILE_WRITE_ATTRIBUTES, rc);
2364 goto err_free_rsp_buf;
2365 }
2366
2367 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2368 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2369 ses->Suid, CREATE_NOT_FILE,
2370 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002371
2372 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2373
2374 /* Eventually save off posix specific response info and timestaps */
2375
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002376err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002377 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002378 kfree(pc_buf);
2379err_free_req:
2380 cifs_small_buf_release(req);
2381err_free_path:
2382 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002383 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002384}
Steve Frenchbea851b2018-06-14 21:56:32 -05002385
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002386int
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002387SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2388 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002389{
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002390 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002391 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002392 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002393 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002394 int copy_size;
2395 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002396 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002397 struct kvec *iov = rqst->rq_iov;
2398 __le16 *copy_path;
2399 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002400
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002401 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002402 if (rc)
2403 return rc;
2404
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002405 iov[0].iov_base = (char *)req;
2406 /* -1 since last byte is buf[0] which is sent below (path) */
2407 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002408
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002409 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002410 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002411 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2412 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002413
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002414 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002415 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002416 /* File attributes ignored on open (used in create though) */
2417 req->FileAttributes = cpu_to_le32(file_attributes);
2418 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002419 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2420 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002421 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002422
2423 /* [MS-SMB2] 2.2.13 NameOffset:
2424 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2425 * the SMB2 header, the file name includes a prefix that will
2426 * be processed during DFS name normalization as specified in
2427 * section 3.3.5.9. Otherwise, the file name is relative to
2428 * the share that is identified by the TreeId in the SMB2
2429 * header.
2430 */
2431 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2432 int name_len;
2433
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002434 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002435 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2436 &name_len,
2437 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002438 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002439 return rc;
2440 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002441 uni_path_len = copy_size;
2442 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002443 } else {
2444 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2445 /* MUST set path len (NameLength) to 0 opening root of share */
2446 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002447 copy_size = uni_path_len;
2448 if (copy_size % 8 != 0)
2449 copy_size = roundup(copy_size, 8);
2450 copy_path = kzalloc(copy_size, GFP_KERNEL);
2451 if (!copy_path)
2452 return -ENOMEM;
2453 memcpy((char *)copy_path, (const char *)path,
2454 uni_path_len);
2455 uni_path_len = copy_size;
2456 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002457 }
2458
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002459 iov[1].iov_len = uni_path_len;
2460 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002461
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002462 if (!server->oplocks)
2463 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2464
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002465 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002466 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2467 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002468 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2469 (oparms->create_options & CREATE_NOT_FILE))
2470 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002471 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002472 rc = add_lease_context(server, iov, &n_iov,
2473 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002474 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002475 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002476 }
2477
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002478 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2479 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002480 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002481 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002482 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002483 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002484 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002485 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002486
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002487 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002488 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002489 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002490 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002491 }
2492
Steve Frenchce558b02018-05-31 19:16:54 -05002493 if (tcon->posix_extensions) {
2494 if (n_iov > 2) {
2495 struct create_context *ccontext =
2496 (struct create_context *)iov[n_iov-1].iov_base;
2497 ccontext->Next =
2498 cpu_to_le32(iov[n_iov-1].iov_len);
2499 }
2500
2501 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002502 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002503 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002504 }
Steve Frenchce558b02018-05-31 19:16:54 -05002505
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002506 if (tcon->snapshot_time) {
2507 cifs_dbg(FYI, "adding snapshot context\n");
2508 if (n_iov > 2) {
2509 struct create_context *ccontext =
2510 (struct create_context *)iov[n_iov-1].iov_base;
2511 ccontext->Next =
2512 cpu_to_le32(iov[n_iov-1].iov_len);
2513 }
2514
2515 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2516 if (rc)
2517 return rc;
2518 }
2519
Steve Frenchff2a09e2019-07-06 14:41:38 -05002520 if (n_iov > 2) {
2521 struct create_context *ccontext =
2522 (struct create_context *)iov[n_iov-1].iov_base;
2523 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2524 }
2525 add_query_id_context(iov, &n_iov);
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002526
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002527 rqst->rq_nvec = n_iov;
2528 return 0;
2529}
2530
2531/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2532 * All other vectors are freed by kfree().
2533 */
2534void
2535SMB2_open_free(struct smb_rqst *rqst)
2536{
2537 int i;
2538
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002539 if (rqst && rqst->rq_iov) {
2540 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2541 for (i = 1; i < rqst->rq_nvec; i++)
2542 if (rqst->rq_iov[i].iov_base != smb2_padding)
2543 kfree(rqst->rq_iov[i].iov_base);
2544 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002545}
2546
2547int
2548SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2549 __u8 *oplock, struct smb2_file_all_info *buf,
2550 struct kvec *err_iov, int *buftype)
2551{
2552 struct smb_rqst rqst;
2553 struct smb2_create_rsp *rsp = NULL;
2554 struct TCP_Server_Info *server;
2555 struct cifs_tcon *tcon = oparms->tcon;
2556 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002557 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002558 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002559 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002560 int rc = 0;
2561 int flags = 0;
2562
2563 cifs_dbg(FYI, "create/open\n");
2564 if (ses && (ses->server))
2565 server = ses->server;
2566 else
2567 return -EIO;
2568
2569 if (smb3_encryption_required(tcon))
2570 flags |= CIFS_TRANSFORM_REQ;
2571
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002572 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002573 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002574 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002575 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002576
2577 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2578 if (rc)
2579 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002580
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002581 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2582 oparms->create_options, oparms->desired_access);
2583
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002584 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002585 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002586 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002587
2588 if (rc != 0) {
2589 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002590 if (err_iov && rsp) {
2591 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002592 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002593 resp_buftype = CIFS_NO_BUFFER;
2594 rsp = NULL;
2595 }
Steve French28d59362018-05-30 21:42:34 -05002596 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2597 oparms->create_options, oparms->desired_access, rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002598 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002599 } else
2600 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2601 ses->Suid, oparms->create_options,
2602 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002603
Steve Frenchfae80442018-10-19 17:14:32 -05002604 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002605 oparms->fid->persistent_fid = rsp->PersistentFileId;
2606 oparms->fid->volatile_fid = rsp->VolatileFileId;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002607#ifdef CONFIG_CIFS_DEBUG2
2608 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2609#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002610
2611 if (buf) {
2612 memcpy(buf, &rsp->CreationTime, 32);
2613 buf->AllocationSize = rsp->AllocationSize;
2614 buf->EndOfFile = rsp->EndofFile;
2615 buf->Attributes = rsp->FileAttributes;
2616 buf->NumberOfLinks = cpu_to_le32(1);
2617 buf->DeletePending = 0;
2618 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002619
Steve French89a5bfa2019-07-18 17:22:18 -05002620
2621 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2622 oparms->fid->lease_key, oplock, buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002623creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002624 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002625 free_rsp_buf(resp_buftype, rsp);
2626 return rc;
2627}
2628
Steve French4a72daf2013-06-25 00:20:49 -05002629int
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002630SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2631 u64 persistent_fid, u64 volatile_fid, u32 opcode,
Steve French153322f2019-03-28 22:32:49 -05002632 bool is_fsctl, char *in_data, u32 indatalen,
2633 __u32 max_response_size)
Steve French4a72daf2013-06-25 00:20:49 -05002634{
2635 struct smb2_ioctl_req *req;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002636 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002637 unsigned int total_len;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002638 int rc;
Long Li2c87d6a2019-05-15 14:09:05 -07002639 char *in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002640
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002641 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002642 if (rc)
2643 return rc;
2644
Long Li2c87d6a2019-05-15 14:09:05 -07002645 if (indatalen) {
2646 /*
2647 * indatalen is usually small at a couple of bytes max, so
2648 * just allocate through generic pool
2649 */
YueHaibingd81f0972019-06-01 03:31:10 +00002650 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
Long Li2c87d6a2019-05-15 14:09:05 -07002651 if (!in_data_buf) {
2652 cifs_small_buf_release(req);
2653 return -ENOMEM;
2654 }
Long Li2c87d6a2019-05-15 14:09:05 -07002655 }
2656
Steve French4a72daf2013-06-25 00:20:49 -05002657 req->CtlCode = cpu_to_le32(opcode);
2658 req->PersistentFileId = persistent_fid;
2659 req->VolatileFileId = volatile_fid;
2660
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002661 iov[0].iov_base = (char *)req;
2662 /*
2663 * If no input data, the size of ioctl struct in
2664 * protocol spec still includes a 1 byte data buffer,
2665 * but if input data passed to ioctl, we do not
2666 * want to double count this, so we do not send
2667 * the dummy one byte of data in iovec[0] if sending
2668 * input data (in iovec[1]).
2669 */
Steve French4a72daf2013-06-25 00:20:49 -05002670 if (indatalen) {
2671 req->InputCount = cpu_to_le32(indatalen);
2672 /* do not set InputOffset if no input data */
2673 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002674 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002675 rqst->rq_nvec = 2;
2676 iov[0].iov_len = total_len - 1;
Long Li2c87d6a2019-05-15 14:09:05 -07002677 iov[1].iov_base = in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002678 iov[1].iov_len = indatalen;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002679 } else {
2680 rqst->rq_nvec = 1;
2681 iov[0].iov_len = total_len;
2682 }
Steve French4a72daf2013-06-25 00:20:49 -05002683
2684 req->OutputOffset = 0;
2685 req->OutputCount = 0; /* MBZ */
2686
2687 /*
Steve French153322f2019-03-28 22:32:49 -05002688 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2689 * We Could increase default MaxOutputResponse, but that could require
2690 * more credits. Windows typically sets this smaller, but for some
Steve French4a72daf2013-06-25 00:20:49 -05002691 * ioctls it may be useful to allow server to send more. No point
2692 * limiting what the server can send as long as fits in one credit
Steve French153322f2019-03-28 22:32:49 -05002693 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2694 * to increase this limit up in the future.
2695 * Note that for snapshot queries that servers like Azure expect that
2696 * the first query be minimal size (and just used to get the number/size
2697 * of previous versions) so response size must be specified as EXACTLY
2698 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2699 * of eight bytes. Currently that is the only case where we set max
2700 * response size smaller.
Steve French4a72daf2013-06-25 00:20:49 -05002701 */
Steve French153322f2019-03-28 22:32:49 -05002702 req->MaxOutputResponse = cpu_to_le32(max_response_size);
Steve French4a72daf2013-06-25 00:20:49 -05002703
2704 if (is_fsctl)
2705 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2706 else
2707 req->Flags = 0;
2708
Steve French4587eee2017-10-25 15:58:31 -05002709 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2710 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002711 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002712
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002713 return 0;
2714}
2715
2716void
2717SMB2_ioctl_free(struct smb_rqst *rqst)
2718{
Murphy Zhou6457c202019-05-23 12:12:43 +08002719 int i;
Long Li2c87d6a2019-05-15 14:09:05 -07002720 if (rqst && rqst->rq_iov) {
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002721 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Murphy Zhou6457c202019-05-23 12:12:43 +08002722 for (i = 1; i < rqst->rq_nvec; i++)
2723 if (rqst->rq_iov[i].iov_base != smb2_padding)
2724 kfree(rqst->rq_iov[i].iov_base);
Long Li2c87d6a2019-05-15 14:09:05 -07002725 }
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002726}
2727
Steve French153322f2019-03-28 22:32:49 -05002728
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002729/*
2730 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2731 */
2732int
2733SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2734 u64 volatile_fid, u32 opcode, bool is_fsctl,
Steve French153322f2019-03-28 22:32:49 -05002735 char *in_data, u32 indatalen, u32 max_out_data_len,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002736 char **out_data, u32 *plen /* returned data len */)
2737{
2738 struct smb_rqst rqst;
2739 struct smb2_ioctl_rsp *rsp = NULL;
2740 struct cifs_ses *ses;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002741 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002742 struct kvec rsp_iov = {NULL, 0};
2743 int resp_buftype = CIFS_NO_BUFFER;
2744 int rc = 0;
2745 int flags = 0;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002746 struct TCP_Server_Info *server;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002747
2748 cifs_dbg(FYI, "SMB2 IOCTL\n");
2749
2750 if (out_data != NULL)
2751 *out_data = NULL;
2752
2753 /* zero out returned data len, in case of error */
2754 if (plen)
2755 *plen = 0;
2756
2757 if (tcon)
2758 ses = tcon->ses;
2759 else
2760 return -EIO;
2761
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01002762 if (!ses)
2763 return -EIO;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002764 server = ses->server;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01002765 if (!server)
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002766 return -EIO;
2767
2768 if (smb3_encryption_required(tcon))
2769 flags |= CIFS_TRANSFORM_REQ;
2770
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002771 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002772 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002773 rqst.rq_iov = iov;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002774 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002775
Steve French153322f2019-03-28 22:32:49 -05002776 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2777 is_fsctl, in_data, indatalen, max_out_data_len);
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002778 if (rc)
2779 goto ioctl_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002780
2781 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002782 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002783 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002784
Steve Frencheccb4422018-05-17 21:16:55 -05002785 if (rc != 0)
2786 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2787 ses->Suid, 0, opcode, rc);
2788
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002789 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
Steve French8e353102015-03-26 19:47:02 -05002790 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002791 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002792 } else if (rc == -EINVAL) {
2793 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2794 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002795 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002796 goto ioctl_exit;
2797 }
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002798 } else if (rc == -E2BIG) {
2799 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2800 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2801 goto ioctl_exit;
2802 }
Steve French4a72daf2013-06-25 00:20:49 -05002803 }
2804
2805 /* check if caller wants to look at return data or just return rc */
2806 if ((plen == NULL) || (out_data == NULL))
2807 goto ioctl_exit;
2808
2809 *plen = le32_to_cpu(rsp->OutputCount);
2810
2811 /* We check for obvious errors in the output buffer length and offset */
2812 if (*plen == 0)
2813 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002814 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002815 cifs_server_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002816 *plen = 0;
2817 rc = -EIO;
2818 goto ioctl_exit;
2819 }
2820
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002821 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002822 cifs_server_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
Steve French4a72daf2013-06-25 00:20:49 -05002823 le32_to_cpu(rsp->OutputOffset));
2824 *plen = 0;
2825 rc = -EIO;
2826 goto ioctl_exit;
2827 }
2828
YueHaibingd034fee2018-09-10 01:33:06 +00002829 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2830 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05002831 if (*out_data == NULL) {
2832 rc = -ENOMEM;
2833 goto ioctl_exit;
2834 }
2835
Steve French4a72daf2013-06-25 00:20:49 -05002836ioctl_exit:
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002837 SMB2_ioctl_free(&rqst);
Steve French4a72daf2013-06-25 00:20:49 -05002838 free_rsp_buf(resp_buftype, rsp);
2839 return rc;
2840}
2841
Steve French64a5cfa2013-10-14 15:31:32 -05002842/*
2843 * Individual callers to ioctl worker function follow
2844 */
2845
2846int
2847SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2848 u64 persistent_fid, u64 volatile_fid)
2849{
2850 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002851 struct compress_ioctl fsctl_input;
2852 char *ret_data = NULL;
2853
2854 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002855 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002856
2857 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2858 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2859 (char *)&fsctl_input /* data input */,
Steve French153322f2019-03-28 22:32:49 -05002860 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2861 &ret_data /* out data */, NULL);
Steve French64a5cfa2013-10-14 15:31:32 -05002862
2863 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002864
2865 return rc;
2866}
2867
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002868int
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002869SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2870 u64 persistent_fid, u64 volatile_fid)
2871{
2872 struct smb2_close_req *req;
2873 struct kvec *iov = rqst->rq_iov;
2874 unsigned int total_len;
2875 int rc;
2876
2877 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2878 if (rc)
2879 return rc;
2880
2881 req->PersistentFileId = persistent_fid;
2882 req->VolatileFileId = volatile_fid;
2883 iov[0].iov_base = (char *)req;
2884 iov[0].iov_len = total_len;
2885
2886 return 0;
2887}
2888
2889void
2890SMB2_close_free(struct smb_rqst *rqst)
2891{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002892 if (rqst && rqst->rq_iov)
2893 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002894}
2895
2896int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002897SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2898 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002899{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002900 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002901 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002902 struct cifs_ses *ses = tcon->ses;
2903 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002904 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01002905 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002906 int rc = 0;
2907
Joe Perchesf96637b2013-05-04 22:12:25 -05002908 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002909
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002910 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002911 return -EIO;
2912
Steve French5a77e752018-05-09 17:43:08 -05002913 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002914 flags |= CIFS_TRANSFORM_REQ;
2915
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002916 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002917 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002918 rqst.rq_iov = iov;
2919 rqst.rq_nvec = 1;
2920
Steve Frenchf90f9792019-09-03 18:35:42 -05002921 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002922 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2923 if (rc)
2924 goto close_exit;
2925
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002926 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002927 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002928
2929 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002930 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002931 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2932 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002933 goto close_exit;
Steve Frenchf90f9792019-09-03 18:35:42 -05002934 } else
2935 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
2936 ses->Suid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002937
Steve Frenchfae80442018-10-19 17:14:32 -05002938 atomic_dec(&tcon->num_remote_opens);
2939
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002940 /* BB FIXME - decode close response, update inode for caching */
2941
2942close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002943 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002944 free_rsp_buf(resp_buftype, rsp);
2945 return rc;
2946}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002947
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002948int
2949SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2950 u64 persistent_fid, u64 volatile_fid)
2951{
2952 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2953}
2954
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002955int
2956smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2957 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002958{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002959 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002960 char *end_of_smb = smb_len + (char *)iov->iov_base;
2961 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002962 char *end_of_buf = begin_of_buf + buffer_length;
2963
2964
2965 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002966 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2967 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002968 return -EINVAL;
2969 }
2970
2971 /* check if beyond RFC1001 maximum length */
2972 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002973 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2974 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002975 return -EINVAL;
2976 }
2977
2978 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002979 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002980 return -EINVAL;
2981 }
2982
2983 return 0;
2984}
2985
2986/*
2987 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2988 * Caller must free buffer.
2989 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002990int
2991smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2992 struct kvec *iov, unsigned int minbufsize,
2993 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002994{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002995 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002996 int rc;
2997
2998 if (!data)
2999 return -EINVAL;
3000
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003001 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003002 if (rc)
3003 return rc;
3004
3005 memcpy(data, begin_of_buf, buffer_length);
3006
3007 return 0;
3008}
3009
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003010int
3011SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3012 u64 persistent_fid, u64 volatile_fid,
3013 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003014 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003015{
3016 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003017 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003018 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003019 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003020
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003021 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3022 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003023 if (rc)
3024 return rc;
3025
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003026 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003027 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003028 req->PersistentFileId = persistent_fid;
3029 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003030 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02003031
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003032 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003033 if (input_len) {
3034 req->InputBufferLength = cpu_to_le32(input_len);
3035 /* total_len for smb query request never close to le16 max */
3036 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3037 memcpy(req->Buffer, input, input_len);
3038 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003039
3040 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003041 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003042 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003043 return 0;
3044}
3045
3046void
3047SMB2_query_info_free(struct smb_rqst *rqst)
3048{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003049 if (rqst && rqst->rq_iov)
3050 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003051}
3052
3053static int
3054query_info(const unsigned int xid, struct cifs_tcon *tcon,
3055 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3056 u32 additional_info, size_t output_len, size_t min_len, void **data,
3057 u32 *dlen)
3058{
3059 struct smb_rqst rqst;
3060 struct smb2_query_info_rsp *rsp = NULL;
3061 struct kvec iov[1];
3062 struct kvec rsp_iov;
3063 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01003064 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003065 struct cifs_ses *ses = tcon->ses;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003066 struct TCP_Server_Info *server;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003067 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003068 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003069
3070 cifs_dbg(FYI, "Query Info\n");
3071
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003072 if (!ses)
3073 return -EIO;
3074 server = ses->server;
3075 if (!server)
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003076 return -EIO;
3077
3078 if (smb3_encryption_required(tcon))
3079 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003080
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003081 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003082 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003083 rqst.rq_iov = iov;
3084 rqst.rq_nvec = 1;
3085
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003086 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3087 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003088 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003089 if (rc)
3090 goto qinf_exit;
3091
Steve Frenchd42043a2019-02-26 21:58:30 -06003092 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3093 ses->Suid, info_class, (__u32)info_type);
3094
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003095 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003096 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003097
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003098 if (rc) {
3099 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003100 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3101 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003102 goto qinf_exit;
3103 }
3104
Steve Frenchd42043a2019-02-26 21:58:30 -06003105 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3106 ses->Suid, info_class, (__u32)info_type);
3107
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003108 if (dlen) {
3109 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3110 if (!*data) {
3111 *data = kmalloc(*dlen, GFP_KERNEL);
3112 if (!*data) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003113 cifs_server_dbg(VFS,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003114 "Error %d allocating memory for acl\n",
3115 rc);
3116 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003117 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003118 goto qinf_exit;
3119 }
Colin Ian King73aaf922019-01-16 16:28:59 +00003120 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003121 }
3122 }
3123
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003124 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3125 le32_to_cpu(rsp->OutputBufferLength),
3126 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00003127 if (rc && allocated) {
3128 kfree(*data);
3129 *data = NULL;
3130 *dlen = 0;
3131 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003132
3133qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003134 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003135 free_rsp_buf(resp_buftype, rsp);
3136 return rc;
3137}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003138
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003139int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3140 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003141{
3142 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003143 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04003144 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003145 sizeof(struct smb2_file_all_info), (void **)&data,
3146 NULL);
3147}
3148
3149int
3150SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3151 u64 persistent_fid, u64 volatile_fid,
3152 void **data, u32 *plen)
3153{
3154 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3155 *plen = 0;
3156
3157 return query_info(xid, tcon, persistent_fid, volatile_fid,
3158 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05003159 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003160}
3161
3162int
3163SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3164 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3165{
3166 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003167 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003168 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003169 sizeof(struct smb2_file_internal_info),
3170 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003171}
3172
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003173/*
3174 * This is a no-op for now. We're not really interested in the reply, but
3175 * rather in the fact that the server sent one and that server->lstrp
3176 * gets updated.
3177 *
3178 * FIXME: maybe we should consider checking that the reply matches request?
3179 */
3180static void
3181smb2_echo_callback(struct mid_q_entry *mid)
3182{
3183 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003184 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003185 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003186
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003187 if (mid->mid_state == MID_RESPONSE_RECEIVED
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003188 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3189 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3190 credits.instance = server->reconnect_instance;
3191 }
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003192
3193 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003194 add_credits(server, &credits, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003195}
3196
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003197void smb2_reconnect_server(struct work_struct *work)
3198{
3199 struct TCP_Server_Info *server = container_of(work,
3200 struct TCP_Server_Info, reconnect.work);
3201 struct cifs_ses *ses;
3202 struct cifs_tcon *tcon, *tcon2;
3203 struct list_head tmp_list;
3204 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01003205 int rc;
3206 int resched = false;
3207
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003208
3209 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3210 mutex_lock(&server->reconnect_mutex);
3211
3212 INIT_LIST_HEAD(&tmp_list);
3213 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3214
3215 spin_lock(&cifs_tcp_ses_lock);
3216 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3217 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003218 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003219 tcon->tc_count++;
3220 list_add_tail(&tcon->rlist, &tmp_list);
3221 tcon_exist = true;
3222 }
3223 }
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003224 /*
3225 * IPC has the same lifetime as its session and uses its
3226 * refcount.
3227 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003228 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3229 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3230 tcon_exist = true;
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003231 ses->ses_count++;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003232 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003233 }
3234 /*
3235 * Get the reference to server struct to be sure that the last call of
3236 * cifs_put_tcon() in the loop below won't release the server pointer.
3237 */
3238 if (tcon_exist)
3239 server->srv_count++;
3240
3241 spin_unlock(&cifs_tcp_ses_lock);
3242
3243 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01003244 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3245 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003246 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01003247 else
3248 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003249 list_del_init(&tcon->rlist);
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003250 if (tcon->ipc)
3251 cifs_put_smb_ses(tcon->ses);
3252 else
3253 cifs_put_tcon(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003254 }
3255
3256 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01003257 if (resched)
3258 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003259 mutex_unlock(&server->reconnect_mutex);
3260
3261 /* now we can safely release srv struct */
3262 if (tcon_exist)
3263 cifs_put_tcp_session(server, 1);
3264}
3265
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003266int
3267SMB2_echo(struct TCP_Server_Info *server)
3268{
3269 struct smb2_echo_req *req;
3270 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003271 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003272 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003273 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003274 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003275
Joe Perchesf96637b2013-05-04 22:12:25 -05003276 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003277
Steve French4fcd1812016-06-22 20:12:05 -05003278 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003279 /* No need to send echo on newly established connections */
3280 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3281 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003282 }
3283
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003284 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003285 if (rc)
3286 return rc;
3287
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003288 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003289
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003290 iov[0].iov_len = total_len;
3291 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003292
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003293 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003294 server, CIFS_ECHO_OP, NULL);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003295 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003296 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003297
3298 cifs_small_buf_release(req);
3299 return rc;
3300}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003301
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003302void
3303SMB2_flush_free(struct smb_rqst *rqst)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003304{
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003305 if (rqst && rqst->rq_iov)
3306 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3307}
3308
3309int
3310SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3311 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3312{
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003313 struct smb2_flush_req *req;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003314 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003315 unsigned int total_len;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003316 int rc;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003317
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003318 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003319 if (rc)
3320 return rc;
3321
3322 req->PersistentFileId = persistent_fid;
3323 req->VolatileFileId = volatile_fid;
3324
3325 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003326 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003327
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003328 return 0;
3329}
3330
3331int
3332SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3333 u64 volatile_fid)
3334{
3335 struct cifs_ses *ses = tcon->ses;
3336 struct smb_rqst rqst;
3337 struct kvec iov[1];
3338 struct kvec rsp_iov = {NULL, 0};
3339 int resp_buftype = CIFS_NO_BUFFER;
3340 int flags = 0;
3341 int rc = 0;
3342
3343 cifs_dbg(FYI, "flush\n");
3344 if (!ses || !(ses->server))
3345 return -EIO;
3346
3347 if (smb3_encryption_required(tcon))
3348 flags |= CIFS_TRANSFORM_REQ;
3349
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003350 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003351 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003352 rqst.rq_iov = iov;
3353 rqst.rq_nvec = 1;
3354
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003355 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3356 if (rc)
3357 goto flush_exit;
3358
Steve Frenchf90f9792019-09-03 18:35:42 -05003359 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003360 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003361
Steve Frencheccb4422018-05-17 21:16:55 -05003362 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003363 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003364 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3365 rc);
Steve Frenchf90f9792019-09-03 18:35:42 -05003366 } else
3367 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3368 ses->Suid);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003369
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003370 flush_exit:
3371 SMB2_flush_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003372 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003373 return rc;
3374}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003375
3376/*
3377 * To form a chain of read requests, any read requests after the first should
3378 * have the end_of_chain boolean set to true.
3379 */
3380static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003381smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003382 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3383 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003384{
3385 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003386 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003387 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07003388 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003389
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003390 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3391 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003392 if (rc)
3393 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003394
3395 server = io_parms->tcon->ses->server;
3396 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003397 return -ECONNABORTED;
3398
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003399 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003400 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003401
3402 req->PersistentFileId = io_parms->persistent_fid;
3403 req->VolatileFileId = io_parms->volatile_fid;
3404 req->ReadChannelInfoOffset = 0; /* reserved */
3405 req->ReadChannelInfoLength = 0; /* reserved */
3406 req->Channel = 0; /* reserved */
3407 req->MinimumCount = 0;
3408 req->Length = cpu_to_le32(io_parms->length);
3409 req->Offset = cpu_to_le64(io_parms->offset);
Steve Frenchd323c2462019-02-25 00:52:43 -06003410
3411 trace_smb3_read_enter(0 /* xid */,
3412 io_parms->persistent_fid,
3413 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3414 io_parms->offset, io_parms->length);
Long Libd3dcc62017-11-22 17:38:47 -07003415#ifdef CONFIG_CIFS_SMB_DIRECT
3416 /*
3417 * If we want to do a RDMA write, fill in and append
3418 * smbd_buffer_descriptor_v1 to the end of read request
3419 */
Long Libb4c0412018-04-17 12:17:08 -07003420 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003421 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003422
Long Libd3dcc62017-11-22 17:38:47 -07003423 struct smbd_buffer_descriptor_v1 *v1;
3424 bool need_invalidate =
3425 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3426
3427 rdata->mr = smbd_register_mr(
3428 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003429 rdata->nr_pages, rdata->page_offset,
3430 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003431 if (!rdata->mr)
Long Lib7972092019-04-05 21:36:34 +00003432 return -EAGAIN;
Long Libd3dcc62017-11-22 17:38:47 -07003433
3434 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3435 if (need_invalidate)
3436 req->Channel = SMB2_CHANNEL_RDMA_V1;
3437 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003438 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003439 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003440 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003441 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003442 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3443 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3444 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003445
3446 *total_len += sizeof(*v1) - 1;
3447 }
3448#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003449 if (request_type & CHAINED_REQUEST) {
3450 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003451 /* next 8-byte aligned request */
3452 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3453 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003454 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003455 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003456 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003457 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003458 /*
3459 * Related requests use info from previous read request
3460 * in chain.
3461 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003462 shdr->SessionId = 0xFFFFFFFF;
3463 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003464 req->PersistentFileId = 0xFFFFFFFF;
3465 req->VolatileFileId = 0xFFFFFFFF;
3466 }
3467 }
3468 if (remaining_bytes > io_parms->length)
3469 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3470 else
3471 req->RemainingBytes = 0;
3472
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003473 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003474 return rc;
3475}
3476
3477static void
3478smb2_readv_callback(struct mid_q_entry *mid)
3479{
3480 struct cifs_readdata *rdata = mid->callback_data;
3481 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3482 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003483 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003484 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003485 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003486 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3487 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07003488 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003489 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003490 .rq_npages = rdata->nr_pages,
3491 .rq_pagesz = rdata->pagesz,
3492 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003493
Joe Perchesf96637b2013-05-04 22:12:25 -05003494 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3495 __func__, mid->mid, mid->mid_state, rdata->result,
3496 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003497
3498 switch (mid->mid_state) {
3499 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003500 credits.value = le16_to_cpu(shdr->CreditRequest);
3501 credits.instance = server->reconnect_instance;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003502 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003503 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003504 int rc;
3505
Jeff Layton0b688cf2012-09-18 16:20:34 -07003506 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003507 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003508 cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003509 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003510 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003511 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003512 task_io_account_read(rdata->got_bytes);
3513 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003514 break;
3515 case MID_REQUEST_SUBMITTED:
3516 case MID_RETRY_NEEDED:
3517 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003518 if (server->sign && rdata->got_bytes)
3519 /* reset bytes number since we can not check a sign */
3520 rdata->got_bytes = 0;
3521 /* FIXME: should this be counted toward the initiating task? */
3522 task_io_account_read(rdata->got_bytes);
3523 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003524 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003525 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003526 credits.value = le16_to_cpu(shdr->CreditRequest);
3527 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003528 /* fall through */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003529 default:
Pavel Shilovsky6b15eb12019-01-18 15:46:14 -08003530 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003531 }
Long Libd3dcc62017-11-22 17:38:47 -07003532#ifdef CONFIG_CIFS_SMB_DIRECT
3533 /*
3534 * If this rdata has a memmory registered, the MR can be freed
3535 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3536 * because they have limited number and are used for future I/Os
3537 */
3538 if (rdata->mr) {
3539 smbd_deregister_mr(rdata->mr);
3540 rdata->mr = NULL;
3541 }
3542#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08003543 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003544 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003545 trace_smb3_read_err(0 /* xid */,
3546 rdata->cfile->fid.persistent_fid,
3547 tcon->tid, tcon->ses->Suid, rdata->offset,
3548 rdata->bytes, rdata->result);
3549 } else
3550 trace_smb3_read_done(0 /* xid */,
3551 rdata->cfile->fid.persistent_fid,
3552 tcon->tid, tcon->ses->Suid,
3553 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003554
3555 queue_work(cifsiod_wq, &rdata->work);
3556 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003557 add_credits(server, &credits, 0);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003558}
3559
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003560/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003561int
3562smb2_async_readv(struct cifs_readdata *rdata)
3563{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003564 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003565 char *buf;
3566 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003567 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003568 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003569 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003570 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003571 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003572
Joe Perchesf96637b2013-05-04 22:12:25 -05003573 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3574 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003575
3576 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3577 io_parms.offset = rdata->offset;
3578 io_parms.length = rdata->bytes;
3579 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3580 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3581 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003582
3583 server = io_parms.tcon->ses->server;
3584
Long Li2dabfd52017-11-07 01:54:53 -07003585 rc = smb2_new_read_req(
3586 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003587 if (rc)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003588 return rc;
3589
Steve French5a77e752018-05-09 17:43:08 -05003590 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003591 flags |= CIFS_TRANSFORM_REQ;
3592
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003593 rdata->iov[0].iov_base = buf;
3594 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003595
3596 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003597
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003598 if (rdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003599 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003600 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003601 shdr->CreditRequest =
3602 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003603
3604 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3605 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003606 goto async_readv_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003607
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003608 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003609 }
3610
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003611 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07003612 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003613 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003614 smb3_handle_read_data, rdata, flags,
3615 &rdata->credits);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003616 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003617 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003618 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003619 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3620 io_parms.tcon->tid,
3621 io_parms.tcon->ses->Suid,
3622 io_parms.offset, io_parms.length, rc);
3623 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003624
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003625async_readv_out:
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003626 cifs_small_buf_release(buf);
3627 return rc;
3628}
Pavel Shilovsky33319142012-09-18 16:20:29 -07003629
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003630int
3631SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3632 unsigned int *nbytes, char **buf, int *buf_type)
3633{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003634 struct smb_rqst rqst;
Colin Ian King1efd4fc2019-07-31 10:05:26 +01003635 int resp_buftype, rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003636 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003637 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003638 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003639 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003640 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003641 int flags = CIFS_LOG_ERROR;
3642 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003643
3644 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07003645 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003646 if (rc)
3647 return rc;
3648
Steve French5a77e752018-05-09 17:43:08 -05003649 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003650 flags |= CIFS_TRANSFORM_REQ;
3651
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003652 iov[0].iov_base = (char *)req;
3653 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003654
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003655 memset(&rqst, 0, sizeof(struct smb_rqst));
3656 rqst.rq_iov = iov;
3657 rqst.rq_nvec = 1;
3658
3659 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003660 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003661
3662 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003663 if (rc != -ENODATA) {
3664 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3665 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003666 trace_smb3_read_err(xid, req->PersistentFileId,
3667 io_parms->tcon->tid, ses->Suid,
3668 io_parms->offset, io_parms->length,
3669 rc);
Steve Frenchb0a42f22019-02-25 15:02:58 -06003670 } else
3671 trace_smb3_read_done(xid, req->PersistentFileId,
3672 io_parms->tcon->tid, ses->Suid,
3673 io_parms->offset, 0);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003674 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Ronnie Sahlberg05fd5c22019-04-23 16:39:45 +10003675 cifs_small_buf_release(req);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003676 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05003677 } else
3678 trace_smb3_read_done(xid, req->PersistentFileId,
3679 io_parms->tcon->tid, ses->Suid,
3680 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003681
ZhangXiaoxu088aaf12019-04-06 15:47:39 +08003682 cifs_small_buf_release(req);
3683
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003684 *nbytes = le32_to_cpu(rsp->DataLength);
3685 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3686 (*nbytes > io_parms->length)) {
3687 cifs_dbg(FYI, "bad length %d for count %d\n",
3688 *nbytes, io_parms->length);
3689 rc = -EIO;
3690 *nbytes = 0;
3691 }
3692
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003693 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003694 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003695 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003696 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003697 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003698 if (resp_buftype == CIFS_SMALL_BUFFER)
3699 *buf_type = CIFS_SMALL_BUFFER;
3700 else if (resp_buftype == CIFS_LARGE_BUFFER)
3701 *buf_type = CIFS_LARGE_BUFFER;
3702 }
3703 return rc;
3704}
3705
Pavel Shilovsky33319142012-09-18 16:20:29 -07003706/*
3707 * Check the mid_state and signature on received buffer (if any), and queue the
3708 * workqueue completion task.
3709 */
3710static void
3711smb2_writev_callback(struct mid_q_entry *mid)
3712{
3713 struct cifs_writedata *wdata = mid->callback_data;
3714 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003715 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003716 unsigned int written;
3717 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003718 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky33319142012-09-18 16:20:29 -07003719
3720 switch (mid->mid_state) {
3721 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003722 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3723 credits.instance = server->reconnect_instance;
3724 wdata->result = smb2_check_receive(mid, server, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003725 if (wdata->result != 0)
3726 break;
3727
3728 written = le32_to_cpu(rsp->DataLength);
3729 /*
3730 * Mask off high 16 bits when bytes written as returned
3731 * by the server is greater than bytes requested by the
3732 * client. OS/2 servers are known to set incorrect
3733 * CountHigh values.
3734 */
3735 if (written > wdata->bytes)
3736 written &= 0xFFFF;
3737
3738 if (written < wdata->bytes)
3739 wdata->result = -ENOSPC;
3740 else
3741 wdata->bytes = written;
3742 break;
3743 case MID_REQUEST_SUBMITTED:
3744 case MID_RETRY_NEEDED:
3745 wdata->result = -EAGAIN;
3746 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003747 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003748 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3749 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003750 /* fall through */
Pavel Shilovsky33319142012-09-18 16:20:29 -07003751 default:
3752 wdata->result = -EIO;
3753 break;
3754 }
Long Lidb223a52017-11-22 17:38:45 -07003755#ifdef CONFIG_CIFS_SMB_DIRECT
3756 /*
3757 * If this wdata has a memory registered, the MR can be freed
3758 * The number of MRs available is limited, it's important to recover
3759 * used MR as soon as I/O is finished. Hold MR longer in the later
3760 * I/O process can possibly result in I/O deadlock due to lack of MR
3761 * to send request on I/O retry
3762 */
3763 if (wdata->mr) {
3764 smbd_deregister_mr(wdata->mr);
3765 wdata->mr = NULL;
3766 }
3767#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003768 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003769 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003770 trace_smb3_write_err(0 /* no xid */,
3771 wdata->cfile->fid.persistent_fid,
3772 tcon->tid, tcon->ses->Suid, wdata->offset,
3773 wdata->bytes, wdata->result);
3774 } else
3775 trace_smb3_write_done(0 /* no xid */,
3776 wdata->cfile->fid.persistent_fid,
3777 tcon->tid, tcon->ses->Suid,
3778 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003779
3780 queue_work(cifsiod_wq, &wdata->work);
3781 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003782 add_credits(server, &credits, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003783}
3784
3785/* smb2_async_writev - send an async write, and set up mid to handle result */
3786int
Steve French4a5c80d2014-02-07 20:45:12 -06003787smb2_async_writev(struct cifs_writedata *wdata,
3788 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07003789{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003790 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003791 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003792 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003793 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003794 struct TCP_Server_Info *server = tcon->ses->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003795 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003796 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003797 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003798
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003799 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003800 if (rc)
3801 return rc;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003802
Steve French5a77e752018-05-09 17:43:08 -05003803 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003804 flags |= CIFS_TRANSFORM_REQ;
3805
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003806 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003807 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003808
3809 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3810 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3811 req->WriteChannelInfoOffset = 0;
3812 req->WriteChannelInfoLength = 0;
3813 req->Channel = 0;
3814 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003815 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003816 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003817 req->RemainingBytes = 0;
Steve Frenchd323c2462019-02-25 00:52:43 -06003818
3819 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3820 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003821#ifdef CONFIG_CIFS_SMB_DIRECT
3822 /*
3823 * If we want to do a server RDMA read, fill in and append
3824 * smbd_buffer_descriptor_v1 to the end of write request
3825 */
Long Libb4c0412018-04-17 12:17:08 -07003826 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003827 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003828
Long Lidb223a52017-11-22 17:38:45 -07003829 struct smbd_buffer_descriptor_v1 *v1;
3830 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3831
3832 wdata->mr = smbd_register_mr(
3833 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003834 wdata->nr_pages, wdata->page_offset,
3835 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07003836 if (!wdata->mr) {
Long Lib7972092019-04-05 21:36:34 +00003837 rc = -EAGAIN;
Long Lidb223a52017-11-22 17:38:45 -07003838 goto async_writev_out;
3839 }
3840 req->Length = 0;
3841 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07003842 if (wdata->nr_pages > 1)
3843 req->RemainingBytes =
3844 cpu_to_le32(
3845 (wdata->nr_pages - 1) * wdata->pagesz -
3846 wdata->page_offset + wdata->tailsz
3847 );
3848 else
3849 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003850 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3851 if (need_invalidate)
3852 req->Channel = SMB2_CHANNEL_RDMA_V1;
3853 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003854 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003855 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003856 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003857 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003858 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3859 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3860 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003861 }
3862#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003863 iov[0].iov_len = total_len - 1;
3864 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003865
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003866 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003867 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003868 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07003869 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003870 rqst.rq_npages = wdata->nr_pages;
3871 rqst.rq_pagesz = wdata->pagesz;
3872 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003873#ifdef CONFIG_CIFS_SMB_DIRECT
3874 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003875 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07003876 rqst.rq_npages = 0;
3877 }
3878#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003879 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3880 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003881
Long Lidb223a52017-11-22 17:38:45 -07003882#ifdef CONFIG_CIFS_SMB_DIRECT
3883 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3884 if (!wdata->mr)
3885 req->Length = cpu_to_le32(wdata->bytes);
3886#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003887 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003888#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003889
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003890 if (wdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003891 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003892 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003893 shdr->CreditRequest =
3894 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003895
3896 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3897 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003898 goto async_writev_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003899
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003900 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003901 }
3902
Pavel Shilovsky33319142012-09-18 16:20:29 -07003903 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003904 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003905 wdata, flags, &wdata->credits);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003906
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003907 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003908 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3909 tcon->tid, tcon->ses->Suid, wdata->offset,
3910 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06003911 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003912 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003913 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003914
Pavel Shilovsky33319142012-09-18 16:20:29 -07003915async_writev_out:
3916 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003917 return rc;
3918}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003919
3920/*
3921 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3922 * The length field from io_parms must be at least 1 and indicates a number of
3923 * elements with data to write that begins with position 1 in iov array. All
3924 * data length is specified by count.
3925 */
3926int
3927SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3928 unsigned int *nbytes, struct kvec *iov, int n_vec)
3929{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003930 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003931 int rc = 0;
3932 struct smb2_write_req *req = NULL;
3933 struct smb2_write_rsp *rsp = NULL;
3934 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003935 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003936 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003937 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003938
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003939 *nbytes = 0;
3940
3941 if (n_vec < 1)
3942 return rc;
3943
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003944 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3945 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003946 if (rc)
3947 return rc;
3948
3949 if (io_parms->tcon->ses->server == NULL)
3950 return -ECONNABORTED;
3951
Steve French5a77e752018-05-09 17:43:08 -05003952 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003953 flags |= CIFS_TRANSFORM_REQ;
3954
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003955 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003956
3957 req->PersistentFileId = io_parms->persistent_fid;
3958 req->VolatileFileId = io_parms->volatile_fid;
3959 req->WriteChannelInfoOffset = 0;
3960 req->WriteChannelInfoLength = 0;
3961 req->Channel = 0;
3962 req->Length = cpu_to_le32(io_parms->length);
3963 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003964 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003965 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003966 req->RemainingBytes = 0;
3967
Steve Frenchd323c2462019-02-25 00:52:43 -06003968 trace_smb3_write_enter(xid, io_parms->persistent_fid,
3969 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3970 io_parms->offset, io_parms->length);
3971
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003972 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003973 /* 1 for Buffer */
3974 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003975
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003976 memset(&rqst, 0, sizeof(struct smb_rqst));
3977 rqst.rq_iov = iov;
3978 rqst.rq_nvec = n_vec + 1;
3979
3980 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003981 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003982 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003983
3984 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003985 trace_smb3_write_err(xid, req->PersistentFileId,
3986 io_parms->tcon->tid,
3987 io_parms->tcon->ses->Suid,
3988 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003989 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003990 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05003991 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003992 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05003993 trace_smb3_write_done(xid, req->PersistentFileId,
3994 io_parms->tcon->tid,
3995 io_parms->tcon->ses->Suid,
3996 io_parms->offset, *nbytes);
3997 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003998
ZhangXiaoxu6a3eb332019-04-06 15:47:38 +08003999 cifs_small_buf_release(req);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004000 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004001 return rc;
4002}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004003
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004004static unsigned int
4005num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
4006{
4007 int len;
4008 unsigned int entrycount = 0;
4009 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03004010 char *entryptr;
4011 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004012
4013 if (bufstart == NULL)
4014 return 0;
4015
Dan Carpenter56446f22018-09-06 12:48:22 +03004016 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004017
4018 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03004019 if (entryptr + next_offset < entryptr ||
4020 entryptr + next_offset > end_of_buf ||
4021 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004022 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004023 break;
4024 }
4025
Dan Carpenter56446f22018-09-06 12:48:22 +03004026 entryptr = entryptr + next_offset;
4027 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4028
4029 len = le32_to_cpu(dir_info->FileNameLength);
4030 if (entryptr + len < entryptr ||
4031 entryptr + len > end_of_buf ||
4032 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004033 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4034 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004035 break;
4036 }
4037
Dan Carpenter56446f22018-09-06 12:48:22 +03004038 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004039 entrycount++;
4040
Dan Carpenter56446f22018-09-06 12:48:22 +03004041 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004042 if (!next_offset)
4043 break;
4044 }
4045
4046 return entrycount;
4047}
4048
4049/*
4050 * Readdir/FindFirst
4051 */
4052int
4053SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4054 u64 persistent_fid, u64 volatile_fid, int index,
4055 struct cifs_search_info *srch_inf)
4056{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004057 struct smb_rqst rqst;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004058 struct smb2_query_directory_req *req;
4059 struct smb2_query_directory_rsp *rsp = NULL;
4060 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004061 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004062 int rc = 0;
4063 int len;
Steve French75fdfc82015-03-25 18:51:57 -05004064 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004065 unsigned char *bufptr;
4066 struct TCP_Server_Info *server;
4067 struct cifs_ses *ses = tcon->ses;
4068 __le16 asteriks = cpu_to_le16('*');
4069 char *end_of_smb;
4070 unsigned int output_size = CIFSMaxBufSize;
4071 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004072 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004073 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004074
4075 if (ses && (ses->server))
4076 server = ses->server;
4077 else
4078 return -EIO;
4079
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004080 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4081 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004082 if (rc)
4083 return rc;
4084
Steve French5a77e752018-05-09 17:43:08 -05004085 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004086 flags |= CIFS_TRANSFORM_REQ;
4087
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004088 switch (srch_inf->info_level) {
4089 case SMB_FIND_FILE_DIRECTORY_INFO:
4090 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4091 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4092 break;
4093 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4094 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4095 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4096 break;
4097 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10004098 cifs_server_dbg(VFS, "info level %u isn't supported\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05004099 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004100 rc = -EINVAL;
4101 goto qdir_exit;
4102 }
4103
4104 req->FileIndex = cpu_to_le32(index);
4105 req->PersistentFileId = persistent_fid;
4106 req->VolatileFileId = volatile_fid;
4107
4108 len = 0x2;
4109 bufptr = req->Buffer;
4110 memcpy(bufptr, &asteriks, len);
4111
4112 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004113 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004114 req->FileNameLength = cpu_to_le16(len);
4115 /*
4116 * BB could be 30 bytes or so longer if we used SMB2 specific
4117 * buffer lengths, but this is safe and close enough.
4118 */
4119 output_size = min_t(unsigned int, output_size, server->maxBuf);
4120 output_size = min_t(unsigned int, output_size, 2 << 15);
4121 req->OutputBufferLength = cpu_to_le32(output_size);
4122
4123 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004124 /* 1 for Buffer */
4125 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004126
4127 iov[1].iov_base = (char *)(req->Buffer);
4128 iov[1].iov_len = len;
4129
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004130 memset(&rqst, 0, sizeof(struct smb_rqst));
4131 rqst.rq_iov = iov;
4132 rqst.rq_nvec = 2;
4133
Steve Frenchd323c2462019-02-25 00:52:43 -06004134 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4135 tcon->ses->Suid, index, output_size);
4136
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004137 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004138 cifs_small_buf_release(req);
4139 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004140
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004141 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004142 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004143 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004144 trace_smb3_query_dir_done(xid, persistent_fid,
4145 tcon->tid, tcon->ses->Suid, index, 0);
Pavel Shilovsky52755802014-08-18 20:49:57 +04004146 srch_inf->endOfSearch = true;
4147 rc = 0;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004148 } else {
4149 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4150 tcon->ses->Suid, index, 0, rc);
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08004151 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004152 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004153 goto qdir_exit;
4154 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004155
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004156 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4157 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4158 info_buf_size);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004159 if (rc) {
4160 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4161 tcon->ses->Suid, index, 0, rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004162 goto qdir_exit;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004163 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004164
4165 srch_inf->unicode = true;
4166
4167 if (srch_inf->ntwrk_buf_start) {
4168 if (srch_inf->smallBuf)
4169 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4170 else
4171 cifs_buf_release(srch_inf->ntwrk_buf_start);
4172 }
4173 srch_inf->ntwrk_buf_start = (char *)rsp;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004174 srch_inf->srch_entries_start = srch_inf->last_entry =
4175 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4176 end_of_smb = rsp_iov.iov_len + (char *)rsp;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004177 srch_inf->entries_in_buffer =
4178 num_entries(srch_inf->srch_entries_start, end_of_smb,
4179 &srch_inf->last_entry, info_buf_size);
4180 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05004181 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4182 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4183 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004184 if (resp_buftype == CIFS_LARGE_BUFFER)
4185 srch_inf->smallBuf = false;
4186 else if (resp_buftype == CIFS_SMALL_BUFFER)
4187 srch_inf->smallBuf = true;
4188 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10004189 cifs_server_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004190
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004191 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4192 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004193 return rc;
4194
4195qdir_exit:
4196 free_rsp_buf(resp_buftype, rsp);
4197 return rc;
4198}
4199
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004200int
4201SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004202 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004203 u8 info_type, u32 additional_info,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004204 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004205{
4206 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004207 struct kvec *iov = rqst->rq_iov;
4208 unsigned int i, total_len;
4209 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004210
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004211 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004212 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004213 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004214
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004215 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004216 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004217 req->FileInfoClass = info_class;
4218 req->PersistentFileId = persistent_fid;
4219 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004220 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004221
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004222 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004223 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004224 req->BufferLength = cpu_to_le32(*size);
4225
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004226 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004227 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004228
4229 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004230 /* 1 for Buffer */
4231 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004232
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004233 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004234 le32_add_cpu(&req->BufferLength, size[i]);
4235 iov[i].iov_base = (char *)data[i];
4236 iov[i].iov_len = size[i];
4237 }
4238
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004239 return 0;
4240}
4241
4242void
4243SMB2_set_info_free(struct smb_rqst *rqst)
4244{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10004245 if (rqst && rqst->rq_iov)
4246 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004247}
4248
4249static int
4250send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4251 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4252 u8 info_type, u32 additional_info, unsigned int num,
4253 void **data, unsigned int *size)
4254{
4255 struct smb_rqst rqst;
4256 struct smb2_set_info_rsp *rsp = NULL;
4257 struct kvec *iov;
4258 struct kvec rsp_iov;
4259 int rc = 0;
4260 int resp_buftype;
4261 struct cifs_ses *ses = tcon->ses;
4262 int flags = 0;
4263
4264 if (!ses || !(ses->server))
4265 return -EIO;
4266
4267 if (!num)
4268 return -EINVAL;
4269
4270 if (smb3_encryption_required(tcon))
4271 flags |= CIFS_TRANSFORM_REQ;
4272
4273 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4274 if (!iov)
4275 return -ENOMEM;
4276
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004277 memset(&rqst, 0, sizeof(struct smb_rqst));
4278 rqst.rq_iov = iov;
4279 rqst.rq_nvec = num;
4280
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004281 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4282 info_class, info_type, additional_info,
4283 data, size);
4284 if (rc) {
4285 kfree(iov);
4286 return rc;
4287 }
4288
4289
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004290 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004291 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004292 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004293 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004294
Steve Frencheccb4422018-05-17 21:16:55 -05004295 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004296 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004297 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4298 ses->Suid, info_class, (__u32)info_type, rc);
4299 }
Steve French7d3fb242013-11-18 09:56:28 -06004300
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004301 free_rsp_buf(resp_buftype, rsp);
4302 kfree(iov);
4303 return rc;
4304}
4305
4306int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004307SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004308 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004309{
4310 struct smb2_file_eof_info info;
4311 void *data;
4312 unsigned int size;
4313
4314 info.EndOfFile = *eof;
4315
4316 data = &info;
4317 size = sizeof(struct smb2_file_eof_info);
4318
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004319 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004320 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4321 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004322}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004323
4324int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004325SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4326 u64 persistent_fid, u64 volatile_fid,
4327 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4328{
4329 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4330 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4331 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004332}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004333
4334int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004335SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4336 u64 persistent_fid, u64 volatile_fid,
4337 struct smb2_file_full_ea_info *buf, int len)
4338{
4339 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4340 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4341 0, 1, (void **)&buf, &len);
4342}
4343
4344int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004345SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4346 const u64 persistent_fid, const u64 volatile_fid,
4347 __u8 oplock_level)
4348{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004349 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004350 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10004351 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004352 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004353 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004354 unsigned int total_len;
4355 struct kvec iov[1];
4356 struct kvec rsp_iov;
4357 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004358
Joe Perchesf96637b2013-05-04 22:12:25 -05004359 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004360 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4361 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004362 if (rc)
4363 return rc;
4364
Steve French5a77e752018-05-09 17:43:08 -05004365 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004366 flags |= CIFS_TRANSFORM_REQ;
4367
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004368 req->VolatileFid = volatile_fid;
4369 req->PersistentFid = persistent_fid;
4370 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004371 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004372
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004373 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004374
4375 iov[0].iov_base = (char *)req;
4376 iov[0].iov_len = total_len;
4377
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004378 memset(&rqst, 0, sizeof(struct smb_rqst));
4379 rqst.rq_iov = iov;
4380 rqst.rq_nvec = 1;
4381
4382 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004383 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004384
4385 if (rc) {
4386 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004387 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004388 }
4389
4390 return rc;
4391}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004392
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004393void
4394smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4395 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004396{
4397 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4398 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4399 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05304400 kst->f_bfree = kst->f_bavail =
4401 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004402 return;
4403}
4404
Steve French2d304212018-06-24 23:28:12 -05004405static void
4406copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4407 struct kstatfs *kst)
4408{
4409 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4410 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4411 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4412 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4413 kst->f_bavail = kst->f_bfree;
4414 else
4415 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4416 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4417 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4418 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4419 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4420
4421 return;
4422}
Steve French2d304212018-06-24 23:28:12 -05004423
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004424static int
4425build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4426 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4427{
4428 int rc;
4429 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004430 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004431
Joe Perchesf96637b2013-05-04 22:12:25 -05004432 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004433
4434 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4435 return -EIO;
4436
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004437 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4438 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004439 if (rc)
4440 return rc;
4441
4442 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4443 req->FileInfoClass = level;
4444 req->PersistentFileId = persistent_fid;
4445 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004446 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004447 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004448 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004449 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004450 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004451
4452 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004453 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004454 return 0;
4455}
4456
Steve French2d304212018-06-24 23:28:12 -05004457int
4458SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4459 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4460{
4461 struct smb_rqst rqst;
4462 struct smb2_query_info_rsp *rsp = NULL;
4463 struct kvec iov;
4464 struct kvec rsp_iov;
4465 int rc = 0;
4466 int resp_buftype;
4467 struct cifs_ses *ses = tcon->ses;
4468 FILE_SYSTEM_POSIX_INFO *info = NULL;
4469 int flags = 0;
4470
4471 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4472 sizeof(FILE_SYSTEM_POSIX_INFO),
4473 persistent_fid, volatile_fid);
4474 if (rc)
4475 return rc;
4476
4477 if (smb3_encryption_required(tcon))
4478 flags |= CIFS_TRANSFORM_REQ;
4479
4480 memset(&rqst, 0, sizeof(struct smb_rqst));
4481 rqst.rq_iov = &iov;
4482 rqst.rq_nvec = 1;
4483
4484 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4485 cifs_small_buf_release(iov.iov_base);
4486 if (rc) {
4487 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4488 goto posix_qfsinf_exit;
4489 }
4490 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4491
4492 info = (FILE_SYSTEM_POSIX_INFO *)(
4493 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004494 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4495 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4496 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05004497 if (!rc)
4498 copy_posix_fs_info_to_kstatfs(info, fsdata);
4499
4500posix_qfsinf_exit:
4501 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4502 return rc;
4503}
Steve French2d304212018-06-24 23:28:12 -05004504
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004505int
4506SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4507 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4508{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004509 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004510 struct smb2_query_info_rsp *rsp = NULL;
4511 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004512 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004513 int rc = 0;
4514 int resp_buftype;
4515 struct cifs_ses *ses = tcon->ses;
4516 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004517 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004518
4519 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4520 sizeof(struct smb2_fs_full_size_info),
4521 persistent_fid, volatile_fid);
4522 if (rc)
4523 return rc;
4524
Steve French5a77e752018-05-09 17:43:08 -05004525 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004526 flags |= CIFS_TRANSFORM_REQ;
4527
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004528 memset(&rqst, 0, sizeof(struct smb_rqst));
4529 rqst.rq_iov = &iov;
4530 rqst.rq_nvec = 1;
4531
4532 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004533 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004534 if (rc) {
4535 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05004536 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004537 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004538 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004539
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004540 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004541 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004542 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4543 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4544 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004545 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004546 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004547
Steve French34f62642013-10-09 02:07:00 -05004548qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004549 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004550 return rc;
4551}
4552
4553int
4554SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05004555 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05004556{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004557 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05004558 struct smb2_query_info_rsp *rsp = NULL;
4559 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004560 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05004561 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05004562 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05004563 struct cifs_ses *ses = tcon->ses;
4564 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004565 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05004566
Steven French21671142013-10-09 13:36:35 -05004567 if (level == FS_DEVICE_INFORMATION) {
4568 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4569 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4570 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4571 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4572 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004573 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4574 max_len = sizeof(struct smb3_fs_ss_info);
4575 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05004576 } else if (level == FS_VOLUME_INFORMATION) {
4577 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4578 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05004579 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004580 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05004581 return -EINVAL;
4582 }
4583
4584 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05004585 persistent_fid, volatile_fid);
4586 if (rc)
4587 return rc;
4588
Steve French5a77e752018-05-09 17:43:08 -05004589 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004590 flags |= CIFS_TRANSFORM_REQ;
4591
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004592 memset(&rqst, 0, sizeof(struct smb_rqst));
4593 rqst.rq_iov = &iov;
4594 rqst.rq_nvec = 1;
4595
4596 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004597 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004598 if (rc) {
4599 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4600 goto qfsattr_exit;
4601 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004602 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05004603
4604 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4605 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004606 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05004607 if (rc)
4608 goto qfsattr_exit;
4609
4610 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004611 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004612 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05004613 rsp_len, max_len));
4614 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004615 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004616 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004617 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4618 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004619 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004620 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4621 tcon->perf_sector_size =
4622 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05004623 } else if (level == FS_VOLUME_INFORMATION) {
4624 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4625 (offset + (char *)rsp);
4626 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4627 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004628 }
Steve French34f62642013-10-09 02:07:00 -05004629
4630qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004631 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004632 return rc;
4633}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004634
4635int
4636smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4637 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4638 const __u32 num_lock, struct smb2_lock_element *buf)
4639{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004640 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004641 int rc = 0;
4642 struct smb2_lock_req *req = NULL;
4643 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004644 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004645 int resp_buf_type;
4646 unsigned int count;
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004647 int flags = CIFS_NO_RSP_BUF;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004648 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004649
Joe Perchesf96637b2013-05-04 22:12:25 -05004650 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004651
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004652 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004653 if (rc)
4654 return rc;
4655
Steve French5a77e752018-05-09 17:43:08 -05004656 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004657 flags |= CIFS_TRANSFORM_REQ;
4658
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004659 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004660 req->LockCount = cpu_to_le16(num_lock);
4661
4662 req->PersistentFileId = persist_fid;
4663 req->VolatileFileId = volatile_fid;
4664
4665 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004666
4667 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004668 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004669 iov[1].iov_base = (char *)buf;
4670 iov[1].iov_len = count;
4671
4672 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004673
4674 memset(&rqst, 0, sizeof(struct smb_rqst));
4675 rqst.rq_iov = iov;
4676 rqst.rq_nvec = 2;
4677
4678 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004679 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004680 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004681 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004682 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004683 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004684 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4685 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004686 }
4687
4688 return rc;
4689}
4690
4691int
4692SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4693 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4694 const __u64 length, const __u64 offset, const __u32 lock_flags,
4695 const bool wait)
4696{
4697 struct smb2_lock_element lock;
4698
4699 lock.Offset = cpu_to_le64(offset);
4700 lock.Length = cpu_to_le64(length);
4701 lock.Flags = cpu_to_le32(lock_flags);
4702 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4703 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4704
4705 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4706}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004707
4708int
4709SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4710 __u8 *lease_key, const __le32 lease_state)
4711{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004712 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004713 int rc;
4714 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004715 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004716 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004717 unsigned int total_len;
4718 struct kvec iov[1];
4719 struct kvec rsp_iov;
4720 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05004721 __u64 *please_key_high;
4722 __u64 *please_key_low;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004723
Joe Perchesf96637b2013-05-04 22:12:25 -05004724 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004725 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4726 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004727 if (rc)
4728 return rc;
4729
Steve French5a77e752018-05-09 17:43:08 -05004730 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004731 flags |= CIFS_TRANSFORM_REQ;
4732
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004733 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004734 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004735 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004736
4737 memcpy(req->LeaseKey, lease_key, 16);
4738 req->LeaseState = lease_state;
4739
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004740 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004741
4742 iov[0].iov_base = (char *)req;
4743 iov[0].iov_len = total_len;
4744
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004745 memset(&rqst, 0, sizeof(struct smb_rqst));
4746 rqst.rq_iov = iov;
4747 rqst.rq_nvec = 1;
4748
4749 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004750 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004751
Aurelien Apteld339adc2019-01-31 13:46:07 +01004752 please_key_low = (__u64 *)lease_key;
4753 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004754 if (rc) {
4755 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05004756 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4757 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05004758 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05004759 } else
4760 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4761 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004762
4763 return rc;
4764}