blob: 77f2c723befa6f005b7338fba74cb7759bd38493 [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);
110 /* Request up to 2 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 -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100116 server->credits, 2));
117 spin_unlock(&server->req_lock);
118 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700119 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100120 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400122
123 if (!tcon)
124 goto out;
125
Steve French2b80d042013-06-23 18:43:37 -0500126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500128 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700130 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500131 /* else CreditCharge MBZ */
132
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700133 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400134 /* Uid is not converted */
135 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700136 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500137
138 /*
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
147 */
148/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500150
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
Steve French5a77e752018-05-09 17:43:08 -0500152 !smb3_encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700153 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400154out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400155 return;
156}
157
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200158#ifdef CONFIG_CIFS_DFS_UPCALL
159static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
161{
162 int rc;
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100165 char *tree;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200166 const char *tcp_host;
167 size_t tcp_host_len;
168 const char *dfs_host;
169 size_t dfs_host_len;
170
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
172 if (!tree)
173 return -ENOMEM;
174
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200175 if (tcon->ipc) {
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000176 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
179 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200180 }
181
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
184 goto out;
185 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200186
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
188 if (rc)
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100189 goto out;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200190
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
192 &tcp_host_len);
193
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
196 const char *tgt = dfs_cache_get_tgt_name(it);
197
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
199
200 if (dfs_host_len != tcp_host_len
201 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
202 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
203 __func__,
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
206 continue;
207 }
208
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000209 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200210
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
212 if (!rc)
213 break;
214 if (rc == -EREMOTE)
215 break;
216 }
217
218 if (!rc) {
219 if (it)
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
221 it);
222 else
223 rc = -ENOENT;
224 }
225 dfs_cache_free_tgts(&tl);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100226out:
227 kfree(tree);
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200228 return rc;
229}
230#else
231static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
233{
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
235}
236#endif
237
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400238static int
239smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
240{
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300241 int rc;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200245 int retries;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400246
247 /*
248 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
249 * check for tcp and smb session status done differently
250 * for those three - in the calling routine.
251 */
252 if (tcon == NULL)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300253 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400254
255 if (smb2_command == SMB2_TREE_CONNECT)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300256 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400257
258 if (tcon->tidStatus == CifsExiting) {
259 /*
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
263 */
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
268 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400269 return -ENODEV;
270 }
271 }
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
274 return -EIO;
275
276 ses = tcon->ses;
277 server = ses->server;
278
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200279 retries = server->nr_targets;
280
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400281 /*
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200282 * Give demultiplex thread up to 10 seconds to each target available for
283 * reconnect -- should be greater than cifs socket timeout which is 7
284 * seconds.
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400285 */
286 while (server->tcpStatus == CifsNeedReconnect) {
287 /*
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
290 */
291 switch (smb2_command) {
292 /*
293 * BB Should we keep oplock break and add flush to exceptions?
294 */
295 case SMB2_TREE_DISCONNECT:
296 case SMB2_CANCEL:
297 case SMB2_CLOSE:
298 case SMB2_OPLOCK_BREAK:
299 return -EAGAIN;
300 }
301
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
304 10 * HZ);
305 if (rc < 0) {
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
308 return -ERESTARTSYS;
309 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400310
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
313 break;
314
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200315 if (--retries)
316 continue;
317
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400318 /*
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
321 * back on-line
322 */
323 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400325 return -EHOSTDOWN;
326 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200327 retries = server->nr_targets;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400328 }
329
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300331 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400332
333 nls_codepage = load_nls_default();
334
335 /*
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
338 */
339 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200340
341 /*
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
345 */
346 if (server->tcpStatus == CifsNeedReconnect) {
347 rc = -EHOSTDOWN;
348 mutex_unlock(&tcon->ses->session_mutex);
349 goto out;
350 }
351
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400352 rc = cifs_negotiate_protocol(0, tcon->ses);
353 if (!rc && tcon->ses->need_reconnect)
354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
355
356 if (rc || !tcon->need_reconnect) {
357 mutex_unlock(&tcon->ses->session_mutex);
358 goto out;
359 }
360
361 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800362 if (tcon->use_persistent)
363 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500364
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200365 rc = __smb2_reconnect(nls_codepage, tcon);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400366 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500367
Joe Perchesf96637b2013-05-04 22:12:25 -0500368 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500369 if (rc) {
370 /* If sess reconnected but tcon didn't, something strange ... */
371 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400372 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500373 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800374
375 if (smb2_command != SMB2_INTERNAL_CMD)
376 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
377
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400378 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400379out:
380 /*
381 * Check if handle based operation so we know whether we can continue
382 * or not without returning to caller to reset file handle.
383 */
384 /*
385 * BB Is flush done by server on drop of tcp session? Should we special
386 * case it and skip above?
387 */
388 switch (smb2_command) {
389 case SMB2_FLUSH:
390 case SMB2_READ:
391 case SMB2_WRITE:
392 case SMB2_LOCK:
393 case SMB2_IOCTL:
394 case SMB2_QUERY_DIRECTORY:
395 case SMB2_CHANGE_NOTIFY:
396 case SMB2_QUERY_INFO:
397 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800398 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400399 }
400 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400401 return rc;
402}
403
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700404static void
405fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
406 unsigned int *total_len)
407{
408 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
409 /* lookup word count ie StructureSize from table */
410 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
411
412 /*
413 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
414 * largest operations (Create)
415 */
416 memset(buf, 0, 256);
417
418 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
419 spdu->StructureSize2 = cpu_to_le16(parmsize);
420
421 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
422}
423
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100424/*
425 * Allocate and return pointer to an SMB request hdr, and set basic
426 * SMB information in the SMB header. If the return code is zero, this
427 * function must have filled in request_buf pointer.
428 */
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800429static int
430smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
431 void **request_buf, unsigned int *total_len)
432{
433 int rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800434
435 rc = smb2_reconnect(smb2_command, tcon);
436 if (rc)
437 return rc;
438
439 /* BB eventually switch this to SMB2 specific small buf size */
Stefano Briviof46ecbd2018-07-05 11:46:42 +0200440 if (smb2_command == SMB2_SET_INFO)
441 *request_buf = cifs_buf_get();
442 else
443 *request_buf = cifs_small_buf_get();
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800444 if (*request_buf == NULL) {
445 /* BB should we add a retry in here if not a writepage? */
446 return -ENOMEM;
447 }
448
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100449 fill_small_buf(smb2_command, tcon,
450 (struct smb2_sync_hdr *)(*request_buf),
451 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400452
453 if (tcon != NULL) {
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400454 uint16_t com_code = le16_to_cpu(smb2_command);
455 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400456 cifs_stats_inc(&tcon->num_smbs_sent);
457 }
458
459 return rc;
460}
461
Steve French0fdfef92018-06-28 19:30:23 -0500462
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500463#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
464#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
Steve Frenchfcef0db2018-05-19 20:45:27 -0500465#define SMB2_POSIX_EXTENSIONS_AVAILABLE cpu_to_le16(0x100)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500466
467static void
468build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
469{
470 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
471 pneg_ctxt->DataLength = cpu_to_le16(38);
472 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
473 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
474 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
475 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
476}
477
478static void
479build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
480{
481 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve French23657ad2018-04-22 15:14:58 -0500482 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
483 pneg_ctxt->CipherCount = cpu_to_le16(1);
484/* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
485 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500486}
487
488static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500489build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
490{
491 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
492 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
493}
494
495static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100496assemble_neg_contexts(struct smb2_negotiate_req *req,
497 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500498{
Steve Frenchd5c70762019-01-03 02:37:21 -0600499 char *pneg_ctxt = (char *)req;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500500 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500501
Steve Frenchd5c70762019-01-03 02:37:21 -0600502 if (*total_len > 200) {
503 /* In case length corrupted don't want to overrun smb buffer */
504 cifs_dbg(VFS, "Bad frame length assembling neg contexts\n");
505 return;
506 }
507
508 /*
509 * round up total_len of fixed part of SMB3 negotiate request to 8
510 * byte boundary before adding negotiate contexts
511 */
512 *total_len = roundup(*total_len, 8);
513
514 pneg_ctxt = (*total_len) + (char *)req;
515 req->NegotiateContextOffset = cpu_to_le32(*total_len);
516
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500517 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500518 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
519 *total_len += ctxt_len;
520 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100521
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500522 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500523 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
524 *total_len += ctxt_len;
525 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100526
Steve Frenchfcef0db2018-05-19 20:45:27 -0500527 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
528 *total_len += sizeof(struct smb2_posix_neg_context);
529
Steve Frenchfcef0db2018-05-19 20:45:27 -0500530 req->NegotiateContextCount = cpu_to_le16(3);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500531}
Steve French5100d8a2018-04-09 10:47:14 -0500532
533static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
534{
535 unsigned int len = le16_to_cpu(ctxt->DataLength);
536
537 /* If invalid preauth context warn but use what we requested, SHA-512 */
538 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
539 printk_once(KERN_WARNING "server sent bad preauth context\n");
540 return;
541 }
542 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
543 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
544 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
545 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
546}
547
548static int decode_encrypt_ctx(struct TCP_Server_Info *server,
549 struct smb2_encryption_neg_context *ctxt)
550{
551 unsigned int len = le16_to_cpu(ctxt->DataLength);
552
553 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
554 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
555 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
556 return -EINVAL;
557 }
558
559 if (le16_to_cpu(ctxt->CipherCount) != 1) {
560 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
561 return -EINVAL;
562 }
563 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
564 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
565 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
566 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
567 return -EINVAL;
568 }
569 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500570 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500571 return 0;
572}
573
574static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000575 struct TCP_Server_Info *server,
576 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500577{
578 struct smb2_neg_context *pctx;
579 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
580 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500581 unsigned int len_of_ctxts, i;
582 int rc = 0;
583
584 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
585 if (len_of_smb <= offset) {
586 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
587 return -EINVAL;
588 }
589
590 len_of_ctxts = len_of_smb - offset;
591
592 for (i = 0; i < ctxt_cnt; i++) {
593 int clen;
594 /* check that offset is not beyond end of SMB */
595 if (len_of_ctxts == 0)
596 break;
597
598 if (len_of_ctxts < sizeof(struct smb2_neg_context))
599 break;
600
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000601 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500602 clen = le16_to_cpu(pctx->DataLength);
603 if (clen > len_of_ctxts)
604 break;
605
606 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
607 decode_preauth_context(
608 (struct smb2_preauth_neg_context *)pctx);
609 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
610 rc = decode_encrypt_ctx(server,
611 (struct smb2_encryption_neg_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500612 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
613 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500614 else
615 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
616 le16_to_cpu(pctx->ContextType));
617
618 if (rc)
619 break;
620 /* offsets must be 8 byte aligned */
621 clen = (clen + 7) & ~0x7;
622 offset += clen + sizeof(struct smb2_neg_context);
623 len_of_ctxts -= clen;
624 }
625 return rc;
626}
627
Steve Frenchce558b02018-05-31 19:16:54 -0500628static struct create_posix *
629create_posix_buf(umode_t mode)
630{
631 struct create_posix *buf;
632
633 buf = kzalloc(sizeof(struct create_posix),
634 GFP_KERNEL);
635 if (!buf)
636 return NULL;
637
638 buf->ccontext.DataOffset =
639 cpu_to_le16(offsetof(struct create_posix, Mode));
640 buf->ccontext.DataLength = cpu_to_le32(4);
641 buf->ccontext.NameOffset =
642 cpu_to_le16(offsetof(struct create_posix, Name));
643 buf->ccontext.NameLength = cpu_to_le16(16);
644
645 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
646 buf->Name[0] = 0x93;
647 buf->Name[1] = 0xAD;
648 buf->Name[2] = 0x25;
649 buf->Name[3] = 0x50;
650 buf->Name[4] = 0x9C;
651 buf->Name[5] = 0xB4;
652 buf->Name[6] = 0x11;
653 buf->Name[7] = 0xE7;
654 buf->Name[8] = 0xB4;
655 buf->Name[9] = 0x23;
656 buf->Name[10] = 0x83;
657 buf->Name[11] = 0xDE;
658 buf->Name[12] = 0x96;
659 buf->Name[13] = 0x8B;
660 buf->Name[14] = 0xCD;
661 buf->Name[15] = 0x7C;
662 buf->Mode = cpu_to_le32(mode);
663 cifs_dbg(FYI, "mode on posix create 0%o", mode);
664 return buf;
665}
666
667static int
668add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
669{
670 struct smb2_create_req *req = iov[0].iov_base;
671 unsigned int num = *num_iovec;
672
673 iov[num].iov_base = create_posix_buf(mode);
674 if (iov[num].iov_base == NULL)
675 return -ENOMEM;
676 iov[num].iov_len = sizeof(struct create_posix);
677 if (!req->CreateContextsOffset)
678 req->CreateContextsOffset = cpu_to_le32(
679 sizeof(struct smb2_create_req) +
680 iov[num - 1].iov_len);
681 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
682 *num_iovec = num + 1;
683 return 0;
684}
685
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500686
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400687/*
688 *
689 * SMB2 Worker functions follow:
690 *
691 * The general structure of the worker functions is:
692 * 1) Call smb2_init (assembles SMB2 header)
693 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
694 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
695 * 4) Decode SMB2 command specific fields in the fixed length area
696 * 5) Decode variable length data area (if any for this SMB2 command type)
697 * 6) Call free smb buffer
698 * 7) return
699 *
700 */
701
702int
703SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
704{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000705 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400706 struct smb2_negotiate_req *req;
707 struct smb2_negotiate_rsp *rsp;
708 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700709 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400710 int rc = 0;
711 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400712 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400713 int blob_offset, blob_length;
714 char *security_blob;
715 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100716 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400717
Joe Perchesf96637b2013-05-04 22:12:25 -0500718 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400719
Jeff Layton3534b852013-05-24 07:41:01 -0400720 if (!server) {
721 WARN(1, "%s: server is NULL!\n", __func__);
722 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400723 }
724
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100725 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400726 if (rc)
727 return rc;
728
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100729 req->sync_hdr.SessionId = 0;
Steve French0fdfef92018-06-28 19:30:23 -0500730
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100731 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
732 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400733
Steve French9764c022017-09-17 10:41:35 -0500734 if (strcmp(ses->server->vals->version_string,
735 SMB3ANY_VERSION_STRING) == 0) {
736 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
737 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
738 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100739 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500740 } else if (strcmp(ses->server->vals->version_string,
741 SMBDEFAULT_VERSION_STRING) == 0) {
742 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
743 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
744 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600745 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
746 req->DialectCount = cpu_to_le16(4);
747 total_len += 8;
Steve French9764c022017-09-17 10:41:35 -0500748 } else {
749 /* otherwise send specific dialect */
750 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
751 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100752 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500753 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400754
755 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400756 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500757 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400758 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500759 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400760 else
761 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400762
Steve Frenche4aa25e2012-10-01 12:26:22 -0500763 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400764
Steve French3c5f9be12014-05-13 13:37:45 -0700765 /* ClientGUID must be zero for SMB2.02 dialect */
766 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
767 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500768 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700769 memcpy(req->ClientGUID, server->client_guid,
770 SMB2_CLIENT_GUID_SIZE);
Steve Frenchd5c70762019-01-03 02:37:21 -0600771 if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
772 (strcmp(ses->server->vals->version_string,
773 SMBDEFAULT_VERSION_STRING) == 0))
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100774 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500775 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400776 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100777 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400778
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000779 memset(&rqst, 0, sizeof(struct smb_rqst));
780 rqst.rq_iov = iov;
781 rqst.rq_nvec = 1;
782
783 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700784 cifs_small_buf_release(req);
785 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400786 /*
787 * No tcon so can't do
788 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
789 */
Steve French7e682f72017-08-31 21:34:24 -0500790 if (rc == -EOPNOTSUPP) {
791 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500792 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500793 " older servers\n");
794 goto neg_exit;
795 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400796 goto neg_exit;
797
Steve French9764c022017-09-17 10:41:35 -0500798 if (strcmp(ses->server->vals->version_string,
799 SMB3ANY_VERSION_STRING) == 0) {
800 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
801 cifs_dbg(VFS,
802 "SMB2 dialect returned but not requested\n");
803 return -EIO;
804 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
805 cifs_dbg(VFS,
806 "SMB2.1 dialect returned but not requested\n");
807 return -EIO;
808 }
809 } else if (strcmp(ses->server->vals->version_string,
810 SMBDEFAULT_VERSION_STRING) == 0) {
811 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
812 cifs_dbg(VFS,
813 "SMB2 dialect returned but not requested\n");
814 return -EIO;
815 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
816 /* ops set to 3.0 by default for default so update */
817 ses->server->ops = &smb21_operations;
Steve Frenchd5c70762019-01-03 02:37:21 -0600818 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
819 ses->server->ops = &smb311_operations;
Steve French590d08d2017-09-19 11:43:47 -0500820 } else if (le16_to_cpu(rsp->DialectRevision) !=
821 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500822 /* if requested single dialect ensure returned dialect matched */
823 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500824 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500825 return -EIO;
826 }
827
Joe Perchesf96637b2013-05-04 22:12:25 -0500828 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400829
Steve Frenche4aa25e2012-10-01 12:26:22 -0500830 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500831 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500832 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500833 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500834 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500835 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500836 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
837 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600838 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
839 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400840 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500841 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500842 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400843 rc = -EIO;
844 goto neg_exit;
845 }
846 server->dialect = le16_to_cpu(rsp->DialectRevision);
847
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100848 /*
849 * Keep a copy of the hash after negprot. This hash will be
850 * the starting hash value for all sessions made from this
851 * server.
852 */
853 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
854 SMB2_PREAUTH_HASH_SIZE);
Steve French0fdfef92018-06-28 19:30:23 -0500855
Jeff Laytone598d1d82013-05-26 07:00:59 -0400856 /* SMB2 only has an extended negflavor */
857 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400858 /* set it to the maximum buffer size value we can send with 1 credit */
859 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
860 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400861 server->max_read = le32_to_cpu(rsp->MaxReadSize);
862 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400863 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500864 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
865 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
866 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400867 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400868 /* Internal types */
869 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400870
871 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000872 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500873 /*
874 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
875 * for us will be
876 * ses->sectype = RawNTLMSSP;
877 * but for time being this is our only auth choice so doesn't matter.
878 * We just found a server which sets blob length to zero expecting raw.
879 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700880 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500881 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700882 server->sec_ntlmssp = true;
883 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700884
Jeff Layton38d77c52013-05-26 07:01:00 -0400885 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400886 if (rc)
887 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500888 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500889 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500890 if (rc == 1)
891 rc = 0;
892 else if (rc == 0)
893 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400894 }
Steve French5100d8a2018-04-09 10:47:14 -0500895
Steve French5100d8a2018-04-09 10:47:14 -0500896 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
897 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000898 rc = smb311_decode_neg_context(rsp, server,
899 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -0500900 else
901 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
902 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400903neg_exit:
904 free_rsp_buf(resp_buftype, rsp);
905 return rc;
906}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400907
Steve Frenchff1c0382013-11-19 23:44:46 -0600908int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
909{
Long Li2796d302018-04-25 11:30:04 -0700910 int rc;
911 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200912 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600913 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500914 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600915
916 cifs_dbg(FYI, "validate negotiate\n");
917
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100918 /* In SMB3.11 preauth integrity supersedes validate negotiate */
919 if (tcon->ses->server->dialect == SMB311_PROT_ID)
920 return 0;
921
Steve Frenchff1c0382013-11-19 23:44:46 -0600922 /*
923 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500924 * can not sign it (ie are not known user). Even if signing is not
925 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600926 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500927 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600928 */
Steve French0603c962017-09-20 19:57:18 -0500929 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600930 return 0; /* validation requires signing */
931
Steve French0603c962017-09-20 19:57:18 -0500932 if (tcon->ses->user_name == NULL) {
933 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
934 return 0; /* validation requires signing */
935 }
936
937 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
938 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
939
Long Li2796d302018-04-25 11:30:04 -0700940 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
941 if (!pneg_inbuf)
942 return -ENOMEM;
943
944 pneg_inbuf->Capabilities =
Steve Frenchff1c0382013-11-19 23:44:46 -0600945 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Long Li2796d302018-04-25 11:30:04 -0700946 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100947 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600948
949 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -0700950 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600951 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
952 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -0700953 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600954 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
955 else
Long Li2796d302018-04-25 11:30:04 -0700956 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -0600957
Steve French9764c022017-09-17 10:41:35 -0500958
959 if (strcmp(tcon->ses->server->vals->version_string,
960 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700961 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
962 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
963 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -0500964 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -0700965 inbuflen = sizeof(*pneg_inbuf) -
Steve Frenchd5c70762019-01-03 02:37:21 -0600966 (2 * sizeof(pneg_inbuf->Dialects[0]));
Steve French9764c022017-09-17 10:41:35 -0500967 } else if (strcmp(tcon->ses->server->vals->version_string,
968 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -0700969 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
970 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
971 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600972 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
973 pneg_inbuf->DialectCount = cpu_to_le16(4);
Steve French9764c022017-09-17 10:41:35 -0500974 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -0700975 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -0500976 } else {
977 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -0700978 pneg_inbuf->Dialects[0] =
Steve French9764c022017-09-17 10:41:35 -0500979 cpu_to_le16(tcon->ses->server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -0700980 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -0500981 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -0700982 inbuflen = sizeof(*pneg_inbuf) -
983 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -0500984 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600985
986 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
987 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Long Li2796d302018-04-25 11:30:04 -0700988 (char *)pneg_inbuf, inbuflen, (char **)&pneg_rsp, &rsplen);
Namjae Jeon969ae8e2019-01-22 09:46:45 +0900989 if (rc == -EOPNOTSUPP) {
990 /*
991 * Old Windows versions or Netapp SMB server can return
992 * not supported error. Client should accept it.
993 */
994 cifs_dbg(VFS, "Server does not support validate negotiate\n");
995 return 0;
996 } else if (rc != 0) {
Steve Frenchff1c0382013-11-19 23:44:46 -0600997 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
Long Li2796d302018-04-25 11:30:04 -0700998 rc = -EIO;
999 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -06001000 }
1001
Long Li2796d302018-04-25 11:30:04 -07001002 rc = -EIO;
1003 if (rsplen != sizeof(*pneg_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -05001004 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1005 rsplen);
1006
1007 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -07001008 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1009 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001010 }
1011
1012 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +01001013 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -06001014 goto vneg_out;
1015
1016 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
1017 goto vneg_out;
1018
1019 /* do not validate server guid because not saved at negprot time yet */
1020
1021 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1022 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
1023 goto vneg_out;
1024
1025 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -07001026 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001027 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -07001028 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001029
1030vneg_out:
1031 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -07001032out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001033 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -07001034out_free_inbuf:
1035 kfree(pneg_inbuf);
1036 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -06001037}
1038
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301039enum securityEnum
1040smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1041{
1042 switch (requested) {
1043 case Kerberos:
1044 case RawNTLMSSP:
1045 return requested;
1046 case NTLMv2:
1047 return RawNTLMSSP;
1048 case Unspecified:
1049 if (server->sec_ntlmssp &&
1050 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1051 return RawNTLMSSP;
1052 if ((server->sec_kerberos || server->sec_mskerberos) &&
1053 (global_secflags & CIFSSEC_MAY_KRB5))
1054 return Kerberos;
1055 /* Fallthrough */
1056 default:
1057 return Unspecified;
1058 }
1059}
1060
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001061struct SMB2_sess_data {
1062 unsigned int xid;
1063 struct cifs_ses *ses;
1064 struct nls_table *nls_cp;
1065 void (*func)(struct SMB2_sess_data *);
1066 int result;
1067 u64 previous_session;
1068
1069 /* we will send the SMB in three pieces:
1070 * a fixed length beginning part, an optional
1071 * SPNEGO blob (which can be zero length), and a
1072 * last part which will include the strings
1073 * and rest of bcc area. This allows us to avoid
1074 * a large buffer 17K allocation
1075 */
1076 int buf0_type;
1077 struct kvec iov[2];
1078};
1079
1080static int
1081SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1082{
1083 int rc;
1084 struct cifs_ses *ses = sess_data->ses;
1085 struct smb2_sess_setup_req *req;
1086 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001087 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001088
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001089 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1090 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001091 if (rc)
1092 return rc;
1093
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001094 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001095 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001096
1097 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1098 req->PreviousSessionId = sess_data->previous_session;
1099
1100 req->Flags = 0; /* MBZ */
Steve Frenchd4090142018-06-13 17:05:58 -05001101
1102 /* enough to enable echos and oplocks and one max size write */
1103 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001104
1105 /* only one of SMB2 signing flags may be set in SMB2 request */
1106 if (server->sign)
1107 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1108 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1109 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1110 else
1111 req->SecurityMode = 0;
1112
1113 req->Capabilities = 0;
1114 req->Channel = 0; /* MBZ */
1115
1116 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001117 /* 1 for pad */
1118 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001119 /*
1120 * This variable will be used to clear the buffer
1121 * allocated above in case of any error in the calling function.
1122 */
1123 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1124
1125 return 0;
1126}
1127
1128static void
1129SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1130{
1131 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1132 sess_data->buf0_type = CIFS_NO_BUFFER;
1133}
1134
1135static int
1136SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1137{
1138 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001139 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001140 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001141 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001142
1143 /* Testing shows that buffer offset must be at location of Buffer[0] */
1144 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001145 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001146 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1147
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001148 memset(&rqst, 0, sizeof(struct smb_rqst));
1149 rqst.rq_iov = sess_data->iov;
1150 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001151
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001152 /* BB add code to build os and lm fields */
1153 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1154 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001155 &sess_data->buf0_type,
1156 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001157 cifs_small_buf_release(sess_data->iov[0].iov_base);
1158 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001159
1160 return rc;
1161}
1162
1163static int
1164SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1165{
1166 int rc = 0;
1167 struct cifs_ses *ses = sess_data->ses;
1168
1169 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001170 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001171 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001172 if (rc) {
1173 cifs_dbg(FYI,
1174 "SMB3 session key generation failed\n");
1175 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001176 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001177 }
1178 }
1179 if (!ses->server->session_estab) {
1180 ses->server->sequence_number = 0x2;
1181 ses->server->session_estab = true;
1182 }
1183 mutex_unlock(&ses->server->srv_mutex);
1184
1185 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1186 spin_lock(&GlobalMid_Lock);
1187 ses->status = CifsGood;
1188 ses->need_reconnect = false;
1189 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001190 return rc;
1191}
1192
1193#ifdef CONFIG_CIFS_UPCALL
1194static void
1195SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1196{
1197 int rc;
1198 struct cifs_ses *ses = sess_data->ses;
1199 struct cifs_spnego_msg *msg;
1200 struct key *spnego_key = NULL;
1201 struct smb2_sess_setup_rsp *rsp = NULL;
1202
1203 rc = SMB2_sess_alloc_buffer(sess_data);
1204 if (rc)
1205 goto out;
1206
1207 spnego_key = cifs_get_spnego_key(ses);
1208 if (IS_ERR(spnego_key)) {
1209 rc = PTR_ERR(spnego_key);
1210 spnego_key = NULL;
1211 goto out;
1212 }
1213
1214 msg = spnego_key->payload.data[0];
1215 /*
1216 * check version field to make sure that cifs.upcall is
1217 * sending us a response in an expected form
1218 */
1219 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1220 cifs_dbg(VFS,
1221 "bad cifs.upcall version. Expected %d got %d",
1222 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1223 rc = -EKEYREJECTED;
1224 goto out_put_spnego_key;
1225 }
1226
1227 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1228 GFP_KERNEL);
1229 if (!ses->auth_key.response) {
1230 cifs_dbg(VFS,
1231 "Kerberos can't allocate (%u bytes) memory",
1232 msg->sesskey_len);
1233 rc = -ENOMEM;
1234 goto out_put_spnego_key;
1235 }
1236 ses->auth_key.len = msg->sesskey_len;
1237
1238 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1239 sess_data->iov[1].iov_len = msg->secblob_len;
1240
1241 rc = SMB2_sess_sendreceive(sess_data);
1242 if (rc)
1243 goto out_put_spnego_key;
1244
1245 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001246 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001247
1248 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001249
1250 rc = SMB2_sess_establish_session(sess_data);
1251out_put_spnego_key:
1252 key_invalidate(spnego_key);
1253 key_put(spnego_key);
1254out:
1255 sess_data->result = rc;
1256 sess_data->func = NULL;
1257 SMB2_sess_free_buffer(sess_data);
1258}
1259#else
1260static void
1261SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1262{
1263 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1264 sess_data->result = -EOPNOTSUPP;
1265 sess_data->func = NULL;
1266}
1267#endif
1268
Sachin Prabhu166cea42016-10-07 19:11:22 +01001269static void
1270SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1271
1272static void
1273SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1274{
1275 int rc;
1276 struct cifs_ses *ses = sess_data->ses;
1277 struct smb2_sess_setup_rsp *rsp = NULL;
1278 char *ntlmssp_blob = NULL;
1279 bool use_spnego = false; /* else use raw ntlmssp */
1280 u16 blob_length = 0;
1281
1282 /*
1283 * If memory allocation is successful, caller of this function
1284 * frees it.
1285 */
1286 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1287 if (!ses->ntlmssp) {
1288 rc = -ENOMEM;
1289 goto out_err;
1290 }
1291 ses->ntlmssp->sesskey_per_smbsess = true;
1292
1293 rc = SMB2_sess_alloc_buffer(sess_data);
1294 if (rc)
1295 goto out_err;
1296
1297 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1298 GFP_KERNEL);
1299 if (ntlmssp_blob == NULL) {
1300 rc = -ENOMEM;
1301 goto out;
1302 }
1303
1304 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1305 if (use_spnego) {
1306 /* BB eventually need to add this */
1307 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1308 rc = -EOPNOTSUPP;
1309 goto out;
1310 } else {
1311 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1312 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1313 }
1314 sess_data->iov[1].iov_base = ntlmssp_blob;
1315 sess_data->iov[1].iov_len = blob_length;
1316
1317 rc = SMB2_sess_sendreceive(sess_data);
1318 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1319
1320 /* If true, rc here is expected and not an error */
1321 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001322 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001323 rc = 0;
1324
1325 if (rc)
1326 goto out;
1327
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001328 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001329 le16_to_cpu(rsp->SecurityBufferOffset)) {
1330 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1331 le16_to_cpu(rsp->SecurityBufferOffset));
1332 rc = -EIO;
1333 goto out;
1334 }
1335 rc = decode_ntlmssp_challenge(rsp->Buffer,
1336 le16_to_cpu(rsp->SecurityBufferLength), ses);
1337 if (rc)
1338 goto out;
1339
1340 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1341
1342
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001343 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001344 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001345
1346out:
1347 kfree(ntlmssp_blob);
1348 SMB2_sess_free_buffer(sess_data);
1349 if (!rc) {
1350 sess_data->result = 0;
1351 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1352 return;
1353 }
1354out_err:
1355 kfree(ses->ntlmssp);
1356 ses->ntlmssp = NULL;
1357 sess_data->result = rc;
1358 sess_data->func = NULL;
1359}
1360
1361static void
1362SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1363{
1364 int rc;
1365 struct cifs_ses *ses = sess_data->ses;
1366 struct smb2_sess_setup_req *req;
1367 struct smb2_sess_setup_rsp *rsp = NULL;
1368 unsigned char *ntlmssp_blob = NULL;
1369 bool use_spnego = false; /* else use raw ntlmssp */
1370 u16 blob_length = 0;
1371
1372 rc = SMB2_sess_alloc_buffer(sess_data);
1373 if (rc)
1374 goto out;
1375
1376 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001377 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001378
1379 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1380 sess_data->nls_cp);
1381 if (rc) {
1382 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1383 goto out;
1384 }
1385
1386 if (use_spnego) {
1387 /* BB eventually need to add this */
1388 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1389 rc = -EOPNOTSUPP;
1390 goto out;
1391 }
1392 sess_data->iov[1].iov_base = ntlmssp_blob;
1393 sess_data->iov[1].iov_len = blob_length;
1394
1395 rc = SMB2_sess_sendreceive(sess_data);
1396 if (rc)
1397 goto out;
1398
1399 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1400
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001401 ses->Suid = rsp->sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001402 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001403
1404 rc = SMB2_sess_establish_session(sess_data);
1405out:
1406 kfree(ntlmssp_blob);
1407 SMB2_sess_free_buffer(sess_data);
1408 kfree(ses->ntlmssp);
1409 ses->ntlmssp = NULL;
1410 sess_data->result = rc;
1411 sess_data->func = NULL;
1412}
1413
1414static int
1415SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1416{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301417 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001418
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301419 type = smb2_select_sectype(ses->server, ses->sectype);
1420 cifs_dbg(FYI, "sess setup type %d\n", type);
1421 if (type == Unspecified) {
1422 cifs_dbg(VFS,
1423 "Unable to select appropriate authentication method!");
1424 return -EINVAL;
1425 }
1426
1427 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001428 case Kerberos:
1429 sess_data->func = SMB2_auth_kerberos;
1430 break;
1431 case RawNTLMSSP:
1432 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1433 break;
1434 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301435 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001436 return -EOPNOTSUPP;
1437 }
1438
1439 return 0;
1440}
1441
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001442int
1443SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1444 const struct nls_table *nls_cp)
1445{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001446 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001447 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001448 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001449
Joe Perchesf96637b2013-05-04 22:12:25 -05001450 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001451
Jeff Layton3534b852013-05-24 07:41:01 -04001452 if (!server) {
1453 WARN(1, "%s: server is NULL!\n", __func__);
1454 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001455 }
1456
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001457 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1458 if (!sess_data)
1459 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001460
1461 rc = SMB2_select_sec(ses, sess_data);
1462 if (rc)
1463 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001464 sess_data->xid = xid;
1465 sess_data->ses = ses;
1466 sess_data->buf0_type = CIFS_NO_BUFFER;
1467 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001468 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001469
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001470 /*
1471 * Initialize the session hash with the server one.
1472 */
1473 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1474 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001475
Sachin Prabhu166cea42016-10-07 19:11:22 +01001476 while (sess_data->func)
1477 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001478
Steve Frenchc721c382017-09-19 18:40:03 -05001479 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1480 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001481 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001482out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001483 kfree(sess_data);
1484 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001485}
1486
1487int
1488SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1489{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001490 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001491 struct smb2_logoff_req *req; /* response is also trivial struct */
1492 int rc = 0;
1493 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001494 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001495 unsigned int total_len;
1496 struct kvec iov[1];
1497 struct kvec rsp_iov;
1498 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001499
Joe Perchesf96637b2013-05-04 22:12:25 -05001500 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001501
1502 if (ses && (ses->server))
1503 server = ses->server;
1504 else
1505 return -EIO;
1506
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001507 /* no need to send SMB logoff if uid already closed due to reconnect */
1508 if (ses->need_reconnect)
1509 goto smb2_session_already_dead;
1510
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001511 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001512 if (rc)
1513 return rc;
1514
1515 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001516 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001517
1518 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1519 flags |= CIFS_TRANSFORM_REQ;
1520 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001521 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001522
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001523 flags |= CIFS_NO_RESP;
1524
1525 iov[0].iov_base = (char *)req;
1526 iov[0].iov_len = total_len;
1527
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001528 memset(&rqst, 0, sizeof(struct smb_rqst));
1529 rqst.rq_iov = iov;
1530 rqst.rq_nvec = 1;
1531
1532 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001533 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001534 /*
1535 * No tcon so can't do
1536 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1537 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001538
1539smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001540 return rc;
1541}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001542
1543static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1544{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001545 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001546}
1547
1548#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1549
Steve Frenchde9f68df2013-11-15 11:26:24 -06001550/* These are similar values to what Windows uses */
1551static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1552{
1553 tcon->max_chunks = 256;
1554 tcon->max_bytes_chunk = 1048576;
1555 tcon->max_bytes_copy = 16777216;
1556}
1557
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001558int
1559SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1560 struct cifs_tcon *tcon, const struct nls_table *cp)
1561{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001562 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001563 struct smb2_tree_connect_req *req;
1564 struct smb2_tree_connect_rsp *rsp = NULL;
1565 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001566 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001567 int rc = 0;
1568 int resp_buftype;
1569 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001570 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001571 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001572 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001573
Joe Perchesf96637b2013-05-04 22:12:25 -05001574 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001575
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001576 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001577 return -EIO;
1578
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001579 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1580 if (unc_path == NULL)
1581 return -ENOMEM;
1582
1583 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1584 unc_path_len *= 2;
1585 if (unc_path_len < 2) {
1586 kfree(unc_path);
1587 return -EINVAL;
1588 }
1589
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001590 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001591 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001592 atomic_set(&tcon->num_remote_opens, 0);
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001593 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1594 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001595 if (rc) {
1596 kfree(unc_path);
1597 return rc;
1598 }
1599
Steve French5a77e752018-05-09 17:43:08 -05001600 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001601 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001602
1603 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001604 /* 1 for pad */
1605 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001606
1607 /* Testing shows that buffer offset must be at location of Buffer[0] */
1608 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001609 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001610 req->PathLength = cpu_to_le16(unc_path_len - 2);
1611 iov[1].iov_base = unc_path;
1612 iov[1].iov_len = unc_path_len;
1613
Steve French6188f282018-03-13 02:29:36 -05001614 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1615 if ((ses->server->dialect == SMB311_PROT_ID) &&
Steve French5a77e752018-05-09 17:43:08 -05001616 !smb3_encryption_required(tcon))
Steve French6188f282018-03-13 02:29:36 -05001617 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
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 = 2;
1622
Steve French4fe75c42019-02-14 01:19:02 -06001623 /* Need 64 for max size write so ask for more in case not there yet */
1624 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1625
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001626 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001627 cifs_small_buf_release(req);
1628 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001629 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001630 if (rc != 0) {
1631 if (tcon) {
1632 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1633 tcon->need_reconnect = true;
1634 }
1635 goto tcon_error_exit;
1636 }
1637
Christophe JAILLETcd123002017-05-12 17:59:32 +02001638 switch (rsp->ShareType) {
1639 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001640 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001641 break;
1642 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001643 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001644 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001645 break;
1646 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001647 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001648 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001649 break;
1650 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001651 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001652 rc = -EOPNOTSUPP;
1653 goto tcon_error_exit;
1654 }
1655
1656 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001657 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001658 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1659 tcon->tidStatus = CifsGood;
1660 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001661 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001662 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001663
1664 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1665 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001666 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001667
1668 if (tcon->seal &&
1669 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1670 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1671
Steve Frenchde9f68df2013-11-15 11:26:24 -06001672 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001673 if (tcon->ses->server->ops->validate_negotiate)
1674 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001675tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001676
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001677 free_rsp_buf(resp_buftype, rsp);
1678 kfree(unc_path);
1679 return rc;
1680
1681tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001682 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001683 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001684 }
1685 goto tcon_exit;
1686}
1687
1688int
1689SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1690{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001691 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001692 struct smb2_tree_disconnect_req *req; /* response is trivial */
1693 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001694 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001695 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001696 unsigned int total_len;
1697 struct kvec iov[1];
1698 struct kvec rsp_iov;
1699 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001700
Joe Perchesf96637b2013-05-04 22:12:25 -05001701 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001702
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001703 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001704 return -EIO;
1705
1706 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1707 return 0;
1708
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001709 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1710 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001711 if (rc)
1712 return rc;
1713
Steve French5a77e752018-05-09 17:43:08 -05001714 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001715 flags |= CIFS_TRANSFORM_REQ;
1716
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001717 flags |= CIFS_NO_RESP;
1718
1719 iov[0].iov_base = (char *)req;
1720 iov[0].iov_len = total_len;
1721
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001722 memset(&rqst, 0, sizeof(struct smb_rqst));
1723 rqst.rq_iov = iov;
1724 rqst.rq_nvec = 1;
1725
1726 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001727 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001728 if (rc)
1729 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1730
1731 return rc;
1732}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001733
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001734
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001735static struct create_durable *
1736create_durable_buf(void)
1737{
1738 struct create_durable *buf;
1739
1740 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1741 if (!buf)
1742 return NULL;
1743
1744 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001745 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001746 buf->ccontext.DataLength = cpu_to_le32(16);
1747 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1748 (struct create_durable, Name));
1749 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001750 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001751 buf->Name[0] = 'D';
1752 buf->Name[1] = 'H';
1753 buf->Name[2] = 'n';
1754 buf->Name[3] = 'Q';
1755 return buf;
1756}
1757
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001758static struct create_durable *
1759create_reconnect_durable_buf(struct cifs_fid *fid)
1760{
1761 struct create_durable *buf;
1762
1763 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1764 if (!buf)
1765 return NULL;
1766
1767 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1768 (struct create_durable, Data));
1769 buf->ccontext.DataLength = cpu_to_le32(16);
1770 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1771 (struct create_durable, Name));
1772 buf->ccontext.NameLength = cpu_to_le16(4);
1773 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1774 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001775 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001776 buf->Name[0] = 'D';
1777 buf->Name[1] = 'H';
1778 buf->Name[2] = 'n';
1779 buf->Name[3] = 'C';
1780 return buf;
1781}
1782
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001783static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001784parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001785 unsigned int *epoch, char *lease_key)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001786{
1787 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001788 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001789 unsigned int next;
1790 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001791 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001792
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001793 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001794 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001795 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001796 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001797 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001798 if (le16_to_cpu(cc->NameLength) == 4 &&
1799 strncmp(name, "RqLs", 4) == 0)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06001800 return server->ops->parse_lease_buf(cc, epoch,
1801 lease_key);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001802
1803 next = le32_to_cpu(cc->Next);
1804 if (!next)
1805 break;
1806 remaining -= next;
1807 cc = (struct create_context *)((char *)cc + next);
1808 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001809
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001810 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001811}
1812
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001813static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001814add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02001815 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001816{
1817 struct smb2_create_req *req = iov[0].iov_base;
1818 unsigned int num = *num_iovec;
1819
Stefano Brivio729c0c92018-07-05 15:10:02 +02001820 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001821 if (iov[num].iov_base == NULL)
1822 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001823 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001824 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1825 if (!req->CreateContextsOffset)
1826 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001827 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001828 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001829 le32_add_cpu(&req->CreateContextsLength,
1830 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001831 *num_iovec = num + 1;
1832 return 0;
1833}
1834
Steve Frenchb56eae42015-11-03 09:26:27 -06001835static struct create_durable_v2 *
1836create_durable_v2_buf(struct cifs_fid *pfid)
1837{
1838 struct create_durable_v2 *buf;
1839
1840 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1841 if (!buf)
1842 return NULL;
1843
1844 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1845 (struct create_durable_v2, dcontext));
1846 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1847 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1848 (struct create_durable_v2, Name));
1849 buf->ccontext.NameLength = cpu_to_le16(4);
1850
1851 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1852 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001853 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001854 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1855
1856 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1857 buf->Name[0] = 'D';
1858 buf->Name[1] = 'H';
1859 buf->Name[2] = '2';
1860 buf->Name[3] = 'Q';
1861 return buf;
1862}
1863
1864static struct create_durable_handle_reconnect_v2 *
1865create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1866{
1867 struct create_durable_handle_reconnect_v2 *buf;
1868
1869 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1870 GFP_KERNEL);
1871 if (!buf)
1872 return NULL;
1873
1874 buf->ccontext.DataOffset =
1875 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1876 dcontext));
1877 buf->ccontext.DataLength =
1878 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1879 buf->ccontext.NameOffset =
1880 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1881 Name));
1882 buf->ccontext.NameLength = cpu_to_le16(4);
1883
1884 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1885 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1886 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1887 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1888
1889 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1890 buf->Name[0] = 'D';
1891 buf->Name[1] = 'H';
1892 buf->Name[2] = '2';
1893 buf->Name[3] = 'C';
1894 return buf;
1895}
1896
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001897static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001898add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001899 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001900{
1901 struct smb2_create_req *req = iov[0].iov_base;
1902 unsigned int num = *num_iovec;
1903
Steve Frenchb56eae42015-11-03 09:26:27 -06001904 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1905 if (iov[num].iov_base == NULL)
1906 return -ENOMEM;
1907 iov[num].iov_len = sizeof(struct create_durable_v2);
1908 if (!req->CreateContextsOffset)
1909 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001910 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001911 iov[1].iov_len);
1912 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001913 *num_iovec = num + 1;
1914 return 0;
1915}
1916
1917static int
1918add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1919 struct cifs_open_parms *oparms)
1920{
1921 struct smb2_create_req *req = iov[0].iov_base;
1922 unsigned int num = *num_iovec;
1923
1924 /* indicate that we don't need to relock the file */
1925 oparms->reconnect = false;
1926
1927 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1928 if (iov[num].iov_base == NULL)
1929 return -ENOMEM;
1930 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1931 if (!req->CreateContextsOffset)
1932 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001933 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001934 iov[1].iov_len);
1935 le32_add_cpu(&req->CreateContextsLength,
1936 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001937 *num_iovec = num + 1;
1938 return 0;
1939}
1940
1941static int
1942add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1943 struct cifs_open_parms *oparms, bool use_persistent)
1944{
1945 struct smb2_create_req *req = iov[0].iov_base;
1946 unsigned int num = *num_iovec;
1947
1948 if (use_persistent) {
1949 if (oparms->reconnect)
1950 return add_durable_reconnect_v2_context(iov, num_iovec,
1951 oparms);
1952 else
1953 return add_durable_v2_context(iov, num_iovec, oparms);
1954 }
1955
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001956 if (oparms->reconnect) {
1957 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1958 /* indicate that we don't need to relock the file */
1959 oparms->reconnect = false;
1960 } else
1961 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001962 if (iov[num].iov_base == NULL)
1963 return -ENOMEM;
1964 iov[num].iov_len = sizeof(struct create_durable);
1965 if (!req->CreateContextsOffset)
1966 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001967 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001968 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001969 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001970 *num_iovec = num + 1;
1971 return 0;
1972}
1973
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05001974/* See MS-SMB2 2.2.13.2.7 */
1975static struct crt_twarp_ctxt *
1976create_twarp_buf(__u64 timewarp)
1977{
1978 struct crt_twarp_ctxt *buf;
1979
1980 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
1981 if (!buf)
1982 return NULL;
1983
1984 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1985 (struct crt_twarp_ctxt, Timestamp));
1986 buf->ccontext.DataLength = cpu_to_le32(8);
1987 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1988 (struct crt_twarp_ctxt, Name));
1989 buf->ccontext.NameLength = cpu_to_le16(4);
1990 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
1991 buf->Name[0] = 'T';
1992 buf->Name[1] = 'W';
1993 buf->Name[2] = 'r';
1994 buf->Name[3] = 'p';
1995 buf->Timestamp = cpu_to_le64(timewarp);
1996 return buf;
1997}
1998
1999/* See MS-SMB2 2.2.13.2.7 */
2000static int
2001add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2002{
2003 struct smb2_create_req *req = iov[0].iov_base;
2004 unsigned int num = *num_iovec;
2005
2006 iov[num].iov_base = create_twarp_buf(timewarp);
2007 if (iov[num].iov_base == NULL)
2008 return -ENOMEM;
2009 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2010 if (!req->CreateContextsOffset)
2011 req->CreateContextsOffset = cpu_to_le32(
2012 sizeof(struct smb2_create_req) +
2013 iov[num - 1].iov_len);
2014 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2015 *num_iovec = num + 1;
2016 return 0;
2017}
2018
Aurelien Aptelf0712922017-02-22 14:47:17 +01002019static int
2020alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2021 const char *treename, const __le16 *path)
2022{
2023 int treename_len, path_len;
2024 struct nls_table *cp;
2025 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2026
2027 /*
2028 * skip leading "\\"
2029 */
2030 treename_len = strlen(treename);
2031 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2032 return -EINVAL;
2033
2034 treename += 2;
2035 treename_len -= 2;
2036
2037 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2038
2039 /*
2040 * make room for one path separator between the treename and
2041 * path
2042 */
2043 *out_len = treename_len + 1 + path_len;
2044
2045 /*
2046 * final path needs to be null-terminated UTF16 with a
2047 * size aligned to 8
2048 */
2049
2050 *out_size = roundup((*out_len+1)*2, 8);
2051 *out_path = kzalloc(*out_size, GFP_KERNEL);
2052 if (!*out_path)
2053 return -ENOMEM;
2054
2055 cp = load_nls_default();
2056 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2057 UniStrcat(*out_path, sep);
2058 UniStrcat(*out_path, path);
2059 unload_nls(cp);
2060
2061 return 0;
2062}
2063
Steve Frenchbea851b2018-06-14 21:56:32 -05002064int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2065 umode_t mode, struct cifs_tcon *tcon,
2066 const char *full_path,
2067 struct cifs_sb_info *cifs_sb)
2068{
2069 struct smb_rqst rqst;
2070 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002071 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002072 struct cifs_ses *ses = tcon->ses;
2073 struct kvec iov[3]; /* make sure at least one for each open context */
2074 struct kvec rsp_iov = {NULL, 0};
2075 int resp_buftype;
2076 int uni_path_len;
2077 __le16 *copy_path = NULL;
2078 int copy_size;
2079 int rc = 0;
2080 unsigned int n_iov = 2;
2081 __u32 file_attributes = 0;
2082 char *pc_buf = NULL;
2083 int flags = 0;
2084 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002085 __le16 *utf16_path = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002086
2087 cifs_dbg(FYI, "mkdir\n");
2088
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002089 /* resource #1: path allocation */
2090 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2091 if (!utf16_path)
2092 return -ENOMEM;
2093
YueHaibing29cbfa12018-12-18 02:51:51 +00002094 if (!ses || !(ses->server)) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002095 rc = -EIO;
2096 goto err_free_path;
2097 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002098
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002099 /* resource #2: request */
Steve Frenchbea851b2018-06-14 21:56:32 -05002100 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002101 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002102 goto err_free_path;
2103
Steve Frenchbea851b2018-06-14 21:56:32 -05002104
2105 if (smb3_encryption_required(tcon))
2106 flags |= CIFS_TRANSFORM_REQ;
2107
Steve Frenchbea851b2018-06-14 21:56:32 -05002108 req->ImpersonationLevel = IL_IMPERSONATION;
2109 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2110 /* File attributes ignored on open (used in create though) */
2111 req->FileAttributes = cpu_to_le32(file_attributes);
2112 req->ShareAccess = FILE_SHARE_ALL_LE;
2113 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2114 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2115
2116 iov[0].iov_base = (char *)req;
2117 /* -1 since last byte is buf[0] which is sent below (path) */
2118 iov[0].iov_len = total_len - 1;
2119
2120 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2121
2122 /* [MS-SMB2] 2.2.13 NameOffset:
2123 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2124 * the SMB2 header, the file name includes a prefix that will
2125 * be processed during DFS name normalization as specified in
2126 * section 3.3.5.9. Otherwise, the file name is relative to
2127 * the share that is identified by the TreeId in the SMB2
2128 * header.
2129 */
2130 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2131 int name_len;
2132
2133 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2134 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2135 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002136 tcon->treeName, utf16_path);
2137 if (rc)
2138 goto err_free_req;
2139
Steve Frenchbea851b2018-06-14 21:56:32 -05002140 req->NameLength = cpu_to_le16(name_len * 2);
2141 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002142 /* free before overwriting resource */
2143 kfree(utf16_path);
2144 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002145 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002146 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002147 /* MUST set path len (NameLength) to 0 opening root of share */
2148 req->NameLength = cpu_to_le16(uni_path_len - 2);
2149 if (uni_path_len % 8 != 0) {
2150 copy_size = roundup(uni_path_len, 8);
2151 copy_path = kzalloc(copy_size, GFP_KERNEL);
2152 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002153 rc = -ENOMEM;
2154 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002155 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002156 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002157 uni_path_len);
2158 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002159 /* free before overwriting resource */
2160 kfree(utf16_path);
2161 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002162 }
2163 }
2164
2165 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002166 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002167 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2168
2169 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002170 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002171 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002172 if (rc)
2173 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002174 pc_buf = iov[n_iov-1].iov_base;
2175 }
2176
2177
2178 memset(&rqst, 0, sizeof(struct smb_rqst));
2179 rqst.rq_iov = iov;
2180 rqst.rq_nvec = n_iov;
2181
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002182 /* resource #4: response buffer */
2183 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2184 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002185 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2186 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002187 CREATE_NOT_FILE,
2188 FILE_WRITE_ATTRIBUTES, rc);
2189 goto err_free_rsp_buf;
2190 }
2191
2192 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2193 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2194 ses->Suid, CREATE_NOT_FILE,
2195 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002196
2197 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2198
2199 /* Eventually save off posix specific response info and timestaps */
2200
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002201err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002202 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002203 kfree(pc_buf);
2204err_free_req:
2205 cifs_small_buf_release(req);
2206err_free_path:
2207 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002208 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002209}
Steve Frenchbea851b2018-06-14 21:56:32 -05002210
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002211int
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002212SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2213 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002214{
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002215 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002216 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002217 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002218 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002219 int copy_size;
2220 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002221 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002222 struct kvec *iov = rqst->rq_iov;
2223 __le16 *copy_path;
2224 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002225
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002226 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002227 if (rc)
2228 return rc;
2229
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002230 iov[0].iov_base = (char *)req;
2231 /* -1 since last byte is buf[0] which is sent below (path) */
2232 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002233
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002234 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002235 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002236 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2237 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002238
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002239 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002240 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002241 /* File attributes ignored on open (used in create though) */
2242 req->FileAttributes = cpu_to_le32(file_attributes);
2243 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002244 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2245 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002246 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002247
2248 /* [MS-SMB2] 2.2.13 NameOffset:
2249 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2250 * the SMB2 header, the file name includes a prefix that will
2251 * be processed during DFS name normalization as specified in
2252 * section 3.3.5.9. Otherwise, the file name is relative to
2253 * the share that is identified by the TreeId in the SMB2
2254 * header.
2255 */
2256 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2257 int name_len;
2258
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002259 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002260 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2261 &name_len,
2262 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002263 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002264 return rc;
2265 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002266 uni_path_len = copy_size;
2267 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002268 } else {
2269 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2270 /* MUST set path len (NameLength) to 0 opening root of share */
2271 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002272 copy_size = uni_path_len;
2273 if (copy_size % 8 != 0)
2274 copy_size = roundup(copy_size, 8);
2275 copy_path = kzalloc(copy_size, GFP_KERNEL);
2276 if (!copy_path)
2277 return -ENOMEM;
2278 memcpy((char *)copy_path, (const char *)path,
2279 uni_path_len);
2280 uni_path_len = copy_size;
2281 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002282 }
2283
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002284 iov[1].iov_len = uni_path_len;
2285 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002286
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002287 if (!server->oplocks)
2288 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2289
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002290 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002291 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2292 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002293 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2294 (oparms->create_options & CREATE_NOT_FILE))
2295 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002296 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002297 rc = add_lease_context(server, iov, &n_iov,
2298 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002299 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002300 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002301 }
2302
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002303 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2304 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002305 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002306 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002307 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002308 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002309 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002310 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002311
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002312 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002313 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002314 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002315 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002316 }
2317
Steve Frenchce558b02018-05-31 19:16:54 -05002318 if (tcon->posix_extensions) {
2319 if (n_iov > 2) {
2320 struct create_context *ccontext =
2321 (struct create_context *)iov[n_iov-1].iov_base;
2322 ccontext->Next =
2323 cpu_to_le32(iov[n_iov-1].iov_len);
2324 }
2325
2326 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002327 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002328 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002329 }
Steve Frenchce558b02018-05-31 19:16:54 -05002330
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002331 if (tcon->snapshot_time) {
2332 cifs_dbg(FYI, "adding snapshot context\n");
2333 if (n_iov > 2) {
2334 struct create_context *ccontext =
2335 (struct create_context *)iov[n_iov-1].iov_base;
2336 ccontext->Next =
2337 cpu_to_le32(iov[n_iov-1].iov_len);
2338 }
2339
2340 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2341 if (rc)
2342 return rc;
2343 }
2344
2345
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002346 rqst->rq_nvec = n_iov;
2347 return 0;
2348}
2349
2350/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2351 * All other vectors are freed by kfree().
2352 */
2353void
2354SMB2_open_free(struct smb_rqst *rqst)
2355{
2356 int i;
2357
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002358 if (rqst && rqst->rq_iov) {
2359 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2360 for (i = 1; i < rqst->rq_nvec; i++)
2361 if (rqst->rq_iov[i].iov_base != smb2_padding)
2362 kfree(rqst->rq_iov[i].iov_base);
2363 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002364}
2365
2366int
2367SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2368 __u8 *oplock, struct smb2_file_all_info *buf,
2369 struct kvec *err_iov, int *buftype)
2370{
2371 struct smb_rqst rqst;
2372 struct smb2_create_rsp *rsp = NULL;
2373 struct TCP_Server_Info *server;
2374 struct cifs_tcon *tcon = oparms->tcon;
2375 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002376 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002377 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002378 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002379 int rc = 0;
2380 int flags = 0;
2381
2382 cifs_dbg(FYI, "create/open\n");
2383 if (ses && (ses->server))
2384 server = ses->server;
2385 else
2386 return -EIO;
2387
2388 if (smb3_encryption_required(tcon))
2389 flags |= CIFS_TRANSFORM_REQ;
2390
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002391 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002392 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002393 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002394 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002395
2396 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2397 if (rc)
2398 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002399
2400 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002401 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002402 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002403
2404 if (rc != 0) {
2405 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002406 if (err_iov && rsp) {
2407 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002408 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002409 resp_buftype = CIFS_NO_BUFFER;
2410 rsp = NULL;
2411 }
Steve French28d59362018-05-30 21:42:34 -05002412 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2413 oparms->create_options, oparms->desired_access, rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002414 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002415 } else
2416 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2417 ses->Suid, oparms->create_options,
2418 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002419
Steve Frenchfae80442018-10-19 17:14:32 -05002420 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002421 oparms->fid->persistent_fid = rsp->PersistentFileId;
2422 oparms->fid->volatile_fid = rsp->VolatileFileId;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002423#ifdef CONFIG_CIFS_DEBUG2
2424 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2425#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002426
2427 if (buf) {
2428 memcpy(buf, &rsp->CreationTime, 32);
2429 buf->AllocationSize = rsp->AllocationSize;
2430 buf->EndOfFile = rsp->EndofFile;
2431 buf->Attributes = rsp->FileAttributes;
2432 buf->NumberOfLinks = cpu_to_le32(1);
2433 buf->DeletePending = 0;
2434 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002435
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002436 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002437 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch,
2438 oparms->fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002439 else
2440 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002441creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002442 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002443 free_rsp_buf(resp_buftype, rsp);
2444 return rc;
2445}
2446
Steve French4a72daf2013-06-25 00:20:49 -05002447/*
2448 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2449 */
2450int
2451SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002452 u64 volatile_fid, u32 opcode, bool is_fsctl,
Aurelien Aptel51146622017-02-28 15:08:41 +01002453 char *in_data, u32 indatalen,
2454 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05002455{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002456 struct smb_rqst rqst;
Steve French4a72daf2013-06-25 00:20:49 -05002457 struct smb2_ioctl_req *req;
2458 struct smb2_ioctl_rsp *rsp;
Steve French8e353102015-03-26 19:47:02 -05002459 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05002460 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002461 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002462 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002463 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05002464 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002465 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002466 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05002467
2468 cifs_dbg(FYI, "SMB2 IOCTL\n");
2469
Steve French3d1a3742014-08-11 21:05:25 -05002470 if (out_data != NULL)
2471 *out_data = NULL;
2472
Steve French4a72daf2013-06-25 00:20:49 -05002473 /* zero out returned data len, in case of error */
2474 if (plen)
2475 *plen = 0;
2476
Steve French8e353102015-03-26 19:47:02 -05002477 if (tcon)
2478 ses = tcon->ses;
2479 else
2480 return -EIO;
2481
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002482 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05002483 return -EIO;
2484
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002485 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002486 if (rc)
2487 return rc;
2488
Steve French5a77e752018-05-09 17:43:08 -05002489 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002490 flags |= CIFS_TRANSFORM_REQ;
2491
Steve French4a72daf2013-06-25 00:20:49 -05002492 req->CtlCode = cpu_to_le32(opcode);
2493 req->PersistentFileId = persistent_fid;
2494 req->VolatileFileId = volatile_fid;
2495
2496 if (indatalen) {
2497 req->InputCount = cpu_to_le32(indatalen);
2498 /* do not set InputOffset if no input data */
2499 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002500 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05002501 iov[1].iov_base = in_data;
2502 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002503 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05002504 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002505 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05002506
2507 req->OutputOffset = 0;
2508 req->OutputCount = 0; /* MBZ */
2509
2510 /*
2511 * Could increase MaxOutputResponse, but that would require more
2512 * than one credit. Windows typically sets this smaller, but for some
2513 * ioctls it may be useful to allow server to send more. No point
2514 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05002515 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2516 * (by default, note that it can be overridden to make max larger)
2517 * in responses (except for read responses which can be bigger.
2518 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05002519 */
Steve French7db0a6e2017-05-03 21:12:20 -05002520 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05002521
2522 if (is_fsctl)
2523 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2524 else
2525 req->Flags = 0;
2526
2527 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05002528
Steve French7ff8d452013-10-14 00:44:19 -05002529 /*
2530 * If no input data, the size of ioctl struct in
2531 * protocol spec still includes a 1 byte data buffer,
2532 * but if input data passed to ioctl, we do not
2533 * want to double count this, so we do not send
2534 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002535 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05002536 */
2537
2538 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002539 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05002540 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002541 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05002542
Steve French4587eee2017-10-25 15:58:31 -05002543 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2544 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002545 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002546
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002547 memset(&rqst, 0, sizeof(struct smb_rqst));
2548 rqst.rq_iov = iov;
2549 rqst.rq_nvec = n_iov;
2550
2551 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002552 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002553 cifs_small_buf_release(req);
2554 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002555
Steve Frencheccb4422018-05-17 21:16:55 -05002556 if (rc != 0)
2557 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2558 ses->Suid, 0, opcode, rc);
2559
Steve French9bf0c9c2013-11-16 18:05:28 -06002560 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002561 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002562 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002563 } else if (rc == -EINVAL) {
2564 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2565 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002566 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002567 goto ioctl_exit;
2568 }
Steve French4a72daf2013-06-25 00:20:49 -05002569 }
2570
2571 /* check if caller wants to look at return data or just return rc */
2572 if ((plen == NULL) || (out_data == NULL))
2573 goto ioctl_exit;
2574
2575 *plen = le32_to_cpu(rsp->OutputCount);
2576
2577 /* We check for obvious errors in the output buffer length and offset */
2578 if (*plen == 0)
2579 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002580 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Steve French4a72daf2013-06-25 00:20:49 -05002581 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2582 *plen = 0;
2583 rc = -EIO;
2584 goto ioctl_exit;
2585 }
2586
Dan Carpenter2d204ee2018-09-10 14:12:07 +03002587 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Steve French4a72daf2013-06-25 00:20:49 -05002588 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2589 le32_to_cpu(rsp->OutputOffset));
2590 *plen = 0;
2591 rc = -EIO;
2592 goto ioctl_exit;
2593 }
2594
YueHaibingd034fee2018-09-10 01:33:06 +00002595 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2596 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05002597 if (*out_data == NULL) {
2598 rc = -ENOMEM;
2599 goto ioctl_exit;
2600 }
2601
Steve French4a72daf2013-06-25 00:20:49 -05002602ioctl_exit:
2603 free_rsp_buf(resp_buftype, rsp);
2604 return rc;
2605}
2606
Steve French64a5cfa2013-10-14 15:31:32 -05002607/*
2608 * Individual callers to ioctl worker function follow
2609 */
2610
2611int
2612SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2613 u64 persistent_fid, u64 volatile_fid)
2614{
2615 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002616 struct compress_ioctl fsctl_input;
2617 char *ret_data = NULL;
2618
2619 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002620 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002621
2622 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2623 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2624 (char *)&fsctl_input /* data input */,
2625 2 /* in data len */, &ret_data /* out data */, NULL);
2626
2627 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002628
2629 return rc;
2630}
2631
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002632int
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002633SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2634 u64 persistent_fid, u64 volatile_fid)
2635{
2636 struct smb2_close_req *req;
2637 struct kvec *iov = rqst->rq_iov;
2638 unsigned int total_len;
2639 int rc;
2640
2641 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2642 if (rc)
2643 return rc;
2644
2645 req->PersistentFileId = persistent_fid;
2646 req->VolatileFileId = volatile_fid;
2647 iov[0].iov_base = (char *)req;
2648 iov[0].iov_len = total_len;
2649
2650 return 0;
2651}
2652
2653void
2654SMB2_close_free(struct smb_rqst *rqst)
2655{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002656 if (rqst && rqst->rq_iov)
2657 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002658}
2659
2660int
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002661SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2662 u64 persistent_fid, u64 volatile_fid, int flags)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002663{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002664 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002665 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002666 struct cifs_ses *ses = tcon->ses;
2667 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002668 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01002669 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002670 int rc = 0;
2671
Joe Perchesf96637b2013-05-04 22:12:25 -05002672 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002673
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002674 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002675 return -EIO;
2676
Steve French5a77e752018-05-09 17:43:08 -05002677 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002678 flags |= CIFS_TRANSFORM_REQ;
2679
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002680 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002681 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002682 rqst.rq_iov = iov;
2683 rqst.rq_nvec = 1;
2684
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002685 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2686 if (rc)
2687 goto close_exit;
2688
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002689 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002690 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002691
2692 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002693 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002694 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2695 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002696 goto close_exit;
2697 }
2698
Steve Frenchfae80442018-10-19 17:14:32 -05002699 atomic_dec(&tcon->num_remote_opens);
2700
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002701 /* BB FIXME - decode close response, update inode for caching */
2702
2703close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10002704 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002705 free_rsp_buf(resp_buftype, rsp);
2706 return rc;
2707}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002708
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06002709int
2710SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2711 u64 persistent_fid, u64 volatile_fid)
2712{
2713 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2714}
2715
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002716int
2717smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2718 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002719{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10002720 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002721 char *end_of_smb = smb_len + (char *)iov->iov_base;
2722 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002723 char *end_of_buf = begin_of_buf + buffer_length;
2724
2725
2726 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002727 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2728 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002729 return -EINVAL;
2730 }
2731
2732 /* check if beyond RFC1001 maximum length */
2733 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002734 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2735 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002736 return -EINVAL;
2737 }
2738
2739 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002740 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002741 return -EINVAL;
2742 }
2743
2744 return 0;
2745}
2746
2747/*
2748 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2749 * Caller must free buffer.
2750 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002751int
2752smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2753 struct kvec *iov, unsigned int minbufsize,
2754 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002755{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002756 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002757 int rc;
2758
2759 if (!data)
2760 return -EINVAL;
2761
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002762 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002763 if (rc)
2764 return rc;
2765
2766 memcpy(data, begin_of_buf, buffer_length);
2767
2768 return 0;
2769}
2770
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002771int
2772SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2773 u64 persistent_fid, u64 volatile_fid,
2774 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002775 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002776{
2777 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002778 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002779 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002780 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002781
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002782 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2783 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002784 if (rc)
2785 return rc;
2786
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002787 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002788 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002789 req->PersistentFileId = persistent_fid;
2790 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002791 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002792
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002793 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002794 if (input_len) {
2795 req->InputBufferLength = cpu_to_le32(input_len);
2796 /* total_len for smb query request never close to le16 max */
2797 req->InputBufferOffset = cpu_to_le16(total_len - 1);
2798 memcpy(req->Buffer, input, input_len);
2799 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002800
2801 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002802 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002803 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002804 return 0;
2805}
2806
2807void
2808SMB2_query_info_free(struct smb_rqst *rqst)
2809{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002810 if (rqst && rqst->rq_iov)
2811 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002812}
2813
2814static int
2815query_info(const unsigned int xid, struct cifs_tcon *tcon,
2816 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2817 u32 additional_info, size_t output_len, size_t min_len, void **data,
2818 u32 *dlen)
2819{
2820 struct smb_rqst rqst;
2821 struct smb2_query_info_rsp *rsp = NULL;
2822 struct kvec iov[1];
2823 struct kvec rsp_iov;
2824 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01002825 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002826 struct cifs_ses *ses = tcon->ses;
2827 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00002828 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002829
2830 cifs_dbg(FYI, "Query Info\n");
2831
2832 if (!ses || !(ses->server))
2833 return -EIO;
2834
2835 if (smb3_encryption_required(tcon))
2836 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002837
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002838 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002839 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002840 rqst.rq_iov = iov;
2841 rqst.rq_nvec = 1;
2842
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002843 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
2844 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002845 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002846 if (rc)
2847 goto qinf_exit;
2848
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002849 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002850 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002851
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002852 if (rc) {
2853 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05002854 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2855 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002856 goto qinf_exit;
2857 }
2858
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002859 if (dlen) {
2860 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2861 if (!*data) {
2862 *data = kmalloc(*dlen, GFP_KERNEL);
2863 if (!*data) {
2864 cifs_dbg(VFS,
2865 "Error %d allocating memory for acl\n",
2866 rc);
2867 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00002868 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002869 goto qinf_exit;
2870 }
Colin Ian King73aaf922019-01-16 16:28:59 +00002871 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002872 }
2873 }
2874
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002875 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
2876 le32_to_cpu(rsp->OutputBufferLength),
2877 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00002878 if (rc && allocated) {
2879 kfree(*data);
2880 *data = NULL;
2881 *dlen = 0;
2882 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002883
2884qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10002885 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002886 free_rsp_buf(resp_buftype, rsp);
2887 return rc;
2888}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002889
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002890int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2891 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002892{
2893 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002894 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002895 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002896 sizeof(struct smb2_file_all_info), (void **)&data,
2897 NULL);
2898}
2899
2900int
2901SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2902 u64 persistent_fid, u64 volatile_fid,
2903 void **data, u32 *plen)
2904{
2905 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2906 *plen = 0;
2907
2908 return query_info(xid, tcon, persistent_fid, volatile_fid,
2909 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05002910 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002911}
2912
2913int
2914SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2915 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2916{
2917 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002918 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002919 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002920 sizeof(struct smb2_file_internal_info),
2921 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002922}
2923
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002924/*
2925 * This is a no-op for now. We're not really interested in the reply, but
2926 * rather in the fact that the server sent one and that server->lstrp
2927 * gets updated.
2928 *
2929 * FIXME: maybe we should consider checking that the reply matches request?
2930 */
2931static void
2932smb2_echo_callback(struct mid_q_entry *mid)
2933{
2934 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002935 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08002936 unsigned int credits_received = 0;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002937
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08002938 if (mid->mid_state == MID_RESPONSE_RECEIVED
2939 || mid->mid_state == MID_RESPONSE_MALFORMED)
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002940 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002941
2942 DeleteMidQEntry(mid);
2943 add_credits(server, credits_received, CIFS_ECHO_OP);
2944}
2945
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002946void smb2_reconnect_server(struct work_struct *work)
2947{
2948 struct TCP_Server_Info *server = container_of(work,
2949 struct TCP_Server_Info, reconnect.work);
2950 struct cifs_ses *ses;
2951 struct cifs_tcon *tcon, *tcon2;
2952 struct list_head tmp_list;
2953 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002954 int rc;
2955 int resched = false;
2956
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002957
2958 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2959 mutex_lock(&server->reconnect_mutex);
2960
2961 INIT_LIST_HEAD(&tmp_list);
2962 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2963
2964 spin_lock(&cifs_tcp_ses_lock);
2965 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2966 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002967 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002968 tcon->tc_count++;
2969 list_add_tail(&tcon->rlist, &tmp_list);
2970 tcon_exist = true;
2971 }
2972 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002973 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2974 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2975 tcon_exist = true;
2976 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002977 }
2978 /*
2979 * Get the reference to server struct to be sure that the last call of
2980 * cifs_put_tcon() in the loop below won't release the server pointer.
2981 */
2982 if (tcon_exist)
2983 server->srv_count++;
2984
2985 spin_unlock(&cifs_tcp_ses_lock);
2986
2987 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002988 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2989 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002990 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002991 else
2992 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002993 list_del_init(&tcon->rlist);
2994 cifs_put_tcon(tcon);
2995 }
2996
2997 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002998 if (resched)
2999 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003000 mutex_unlock(&server->reconnect_mutex);
3001
3002 /* now we can safely release srv struct */
3003 if (tcon_exist)
3004 cifs_put_tcp_session(server, 1);
3005}
3006
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003007int
3008SMB2_echo(struct TCP_Server_Info *server)
3009{
3010 struct smb2_echo_req *req;
3011 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003012 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003013 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003014 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003015 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003016
Joe Perchesf96637b2013-05-04 22:12:25 -05003017 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003018
Steve French4fcd1812016-06-22 20:12:05 -05003019 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003020 /* No need to send echo on newly established connections */
3021 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3022 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003023 }
3024
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003025 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003026 if (rc)
3027 return rc;
3028
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003029 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003030
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003031 iov[0].iov_len = total_len;
3032 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003033
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003034 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3035 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003036 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003037 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003038
3039 cifs_small_buf_release(req);
3040 return rc;
3041}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003042
3043int
3044SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3045 u64 volatile_fid)
3046{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003047 struct smb_rqst rqst;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003048 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003049 struct cifs_ses *ses = tcon->ses;
3050 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003051 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003052 int resp_buftype;
3053 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003054 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003055 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003056
Joe Perchesf96637b2013-05-04 22:12:25 -05003057 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003058
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003059 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003060 return -EIO;
3061
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003062 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003063 if (rc)
3064 return rc;
3065
Steve French5a77e752018-05-09 17:43:08 -05003066 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003067 flags |= CIFS_TRANSFORM_REQ;
3068
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003069 req->PersistentFileId = persistent_fid;
3070 req->VolatileFileId = volatile_fid;
3071
3072 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003073 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003074
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003075 memset(&rqst, 0, sizeof(struct smb_rqst));
3076 rqst.rq_iov = iov;
3077 rqst.rq_nvec = 1;
3078
3079 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003080 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003081
Steve Frencheccb4422018-05-17 21:16:55 -05003082 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003083 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003084 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3085 rc);
3086 }
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003087
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003088 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003089 return rc;
3090}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003091
3092/*
3093 * To form a chain of read requests, any read requests after the first should
3094 * have the end_of_chain boolean set to true.
3095 */
3096static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003097smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003098 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3099 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003100{
3101 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003102 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003103 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07003104 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003105
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003106 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3107 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003108 if (rc)
3109 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003110
3111 server = io_parms->tcon->ses->server;
3112 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003113 return -ECONNABORTED;
3114
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003115 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003116 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003117
3118 req->PersistentFileId = io_parms->persistent_fid;
3119 req->VolatileFileId = io_parms->volatile_fid;
3120 req->ReadChannelInfoOffset = 0; /* reserved */
3121 req->ReadChannelInfoLength = 0; /* reserved */
3122 req->Channel = 0; /* reserved */
3123 req->MinimumCount = 0;
3124 req->Length = cpu_to_le32(io_parms->length);
3125 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07003126#ifdef CONFIG_CIFS_SMB_DIRECT
3127 /*
3128 * If we want to do a RDMA write, fill in and append
3129 * smbd_buffer_descriptor_v1 to the end of read request
3130 */
Long Libb4c0412018-04-17 12:17:08 -07003131 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003132 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003133
Long Libd3dcc62017-11-22 17:38:47 -07003134 struct smbd_buffer_descriptor_v1 *v1;
3135 bool need_invalidate =
3136 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3137
3138 rdata->mr = smbd_register_mr(
3139 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003140 rdata->nr_pages, rdata->page_offset,
3141 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003142 if (!rdata->mr)
3143 return -ENOBUFS;
3144
3145 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3146 if (need_invalidate)
3147 req->Channel = SMB2_CHANNEL_RDMA_V1;
3148 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003149 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003150 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003151 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003152 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003153 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3154 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3155 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003156
3157 *total_len += sizeof(*v1) - 1;
3158 }
3159#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003160 if (request_type & CHAINED_REQUEST) {
3161 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003162 /* next 8-byte aligned request */
3163 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3164 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003165 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003166 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003167 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003168 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003169 /*
3170 * Related requests use info from previous read request
3171 * in chain.
3172 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003173 shdr->SessionId = 0xFFFFFFFF;
3174 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003175 req->PersistentFileId = 0xFFFFFFFF;
3176 req->VolatileFileId = 0xFFFFFFFF;
3177 }
3178 }
3179 if (remaining_bytes > io_parms->length)
3180 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3181 else
3182 req->RemainingBytes = 0;
3183
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003184 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003185 return rc;
3186}
3187
3188static void
3189smb2_readv_callback(struct mid_q_entry *mid)
3190{
3191 struct cifs_readdata *rdata = mid->callback_data;
3192 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3193 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003194 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003195 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003196 unsigned int credits_received = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003197 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3198 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07003199 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003200 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003201 .rq_npages = rdata->nr_pages,
3202 .rq_pagesz = rdata->pagesz,
3203 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003204
Joe Perchesf96637b2013-05-04 22:12:25 -05003205 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3206 __func__, mid->mid, mid->mid_state, rdata->result,
3207 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003208
3209 switch (mid->mid_state) {
3210 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003211 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003212 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003213 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003214 int rc;
3215
Jeff Layton0b688cf2012-09-18 16:20:34 -07003216 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003217 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003218 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
3219 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003220 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003221 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003222 task_io_account_read(rdata->got_bytes);
3223 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003224 break;
3225 case MID_REQUEST_SUBMITTED:
3226 case MID_RETRY_NEEDED:
3227 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003228 if (server->sign && rdata->got_bytes)
3229 /* reset bytes number since we can not check a sign */
3230 rdata->got_bytes = 0;
3231 /* FIXME: should this be counted toward the initiating task? */
3232 task_io_account_read(rdata->got_bytes);
3233 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003234 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003235 case MID_RESPONSE_MALFORMED:
3236 credits_received = le16_to_cpu(shdr->CreditRequest);
3237 /* fall through */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003238 default:
Pavel Shilovsky6b15eb12019-01-18 15:46:14 -08003239 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003240 }
Long Libd3dcc62017-11-22 17:38:47 -07003241#ifdef CONFIG_CIFS_SMB_DIRECT
3242 /*
3243 * If this rdata has a memmory registered, the MR can be freed
3244 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3245 * because they have limited number and are used for future I/Os
3246 */
3247 if (rdata->mr) {
3248 smbd_deregister_mr(rdata->mr);
3249 rdata->mr = NULL;
3250 }
3251#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08003252 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003253 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003254 trace_smb3_read_err(0 /* xid */,
3255 rdata->cfile->fid.persistent_fid,
3256 tcon->tid, tcon->ses->Suid, rdata->offset,
3257 rdata->bytes, rdata->result);
3258 } else
3259 trace_smb3_read_done(0 /* xid */,
3260 rdata->cfile->fid.persistent_fid,
3261 tcon->tid, tcon->ses->Suid,
3262 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003263
3264 queue_work(cifsiod_wq, &rdata->work);
3265 DeleteMidQEntry(mid);
3266 add_credits(server, credits_received, 0);
3267}
3268
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003269/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003270int
3271smb2_async_readv(struct cifs_readdata *rdata)
3272{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003273 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003274 char *buf;
3275 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003276 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003277 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003278 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003279 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003280 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003281
Joe Perchesf96637b2013-05-04 22:12:25 -05003282 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3283 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003284
3285 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3286 io_parms.offset = rdata->offset;
3287 io_parms.length = rdata->bytes;
3288 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3289 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3290 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003291
3292 server = io_parms.tcon->ses->server;
3293
Long Li2dabfd52017-11-07 01:54:53 -07003294 rc = smb2_new_read_req(
3295 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003296 if (rc) {
3297 if (rc == -EAGAIN && rdata->credits) {
3298 /* credits was reset by reconnect */
3299 rdata->credits = 0;
3300 /* reduce in_flight value since we won't send the req */
3301 spin_lock(&server->req_lock);
3302 server->in_flight--;
3303 spin_unlock(&server->req_lock);
3304 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003305 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003306 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003307
Steve French5a77e752018-05-09 17:43:08 -05003308 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003309 flags |= CIFS_TRANSFORM_REQ;
3310
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003311 rdata->iov[0].iov_base = buf;
3312 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003313
3314 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003315
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003316 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003317 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003318 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003319 shdr->CreditRequest =
3320 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003321 spin_lock(&server->req_lock);
3322 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003323 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003324 spin_unlock(&server->req_lock);
3325 wake_up(&server->request_q);
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003326 rdata->credits = le16_to_cpu(shdr->CreditCharge);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003327 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003328 }
3329
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003330 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07003331 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003332 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003333 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003334 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003335 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003336 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003337 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3338 io_parms.tcon->tid,
3339 io_parms.tcon->ses->Suid,
3340 io_parms.offset, io_parms.length, rc);
3341 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003342
3343 cifs_small_buf_release(buf);
3344 return rc;
3345}
Pavel Shilovsky33319142012-09-18 16:20:29 -07003346
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003347int
3348SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3349 unsigned int *nbytes, char **buf, int *buf_type)
3350{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003351 struct smb_rqst rqst;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003352 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003353 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003354 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003355 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003356 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003357 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003358 int flags = CIFS_LOG_ERROR;
3359 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003360
3361 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07003362 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003363 if (rc)
3364 return rc;
3365
Steve French5a77e752018-05-09 17:43:08 -05003366 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003367 flags |= CIFS_TRANSFORM_REQ;
3368
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003369 iov[0].iov_base = (char *)req;
3370 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003371
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003372 memset(&rqst, 0, sizeof(struct smb_rqst));
3373 rqst.rq_iov = iov;
3374 rqst.rq_nvec = 1;
3375
3376 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003377 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003378
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003379 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003380
3381 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003382 if (rc != -ENODATA) {
3383 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3384 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003385 trace_smb3_read_err(xid, req->PersistentFileId,
3386 io_parms->tcon->tid, ses->Suid,
3387 io_parms->offset, io_parms->length,
3388 rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003389 }
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003390 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3391 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05003392 } else
3393 trace_smb3_read_done(xid, req->PersistentFileId,
3394 io_parms->tcon->tid, ses->Suid,
3395 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003396
Ronnie Sahlberga821df32017-11-21 09:36:33 +11003397 *nbytes = le32_to_cpu(rsp->DataLength);
3398 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3399 (*nbytes > io_parms->length)) {
3400 cifs_dbg(FYI, "bad length %d for count %d\n",
3401 *nbytes, io_parms->length);
3402 rc = -EIO;
3403 *nbytes = 0;
3404 }
3405
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003406 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003407 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003408 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003409 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003410 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003411 if (resp_buftype == CIFS_SMALL_BUFFER)
3412 *buf_type = CIFS_SMALL_BUFFER;
3413 else if (resp_buftype == CIFS_LARGE_BUFFER)
3414 *buf_type = CIFS_LARGE_BUFFER;
3415 }
3416 return rc;
3417}
3418
Pavel Shilovsky33319142012-09-18 16:20:29 -07003419/*
3420 * Check the mid_state and signature on received buffer (if any), and queue the
3421 * workqueue completion task.
3422 */
3423static void
3424smb2_writev_callback(struct mid_q_entry *mid)
3425{
3426 struct cifs_writedata *wdata = mid->callback_data;
3427 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3428 unsigned int written;
3429 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003430 unsigned int credits_received = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003431
3432 switch (mid->mid_state) {
3433 case MID_RESPONSE_RECEIVED:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003434 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003435 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
3436 if (wdata->result != 0)
3437 break;
3438
3439 written = le32_to_cpu(rsp->DataLength);
3440 /*
3441 * Mask off high 16 bits when bytes written as returned
3442 * by the server is greater than bytes requested by the
3443 * client. OS/2 servers are known to set incorrect
3444 * CountHigh values.
3445 */
3446 if (written > wdata->bytes)
3447 written &= 0xFFFF;
3448
3449 if (written < wdata->bytes)
3450 wdata->result = -ENOSPC;
3451 else
3452 wdata->bytes = written;
3453 break;
3454 case MID_REQUEST_SUBMITTED:
3455 case MID_RETRY_NEEDED:
3456 wdata->result = -EAGAIN;
3457 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003458 case MID_RESPONSE_MALFORMED:
3459 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3460 /* fall through */
Pavel Shilovsky33319142012-09-18 16:20:29 -07003461 default:
3462 wdata->result = -EIO;
3463 break;
3464 }
Long Lidb223a52017-11-22 17:38:45 -07003465#ifdef CONFIG_CIFS_SMB_DIRECT
3466 /*
3467 * If this wdata has a memory registered, the MR can be freed
3468 * The number of MRs available is limited, it's important to recover
3469 * used MR as soon as I/O is finished. Hold MR longer in the later
3470 * I/O process can possibly result in I/O deadlock due to lack of MR
3471 * to send request on I/O retry
3472 */
3473 if (wdata->mr) {
3474 smbd_deregister_mr(wdata->mr);
3475 wdata->mr = NULL;
3476 }
3477#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003478 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003479 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003480 trace_smb3_write_err(0 /* no xid */,
3481 wdata->cfile->fid.persistent_fid,
3482 tcon->tid, tcon->ses->Suid, wdata->offset,
3483 wdata->bytes, wdata->result);
3484 } else
3485 trace_smb3_write_done(0 /* no xid */,
3486 wdata->cfile->fid.persistent_fid,
3487 tcon->tid, tcon->ses->Suid,
3488 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003489
3490 queue_work(cifsiod_wq, &wdata->work);
3491 DeleteMidQEntry(mid);
3492 add_credits(tcon->ses->server, credits_received, 0);
3493}
3494
3495/* smb2_async_writev - send an async write, and set up mid to handle result */
3496int
Steve French4a5c80d2014-02-07 20:45:12 -06003497smb2_async_writev(struct cifs_writedata *wdata,
3498 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07003499{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003500 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003501 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003502 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003503 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003504 struct TCP_Server_Info *server = tcon->ses->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003505 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003506 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003507 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003508
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003509 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003510 if (rc) {
3511 if (rc == -EAGAIN && wdata->credits) {
3512 /* credits was reset by reconnect */
3513 wdata->credits = 0;
3514 /* reduce in_flight value since we won't send the req */
3515 spin_lock(&server->req_lock);
3516 server->in_flight--;
3517 spin_unlock(&server->req_lock);
3518 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003519 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003520 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003521
Steve French5a77e752018-05-09 17:43:08 -05003522 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003523 flags |= CIFS_TRANSFORM_REQ;
3524
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003525 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003526 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003527
3528 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3529 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3530 req->WriteChannelInfoOffset = 0;
3531 req->WriteChannelInfoLength = 0;
3532 req->Channel = 0;
3533 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003534 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003535 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07003536 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07003537#ifdef CONFIG_CIFS_SMB_DIRECT
3538 /*
3539 * If we want to do a server RDMA read, fill in and append
3540 * smbd_buffer_descriptor_v1 to the end of write request
3541 */
Long Libb4c0412018-04-17 12:17:08 -07003542 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07003543 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07003544
Long Lidb223a52017-11-22 17:38:45 -07003545 struct smbd_buffer_descriptor_v1 *v1;
3546 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3547
3548 wdata->mr = smbd_register_mr(
3549 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003550 wdata->nr_pages, wdata->page_offset,
3551 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07003552 if (!wdata->mr) {
3553 rc = -ENOBUFS;
3554 goto async_writev_out;
3555 }
3556 req->Length = 0;
3557 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07003558 if (wdata->nr_pages > 1)
3559 req->RemainingBytes =
3560 cpu_to_le32(
3561 (wdata->nr_pages - 1) * wdata->pagesz -
3562 wdata->page_offset + wdata->tailsz
3563 );
3564 else
3565 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07003566 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3567 if (need_invalidate)
3568 req->Channel = SMB2_CHANNEL_RDMA_V1;
3569 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003570 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07003571 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003572 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07003573 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003574 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3575 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3576 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07003577 }
3578#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003579 iov[0].iov_len = total_len - 1;
3580 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07003581
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003582 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003583 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003584 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07003585 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07003586 rqst.rq_npages = wdata->nr_pages;
3587 rqst.rq_pagesz = wdata->pagesz;
3588 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07003589#ifdef CONFIG_CIFS_SMB_DIRECT
3590 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003591 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07003592 rqst.rq_npages = 0;
3593 }
3594#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05003595 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3596 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003597
Long Lidb223a52017-11-22 17:38:45 -07003598#ifdef CONFIG_CIFS_SMB_DIRECT
3599 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3600 if (!wdata->mr)
3601 req->Length = cpu_to_le32(wdata->bytes);
3602#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07003603 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07003604#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07003605
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003606 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003607 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003608 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003609 shdr->CreditRequest =
3610 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003611 spin_lock(&server->req_lock);
3612 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003613 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003614 spin_unlock(&server->req_lock);
3615 wake_up(&server->request_q);
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003616 wdata->credits = le16_to_cpu(shdr->CreditCharge);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003617 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003618 }
3619
Pavel Shilovsky33319142012-09-18 16:20:29 -07003620 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003621 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3622 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003623
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003624 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003625 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3626 tcon->tid, tcon->ses->Suid, wdata->offset,
3627 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06003628 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003629 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003630 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07003631
Pavel Shilovsky33319142012-09-18 16:20:29 -07003632async_writev_out:
3633 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07003634 return rc;
3635}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003636
3637/*
3638 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3639 * The length field from io_parms must be at least 1 and indicates a number of
3640 * elements with data to write that begins with position 1 in iov array. All
3641 * data length is specified by count.
3642 */
3643int
3644SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3645 unsigned int *nbytes, struct kvec *iov, int n_vec)
3646{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003647 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003648 int rc = 0;
3649 struct smb2_write_req *req = NULL;
3650 struct smb2_write_rsp *rsp = NULL;
3651 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003652 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003653 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003654 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003655
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003656 *nbytes = 0;
3657
3658 if (n_vec < 1)
3659 return rc;
3660
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003661 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3662 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003663 if (rc)
3664 return rc;
3665
3666 if (io_parms->tcon->ses->server == NULL)
3667 return -ECONNABORTED;
3668
Steve French5a77e752018-05-09 17:43:08 -05003669 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003670 flags |= CIFS_TRANSFORM_REQ;
3671
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003672 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003673
3674 req->PersistentFileId = io_parms->persistent_fid;
3675 req->VolatileFileId = io_parms->volatile_fid;
3676 req->WriteChannelInfoOffset = 0;
3677 req->WriteChannelInfoLength = 0;
3678 req->Channel = 0;
3679 req->Length = cpu_to_le32(io_parms->length);
3680 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003681 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003682 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003683 req->RemainingBytes = 0;
3684
3685 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003686 /* 1 for Buffer */
3687 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003688
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003689 memset(&rqst, 0, sizeof(struct smb_rqst));
3690 rqst.rq_iov = iov;
3691 rqst.rq_nvec = n_vec + 1;
3692
3693 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11003694 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003695 cifs_small_buf_release(req);
3696 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003697
3698 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05003699 trace_smb3_write_err(xid, req->PersistentFileId,
3700 io_parms->tcon->tid,
3701 io_parms->tcon->ses->Suid,
3702 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003703 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003704 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05003705 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003706 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05003707 trace_smb3_write_done(xid, req->PersistentFileId,
3708 io_parms->tcon->tid,
3709 io_parms->tcon->ses->Suid,
3710 io_parms->offset, *nbytes);
3711 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003712
3713 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003714 return rc;
3715}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003716
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003717static unsigned int
3718num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3719{
3720 int len;
3721 unsigned int entrycount = 0;
3722 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03003723 char *entryptr;
3724 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003725
3726 if (bufstart == NULL)
3727 return 0;
3728
Dan Carpenter56446f22018-09-06 12:48:22 +03003729 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003730
3731 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03003732 if (entryptr + next_offset < entryptr ||
3733 entryptr + next_offset > end_of_buf ||
3734 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003735 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003736 break;
3737 }
3738
Dan Carpenter56446f22018-09-06 12:48:22 +03003739 entryptr = entryptr + next_offset;
3740 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
3741
3742 len = le32_to_cpu(dir_info->FileNameLength);
3743 if (entryptr + len < entryptr ||
3744 entryptr + len > end_of_buf ||
3745 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003746 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3747 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003748 break;
3749 }
3750
Dan Carpenter56446f22018-09-06 12:48:22 +03003751 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003752 entrycount++;
3753
Dan Carpenter56446f22018-09-06 12:48:22 +03003754 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003755 if (!next_offset)
3756 break;
3757 }
3758
3759 return entrycount;
3760}
3761
3762/*
3763 * Readdir/FindFirst
3764 */
3765int
3766SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3767 u64 persistent_fid, u64 volatile_fid, int index,
3768 struct cifs_search_info *srch_inf)
3769{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003770 struct smb_rqst rqst;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003771 struct smb2_query_directory_req *req;
3772 struct smb2_query_directory_rsp *rsp = NULL;
3773 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003774 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003775 int rc = 0;
3776 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003777 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003778 unsigned char *bufptr;
3779 struct TCP_Server_Info *server;
3780 struct cifs_ses *ses = tcon->ses;
3781 __le16 asteriks = cpu_to_le16('*');
3782 char *end_of_smb;
3783 unsigned int output_size = CIFSMaxBufSize;
3784 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003785 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003786 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003787
3788 if (ses && (ses->server))
3789 server = ses->server;
3790 else
3791 return -EIO;
3792
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003793 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3794 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003795 if (rc)
3796 return rc;
3797
Steve French5a77e752018-05-09 17:43:08 -05003798 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003799 flags |= CIFS_TRANSFORM_REQ;
3800
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003801 switch (srch_inf->info_level) {
3802 case SMB_FIND_FILE_DIRECTORY_INFO:
3803 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3804 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3805 break;
3806 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3807 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3808 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3809 break;
3810 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003811 cifs_dbg(VFS, "info level %u isn't supported\n",
3812 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003813 rc = -EINVAL;
3814 goto qdir_exit;
3815 }
3816
3817 req->FileIndex = cpu_to_le32(index);
3818 req->PersistentFileId = persistent_fid;
3819 req->VolatileFileId = volatile_fid;
3820
3821 len = 0x2;
3822 bufptr = req->Buffer;
3823 memcpy(bufptr, &asteriks, len);
3824
3825 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003826 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003827 req->FileNameLength = cpu_to_le16(len);
3828 /*
3829 * BB could be 30 bytes or so longer if we used SMB2 specific
3830 * buffer lengths, but this is safe and close enough.
3831 */
3832 output_size = min_t(unsigned int, output_size, server->maxBuf);
3833 output_size = min_t(unsigned int, output_size, 2 << 15);
3834 req->OutputBufferLength = cpu_to_le32(output_size);
3835
3836 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003837 /* 1 for Buffer */
3838 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003839
3840 iov[1].iov_base = (char *)(req->Buffer);
3841 iov[1].iov_len = len;
3842
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003843 memset(&rqst, 0, sizeof(struct smb_rqst));
3844 rqst.rq_iov = iov;
3845 rqst.rq_nvec = 2;
3846
3847 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003848 cifs_small_buf_release(req);
3849 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003850
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003851 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003852 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003853 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003854 srch_inf->endOfSearch = true;
3855 rc = 0;
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08003856 } else
3857 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003858 goto qdir_exit;
3859 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003860
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003861 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
3862 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
3863 info_buf_size);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003864 if (rc)
3865 goto qdir_exit;
3866
3867 srch_inf->unicode = true;
3868
3869 if (srch_inf->ntwrk_buf_start) {
3870 if (srch_inf->smallBuf)
3871 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3872 else
3873 cifs_buf_release(srch_inf->ntwrk_buf_start);
3874 }
3875 srch_inf->ntwrk_buf_start = (char *)rsp;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003876 srch_inf->srch_entries_start = srch_inf->last_entry =
3877 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
3878 end_of_smb = rsp_iov.iov_len + (char *)rsp;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003879 srch_inf->entries_in_buffer =
3880 num_entries(srch_inf->srch_entries_start, end_of_smb,
3881 &srch_inf->last_entry, info_buf_size);
3882 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003883 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3884 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3885 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003886 if (resp_buftype == CIFS_LARGE_BUFFER)
3887 srch_inf->smallBuf = false;
3888 else if (resp_buftype == CIFS_SMALL_BUFFER)
3889 srch_inf->smallBuf = true;
3890 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003891 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003892
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003893 return rc;
3894
3895qdir_exit:
3896 free_rsp_buf(resp_buftype, rsp);
3897 return rc;
3898}
3899
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003900int
3901SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003902 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003903 u8 info_type, u32 additional_info,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003904 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003905{
3906 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003907 struct kvec *iov = rqst->rq_iov;
3908 unsigned int i, total_len;
3909 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003910
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003911 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003912 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003913 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003914
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003915 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003916 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003917 req->FileInfoClass = info_class;
3918 req->PersistentFileId = persistent_fid;
3919 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003920 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003921
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003922 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003923 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003924 req->BufferLength = cpu_to_le32(*size);
3925
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003926 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003927 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003928
3929 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003930 /* 1 for Buffer */
3931 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003932
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003933 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003934 le32_add_cpu(&req->BufferLength, size[i]);
3935 iov[i].iov_base = (char *)data[i];
3936 iov[i].iov_len = size[i];
3937 }
3938
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003939 return 0;
3940}
3941
3942void
3943SMB2_set_info_free(struct smb_rqst *rqst)
3944{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003945 if (rqst && rqst->rq_iov)
3946 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003947}
3948
3949static int
3950send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3951 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3952 u8 info_type, u32 additional_info, unsigned int num,
3953 void **data, unsigned int *size)
3954{
3955 struct smb_rqst rqst;
3956 struct smb2_set_info_rsp *rsp = NULL;
3957 struct kvec *iov;
3958 struct kvec rsp_iov;
3959 int rc = 0;
3960 int resp_buftype;
3961 struct cifs_ses *ses = tcon->ses;
3962 int flags = 0;
3963
3964 if (!ses || !(ses->server))
3965 return -EIO;
3966
3967 if (!num)
3968 return -EINVAL;
3969
3970 if (smb3_encryption_required(tcon))
3971 flags |= CIFS_TRANSFORM_REQ;
3972
3973 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
3974 if (!iov)
3975 return -ENOMEM;
3976
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003977 memset(&rqst, 0, sizeof(struct smb_rqst));
3978 rqst.rq_iov = iov;
3979 rqst.rq_nvec = num;
3980
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003981 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
3982 info_class, info_type, additional_info,
3983 data, size);
3984 if (rc) {
3985 kfree(iov);
3986 return rc;
3987 }
3988
3989
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003990 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003991 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10003992 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003993 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003994
Steve Frencheccb4422018-05-17 21:16:55 -05003995 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003996 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003997 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
3998 ses->Suid, info_class, (__u32)info_type, rc);
3999 }
Steve French7d3fb242013-11-18 09:56:28 -06004000
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004001 free_rsp_buf(resp_buftype, rsp);
4002 kfree(iov);
4003 return rc;
4004}
4005
4006int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004007SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004008 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004009{
4010 struct smb2_file_eof_info info;
4011 void *data;
4012 unsigned int size;
4013
4014 info.EndOfFile = *eof;
4015
4016 data = &info;
4017 size = sizeof(struct smb2_file_eof_info);
4018
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004019 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004020 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4021 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004022}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004023
4024int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004025SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4026 u64 persistent_fid, u64 volatile_fid,
4027 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4028{
4029 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4030 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4031 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004032}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004033
4034int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004035SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4036 u64 persistent_fid, u64 volatile_fid,
4037 struct smb2_file_full_ea_info *buf, int len)
4038{
4039 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4040 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4041 0, 1, (void **)&buf, &len);
4042}
4043
4044int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004045SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4046 const u64 persistent_fid, const u64 volatile_fid,
4047 __u8 oplock_level)
4048{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004049 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004050 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10004051 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004052 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004053 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004054 unsigned int total_len;
4055 struct kvec iov[1];
4056 struct kvec rsp_iov;
4057 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004058
Joe Perchesf96637b2013-05-04 22:12:25 -05004059 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004060 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4061 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004062 if (rc)
4063 return rc;
4064
Steve French5a77e752018-05-09 17:43:08 -05004065 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004066 flags |= CIFS_TRANSFORM_REQ;
4067
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004068 req->VolatileFid = volatile_fid;
4069 req->PersistentFid = persistent_fid;
4070 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004071 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004072
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004073 flags |= CIFS_NO_RESP;
4074
4075 iov[0].iov_base = (char *)req;
4076 iov[0].iov_len = total_len;
4077
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004078 memset(&rqst, 0, sizeof(struct smb_rqst));
4079 rqst.rq_iov = iov;
4080 rqst.rq_nvec = 1;
4081
4082 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004083 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004084
4085 if (rc) {
4086 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004087 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004088 }
4089
4090 return rc;
4091}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004092
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004093void
4094smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4095 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004096{
4097 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4098 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4099 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05304100 kst->f_bfree = kst->f_bavail =
4101 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004102 return;
4103}
4104
Steve French2d304212018-06-24 23:28:12 -05004105static void
4106copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4107 struct kstatfs *kst)
4108{
4109 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4110 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4111 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4112 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4113 kst->f_bavail = kst->f_bfree;
4114 else
4115 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4116 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4117 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4118 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4119 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4120
4121 return;
4122}
Steve French2d304212018-06-24 23:28:12 -05004123
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004124static int
4125build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4126 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4127{
4128 int rc;
4129 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004130 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004131
Joe Perchesf96637b2013-05-04 22:12:25 -05004132 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004133
4134 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4135 return -EIO;
4136
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004137 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4138 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004139 if (rc)
4140 return rc;
4141
4142 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4143 req->FileInfoClass = level;
4144 req->PersistentFileId = persistent_fid;
4145 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004146 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004147 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004148 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004149 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004150 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004151
4152 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11004153 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004154 return 0;
4155}
4156
Steve French2d304212018-06-24 23:28:12 -05004157int
4158SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4159 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4160{
4161 struct smb_rqst rqst;
4162 struct smb2_query_info_rsp *rsp = NULL;
4163 struct kvec iov;
4164 struct kvec rsp_iov;
4165 int rc = 0;
4166 int resp_buftype;
4167 struct cifs_ses *ses = tcon->ses;
4168 FILE_SYSTEM_POSIX_INFO *info = NULL;
4169 int flags = 0;
4170
4171 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4172 sizeof(FILE_SYSTEM_POSIX_INFO),
4173 persistent_fid, volatile_fid);
4174 if (rc)
4175 return rc;
4176
4177 if (smb3_encryption_required(tcon))
4178 flags |= CIFS_TRANSFORM_REQ;
4179
4180 memset(&rqst, 0, sizeof(struct smb_rqst));
4181 rqst.rq_iov = &iov;
4182 rqst.rq_nvec = 1;
4183
4184 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4185 cifs_small_buf_release(iov.iov_base);
4186 if (rc) {
4187 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4188 goto posix_qfsinf_exit;
4189 }
4190 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4191
4192 info = (FILE_SYSTEM_POSIX_INFO *)(
4193 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004194 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4195 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4196 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05004197 if (!rc)
4198 copy_posix_fs_info_to_kstatfs(info, fsdata);
4199
4200posix_qfsinf_exit:
4201 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4202 return rc;
4203}
Steve French2d304212018-06-24 23:28:12 -05004204
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004205int
4206SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4207 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4208{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004209 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004210 struct smb2_query_info_rsp *rsp = NULL;
4211 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004212 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004213 int rc = 0;
4214 int resp_buftype;
4215 struct cifs_ses *ses = tcon->ses;
4216 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004217 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004218
4219 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4220 sizeof(struct smb2_fs_full_size_info),
4221 persistent_fid, volatile_fid);
4222 if (rc)
4223 return rc;
4224
Steve French5a77e752018-05-09 17:43:08 -05004225 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004226 flags |= CIFS_TRANSFORM_REQ;
4227
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004228 memset(&rqst, 0, sizeof(struct smb_rqst));
4229 rqst.rq_iov = &iov;
4230 rqst.rq_nvec = 1;
4231
4232 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004233 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004234 if (rc) {
4235 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05004236 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004237 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004238 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004239
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004240 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004241 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004242 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4243 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4244 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004245 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004246 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004247
Steve French34f62642013-10-09 02:07:00 -05004248qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004249 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004250 return rc;
4251}
4252
4253int
4254SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05004255 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05004256{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004257 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05004258 struct smb2_query_info_rsp *rsp = NULL;
4259 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004260 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05004261 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05004262 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05004263 struct cifs_ses *ses = tcon->ses;
4264 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004265 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05004266
Steven French21671142013-10-09 13:36:35 -05004267 if (level == FS_DEVICE_INFORMATION) {
4268 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4269 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4270 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4271 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4272 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004273 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4274 max_len = sizeof(struct smb3_fs_ss_info);
4275 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05004276 } else if (level == FS_VOLUME_INFORMATION) {
4277 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4278 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05004279 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004280 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05004281 return -EINVAL;
4282 }
4283
4284 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05004285 persistent_fid, volatile_fid);
4286 if (rc)
4287 return rc;
4288
Steve French5a77e752018-05-09 17:43:08 -05004289 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004290 flags |= CIFS_TRANSFORM_REQ;
4291
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004292 memset(&rqst, 0, sizeof(struct smb_rqst));
4293 rqst.rq_iov = &iov;
4294 rqst.rq_nvec = 1;
4295
4296 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004297 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05004298 if (rc) {
4299 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4300 goto qfsattr_exit;
4301 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004302 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05004303
4304 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4305 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004306 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05004307 if (rc)
4308 goto qfsattr_exit;
4309
4310 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004311 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004312 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05004313 rsp_len, max_len));
4314 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004315 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004316 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004317 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4318 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004319 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004320 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4321 tcon->perf_sector_size =
4322 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05004323 } else if (level == FS_VOLUME_INFORMATION) {
4324 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4325 (offset + (char *)rsp);
4326 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4327 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004328 }
Steve French34f62642013-10-09 02:07:00 -05004329
4330qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004331 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004332 return rc;
4333}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004334
4335int
4336smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4337 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4338 const __u32 num_lock, struct smb2_lock_element *buf)
4339{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004340 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004341 int rc = 0;
4342 struct smb2_lock_req *req = NULL;
4343 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004344 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004345 int resp_buf_type;
4346 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004347 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004348 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004349
Joe Perchesf96637b2013-05-04 22:12:25 -05004350 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004351
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004352 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004353 if (rc)
4354 return rc;
4355
Steve French5a77e752018-05-09 17:43:08 -05004356 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004357 flags |= CIFS_TRANSFORM_REQ;
4358
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004359 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004360 req->LockCount = cpu_to_le16(num_lock);
4361
4362 req->PersistentFileId = persist_fid;
4363 req->VolatileFileId = volatile_fid;
4364
4365 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004366
4367 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004368 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004369 iov[1].iov_base = (char *)buf;
4370 iov[1].iov_len = count;
4371
4372 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004373
4374 memset(&rqst, 0, sizeof(struct smb_rqst));
4375 rqst.rq_iov = iov;
4376 rqst.rq_nvec = 2;
4377
4378 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11004379 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004380 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004381 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004382 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004383 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004384 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4385 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004386 }
4387
4388 return rc;
4389}
4390
4391int
4392SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4393 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4394 const __u64 length, const __u64 offset, const __u32 lock_flags,
4395 const bool wait)
4396{
4397 struct smb2_lock_element lock;
4398
4399 lock.Offset = cpu_to_le64(offset);
4400 lock.Length = cpu_to_le64(length);
4401 lock.Flags = cpu_to_le32(lock_flags);
4402 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4403 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4404
4405 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4406}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004407
4408int
4409SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4410 __u8 *lease_key, const __le32 lease_state)
4411{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004412 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004413 int rc;
4414 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004415 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004416 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004417 unsigned int total_len;
4418 struct kvec iov[1];
4419 struct kvec rsp_iov;
4420 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05004421 __u64 *please_key_high;
4422 __u64 *please_key_low;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004423
Joe Perchesf96637b2013-05-04 22:12:25 -05004424 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004425 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4426 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004427 if (rc)
4428 return rc;
4429
Steve French5a77e752018-05-09 17:43:08 -05004430 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004431 flags |= CIFS_TRANSFORM_REQ;
4432
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004433 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004434 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004435 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004436
4437 memcpy(req->LeaseKey, lease_key, 16);
4438 req->LeaseState = lease_state;
4439
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11004440 flags |= CIFS_NO_RESP;
4441
4442 iov[0].iov_base = (char *)req;
4443 iov[0].iov_len = total_len;
4444
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004445 memset(&rqst, 0, sizeof(struct smb_rqst));
4446 rqst.rq_iov = iov;
4447 rqst.rq_nvec = 1;
4448
4449 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004450 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004451
Aurelien Apteld339adc2019-01-31 13:46:07 +01004452 please_key_low = (__u64 *)lease_key;
4453 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004454 if (rc) {
4455 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05004456 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4457 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05004458 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05004459 } else
4460 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4461 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07004462
4463 return rc;
4464}