blob: c6f8bc6729aa10e85f78923ac2cdd4c59fc6e309 [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{
Steve Frenchedb16132020-05-31 14:36:56 -050088 if (!tcon || !tcon->ses)
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080089 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,
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500101 const struct cifs_tcon *tcon,
102 struct TCP_Server_Info *server)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400103{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700104 shdr->ProtocolId = SMB2_PROTO_NUMBER;
105 shdr->StructureSize = cpu_to_le16(64);
106 shdr->Command = smb2_cmd;
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500107 if (server) {
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100108 spin_lock(&server->req_lock);
Steve French69dc4b12019-03-05 21:04:56 -0600109 /* Request up to 10 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500110 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700111 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100112 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700113 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500114 min_t(int, server->max_credits -
Steve French69dc4b12019-03-05 21:04:56 -0600115 server->credits, 10));
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100116 spin_unlock(&server->req_lock);
117 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700118 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100119 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700120 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400121
122 if (!tcon)
123 goto out;
124
Steve French2b80d042013-06-23 18:43:37 -0500125 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
126 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500127 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700128 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500129 /* else CreditCharge MBZ */
130
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700131 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400132 /* Uid is not converted */
133 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700134 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500135
136 /*
137 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
138 * to pass the path on the Open SMB prefixed by \\server\share.
139 * Not sure when we would need to do the augmented path (if ever) and
140 * setting this flag breaks the SMB2 open operation since it is
141 * illegal to send an empty path name (without \\server\share prefix)
142 * when the DFS flag is set in the SMB open header. We could
143 * consider setting the flag on all operations other than open
144 * but it is safer to net set it for now.
145 */
146/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700147 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500148
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500149 if (server && server->sign && !smb3_encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700150 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400151out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400152 return;
153}
154
155static int
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500156smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
157 struct TCP_Server_Info *server)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400158{
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300159 int rc;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400160 struct nls_table *nls_codepage;
161 struct cifs_ses *ses;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200162 int retries;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400163
164 /*
165 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
166 * check for tcp and smb session status done differently
167 * for those three - in the calling routine.
168 */
169 if (tcon == NULL)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300170 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400171
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300172 if (smb2_command == SMB2_TREE_CONNECT)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300173 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400174
175 if (tcon->tidStatus == CifsExiting) {
176 /*
177 * only tree disconnect, open, and write,
178 * (and ulogoff which does not have tcon)
179 * are allowed as we start force umount.
180 */
181 if ((smb2_command != SMB2_WRITE) &&
182 (smb2_command != SMB2_CREATE) &&
183 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500184 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
185 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400186 return -ENODEV;
187 }
188 }
189 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500190 (!tcon->ses->server) || !server)
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400191 return -EIO;
192
193 ses = tcon->ses;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200194 retries = server->nr_targets;
195
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400196 /*
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200197 * Give demultiplex thread up to 10 seconds to each target available for
198 * reconnect -- should be greater than cifs socket timeout which is 7
199 * seconds.
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400200 */
201 while (server->tcpStatus == CifsNeedReconnect) {
202 /*
203 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
204 * here since they are implicitly done when session drops.
205 */
206 switch (smb2_command) {
207 /*
208 * BB Should we keep oplock break and add flush to exceptions?
209 */
210 case SMB2_TREE_DISCONNECT:
211 case SMB2_CANCEL:
212 case SMB2_CLOSE:
213 case SMB2_OPLOCK_BREAK:
214 return -EAGAIN;
215 }
216
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300217 rc = wait_event_interruptible_timeout(server->response_q,
218 (server->tcpStatus != CifsNeedReconnect),
219 10 * HZ);
220 if (rc < 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700221 cifs_dbg(FYI, "%s: aborting reconnect due to a received signal by the process\n",
222 __func__);
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300223 return -ERESTARTSYS;
224 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400225
226 /* are we still trying to reconnect? */
227 if (server->tcpStatus != CifsNeedReconnect)
228 break;
229
Ronnie Sahlbergc54849d2020-01-31 05:52:51 +1000230 if (retries && --retries)
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200231 continue;
232
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400233 /*
234 * on "soft" mounts we wait once. Hard mounts keep
235 * retrying until process is killed or server comes
236 * back on-line
237 */
238 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500239 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400240 return -EHOSTDOWN;
241 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200242 retries = server->nr_targets;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400243 }
244
245 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300246 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400247
248 nls_codepage = load_nls_default();
249
250 /*
251 * need to prevent multiple threads trying to simultaneously reconnect
252 * the same SMB session
253 */
254 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200255
256 /*
257 * Recheck after acquire mutex. If another thread is negotiating
258 * and the server never sends an answer the socket will be closed
259 * and tcpStatus set to reconnect.
260 */
261 if (server->tcpStatus == CifsNeedReconnect) {
262 rc = -EHOSTDOWN;
263 mutex_unlock(&tcon->ses->session_mutex);
264 goto out;
265 }
266
Aurelien Aptel2f589672020-04-24 16:55:31 +0200267 /*
268 * If we are reconnecting an extra channel, bind
269 */
270 if (server->is_channel) {
271 ses->binding = true;
272 ses->binding_chan = cifs_ses_find_chan(ses, server);
273 }
274
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400275 rc = cifs_negotiate_protocol(0, tcon->ses);
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000276 if (!rc && tcon->ses->need_reconnect) {
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400277 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000278 if ((rc == -EACCES) && !tcon->retry) {
279 rc = -EHOSTDOWN;
Aurelien Aptel2f589672020-04-24 16:55:31 +0200280 ses->binding = false;
281 ses->binding_chan = NULL;
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000282 mutex_unlock(&tcon->ses->session_mutex);
283 goto failed;
284 }
285 }
Aurelien Aptel2f589672020-04-24 16:55:31 +0200286 /*
287 * End of channel binding
288 */
289 ses->binding = false;
290 ses->binding_chan = NULL;
291
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400292 if (rc || !tcon->need_reconnect) {
293 mutex_unlock(&tcon->ses->session_mutex);
294 goto out;
295 }
296
297 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800298 if (tcon->use_persistent)
299 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500300
Stefan Metzmacher565674d2020-07-21 09:36:38 -0300301 rc = cifs_tree_connect(0, tcon, nls_codepage);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400302 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500303
Joe Perchesf96637b2013-05-04 22:12:25 -0500304 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500305 if (rc) {
306 /* If sess reconnected but tcon didn't, something strange ... */
Joe Perchesa0a30362020-04-14 22:42:53 -0700307 pr_warn_once("reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400308 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500309 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800310
311 if (smb2_command != SMB2_INTERNAL_CMD)
Stefan Metzmacherb08484d2020-02-24 14:14:59 +0100312 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800313
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400314 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400315out:
316 /*
317 * Check if handle based operation so we know whether we can continue
318 * or not without returning to caller to reset file handle.
319 */
320 /*
321 * BB Is flush done by server on drop of tcp session? Should we special
322 * case it and skip above?
323 */
324 switch (smb2_command) {
325 case SMB2_FLUSH:
326 case SMB2_READ:
327 case SMB2_WRITE:
328 case SMB2_LOCK:
329 case SMB2_IOCTL:
330 case SMB2_QUERY_DIRECTORY:
331 case SMB2_CHANGE_NOTIFY:
332 case SMB2_QUERY_INFO:
333 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800334 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400335 }
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000336failed:
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400337 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400338 return rc;
339}
340
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700341static void
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500342fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
343 struct TCP_Server_Info *server,
344 void *buf,
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700345 unsigned int *total_len)
346{
347 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
348 /* lookup word count ie StructureSize from table */
349 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
350
351 /*
352 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
353 * largest operations (Create)
354 */
355 memset(buf, 0, 256);
356
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500357 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon, server);
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700358 spdu->StructureSize2 = cpu_to_le16(parmsize);
359
360 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
361}
362
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100363/*
364 * Allocate and return pointer to an SMB request hdr, and set basic
365 * SMB information in the SMB header. If the return code is zero, this
366 * function must have filled in request_buf pointer.
367 */
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300368static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500369 struct TCP_Server_Info *server,
370 void **request_buf, unsigned int *total_len)
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800371{
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800372 /* BB eventually switch this to SMB2 specific small buf size */
Stefano Briviof46ecbd2018-07-05 11:46:42 +0200373 if (smb2_command == SMB2_SET_INFO)
374 *request_buf = cifs_buf_get();
375 else
376 *request_buf = cifs_small_buf_get();
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800377 if (*request_buf == NULL) {
378 /* BB should we add a retry in here if not a writepage? */
379 return -ENOMEM;
380 }
381
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500382 fill_small_buf(smb2_command, tcon, server,
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100383 (struct smb2_sync_hdr *)(*request_buf),
384 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400385
386 if (tcon != NULL) {
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400387 uint16_t com_code = le16_to_cpu(smb2_command);
388 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400389 cifs_stats_inc(&tcon->num_smbs_sent);
390 }
391
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300392 return 0;
393}
394
395static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500396 struct TCP_Server_Info *server,
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300397 void **request_buf, unsigned int *total_len)
398{
399 int rc;
400
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500401 rc = smb2_reconnect(smb2_command, tcon, server);
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300402 if (rc)
403 return rc;
404
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500405 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300406 total_len);
407}
408
409static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500410 struct TCP_Server_Info *server,
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300411 void **request_buf, unsigned int *total_len)
412{
413 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
414 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500415 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
416 request_buf, total_len);
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300417 }
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500418 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
419 request_buf, total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400420}
421
Steve Frenchd7bef4c2019-04-18 11:03:58 -0500422/* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500423
424static void
425build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
426{
427 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
428 pneg_ctxt->DataLength = cpu_to_le16(38);
429 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
Steve French6489b802020-12-09 22:19:00 -0600430 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_LINUX_CLIENT_SALT_SIZE);
431 get_random_bytes(pneg_ctxt->Salt, SMB311_LINUX_CLIENT_SALT_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500432 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
433}
434
435static void
Steve French26ea8882019-04-26 20:36:08 -0700436build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
437{
438 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
439 pneg_ctxt->DataLength =
440 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
441 - sizeof(struct smb2_neg_context));
442 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
443 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
444 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
445 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
446}
447
448static void
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500449build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
450{
451 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve Frenchfbfd0b42020-09-11 16:19:28 -0500452 if (require_gcm_256) {
453 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
454 pneg_ctxt->CipherCount = cpu_to_le16(1);
455 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
Steve French29e27922020-10-14 20:24:09 -0500456 } else if (enable_gcm_256) {
457 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
458 pneg_ctxt->CipherCount = cpu_to_le16(3);
459 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
460 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
461 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchfbfd0b42020-09-11 16:19:28 -0500462 } else {
463 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
464 pneg_ctxt->CipherCount = cpu_to_le16(2);
465 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
466 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
467 }
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500468}
469
Steve French96d3cca2019-06-25 04:39:51 -0500470static unsigned int
471build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
472{
473 struct nls_table *cp = load_nls_default();
474
475 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
476
477 /* copy up to max of first 100 bytes of server name to NetName field */
Steve Frenchdf58fae2019-08-05 17:07:26 -0500478 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
Steve French96d3cca2019-06-25 04:39:51 -0500479 /* context size is DataLength + minimal smb2_neg_context */
480 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
481 sizeof(struct smb2_neg_context), 8) * 8;
482}
483
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500484static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500485build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
486{
487 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
488 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
Steve French0d481322019-02-24 17:56:33 -0600489 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
490 pneg_ctxt->Name[0] = 0x93;
491 pneg_ctxt->Name[1] = 0xAD;
492 pneg_ctxt->Name[2] = 0x25;
493 pneg_ctxt->Name[3] = 0x50;
494 pneg_ctxt->Name[4] = 0x9C;
495 pneg_ctxt->Name[5] = 0xB4;
496 pneg_ctxt->Name[6] = 0x11;
497 pneg_ctxt->Name[7] = 0xE7;
498 pneg_ctxt->Name[8] = 0xB4;
499 pneg_ctxt->Name[9] = 0x23;
500 pneg_ctxt->Name[10] = 0x83;
501 pneg_ctxt->Name[11] = 0xDE;
502 pneg_ctxt->Name[12] = 0x96;
503 pneg_ctxt->Name[13] = 0x8B;
504 pneg_ctxt->Name[14] = 0xCD;
505 pneg_ctxt->Name[15] = 0x7C;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500506}
507
508static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100509assemble_neg_contexts(struct smb2_negotiate_req *req,
Steve French9fe5ff12019-06-24 20:39:04 -0500510 struct TCP_Server_Info *server, unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500511{
Colin Ian Kinga9f76cf2019-12-02 18:59:42 +0000512 char *pneg_ctxt;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500513 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500514
Steve Frenchd5c70762019-01-03 02:37:21 -0600515 if (*total_len > 200) {
516 /* In case length corrupted don't want to overrun smb buffer */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000517 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
Steve Frenchd5c70762019-01-03 02:37:21 -0600518 return;
519 }
520
521 /*
522 * round up total_len of fixed part of SMB3 negotiate request to 8
523 * byte boundary before adding negotiate contexts
524 */
525 *total_len = roundup(*total_len, 8);
526
527 pneg_ctxt = (*total_len) + (char *)req;
528 req->NegotiateContextOffset = cpu_to_le32(*total_len);
529
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500530 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500531 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
532 *total_len += ctxt_len;
533 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100534
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500535 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500536 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
537 *total_len += ctxt_len;
538 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100539
Steve French9fe5ff12019-06-24 20:39:04 -0500540 if (server->compress_algorithm) {
541 build_compression_ctxt((struct smb2_compression_capabilities_context *)
Steve French26ea8882019-04-26 20:36:08 -0700542 pneg_ctxt);
Steve French9fe5ff12019-06-24 20:39:04 -0500543 ctxt_len = DIV_ROUND_UP(
544 sizeof(struct smb2_compression_capabilities_context),
545 8) * 8;
546 *total_len += ctxt_len;
547 pneg_ctxt += ctxt_len;
Steve French96d3cca2019-06-25 04:39:51 -0500548 req->NegotiateContextCount = cpu_to_le16(5);
Steve French9fe5ff12019-06-24 20:39:04 -0500549 } else
Steve French96d3cca2019-06-25 04:39:51 -0500550 req->NegotiateContextCount = cpu_to_le16(4);
551
552 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
553 server->hostname);
554 *total_len += ctxt_len;
555 pneg_ctxt += ctxt_len;
556
Steve Frenchfcef0db2018-05-19 20:45:27 -0500557 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
558 *total_len += sizeof(struct smb2_posix_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500559}
Steve French5100d8a2018-04-09 10:47:14 -0500560
561static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
562{
563 unsigned int len = le16_to_cpu(ctxt->DataLength);
564
565 /* If invalid preauth context warn but use what we requested, SHA-512 */
566 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700567 pr_warn_once("server sent bad preauth context\n");
Steve French5100d8a2018-04-09 10:47:14 -0500568 return;
Steve French6489b802020-12-09 22:19:00 -0600569 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
570 pr_warn_once("server sent invalid SaltLength\n");
571 return;
Steve French5100d8a2018-04-09 10:47:14 -0500572 }
573 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
Joe Perchesa0a30362020-04-14 22:42:53 -0700574 pr_warn_once("Invalid SMB3 hash algorithm count\n");
Steve French5100d8a2018-04-09 10:47:14 -0500575 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
Joe Perchesa0a30362020-04-14 22:42:53 -0700576 pr_warn_once("unknown SMB3 hash algorithm\n");
Steve French5100d8a2018-04-09 10:47:14 -0500577}
578
Steve French26ea8882019-04-26 20:36:08 -0700579static void decode_compress_ctx(struct TCP_Server_Info *server,
580 struct smb2_compression_capabilities_context *ctxt)
581{
582 unsigned int len = le16_to_cpu(ctxt->DataLength);
583
584 /* sizeof compress context is a one element compression capbility struct */
585 if (len < 10) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700586 pr_warn_once("server sent bad compression cntxt\n");
Steve French26ea8882019-04-26 20:36:08 -0700587 return;
588 }
589 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700590 pr_warn_once("Invalid SMB3 compress algorithm count\n");
Steve French26ea8882019-04-26 20:36:08 -0700591 return;
592 }
593 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700594 pr_warn_once("unknown compression algorithm\n");
Steve French26ea8882019-04-26 20:36:08 -0700595 return;
596 }
597 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
598}
599
Steve French5100d8a2018-04-09 10:47:14 -0500600static int decode_encrypt_ctx(struct TCP_Server_Info *server,
601 struct smb2_encryption_neg_context *ctxt)
602{
603 unsigned int len = le16_to_cpu(ctxt->DataLength);
604
605 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
606 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700607 pr_warn_once("server sent bad crypto ctxt len\n");
Steve French5100d8a2018-04-09 10:47:14 -0500608 return -EINVAL;
609 }
610
611 if (le16_to_cpu(ctxt->CipherCount) != 1) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700612 pr_warn_once("Invalid SMB3.11 cipher count\n");
Steve French5100d8a2018-04-09 10:47:14 -0500613 return -EINVAL;
614 }
615 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
Steve French511ac892020-10-15 00:14:47 -0500616 if (require_gcm_256) {
617 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
618 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
619 return -EOPNOTSUPP;
620 }
621 } else if (ctxt->Ciphers[0] == 0) {
Steve Frenchacf96fe2020-10-17 03:54:27 -0500622 /*
623 * e.g. if server only supported AES256_CCM (very unlikely)
624 * or server supported no encryption types or had all disabled.
625 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
626 * in which mount requested encryption ("seal") checks later
627 * on during tree connection will return proper rc, but if
628 * seal not requested by client, since server is allowed to
629 * return 0 to indicate no supported cipher, we can't fail here
630 */
631 server->cipher_type = 0;
632 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
633 pr_warn_once("Server does not support requested encryption types\n");
634 return 0;
Steve French511ac892020-10-15 00:14:47 -0500635 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
636 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
637 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
638 /* server returned a cipher we didn't ask for */
Joe Perchesa0a30362020-04-14 22:42:53 -0700639 pr_warn_once("Invalid SMB3.11 cipher returned\n");
Steve French5100d8a2018-04-09 10:47:14 -0500640 return -EINVAL;
641 }
642 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500643 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500644 return 0;
645}
646
647static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000648 struct TCP_Server_Info *server,
649 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500650{
651 struct smb2_neg_context *pctx;
652 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
653 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500654 unsigned int len_of_ctxts, i;
655 int rc = 0;
656
657 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
658 if (len_of_smb <= offset) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000659 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
Steve French5100d8a2018-04-09 10:47:14 -0500660 return -EINVAL;
661 }
662
663 len_of_ctxts = len_of_smb - offset;
664
665 for (i = 0; i < ctxt_cnt; i++) {
666 int clen;
667 /* check that offset is not beyond end of SMB */
668 if (len_of_ctxts == 0)
669 break;
670
671 if (len_of_ctxts < sizeof(struct smb2_neg_context))
672 break;
673
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000674 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500675 clen = le16_to_cpu(pctx->DataLength);
676 if (clen > len_of_ctxts)
677 break;
678
679 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
680 decode_preauth_context(
681 (struct smb2_preauth_neg_context *)pctx);
682 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
683 rc = decode_encrypt_ctx(server,
684 (struct smb2_encryption_neg_context *)pctx);
Steve French26ea8882019-04-26 20:36:08 -0700685 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
686 decode_compress_ctx(server,
687 (struct smb2_compression_capabilities_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500688 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
689 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500690 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000691 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
Steve French5100d8a2018-04-09 10:47:14 -0500692 le16_to_cpu(pctx->ContextType));
693
694 if (rc)
695 break;
696 /* offsets must be 8 byte aligned */
697 clen = (clen + 7) & ~0x7;
698 offset += clen + sizeof(struct smb2_neg_context);
699 len_of_ctxts -= clen;
700 }
701 return rc;
702}
703
Steve Frenchce558b02018-05-31 19:16:54 -0500704static struct create_posix *
705create_posix_buf(umode_t mode)
706{
707 struct create_posix *buf;
708
709 buf = kzalloc(sizeof(struct create_posix),
710 GFP_KERNEL);
711 if (!buf)
712 return NULL;
713
714 buf->ccontext.DataOffset =
715 cpu_to_le16(offsetof(struct create_posix, Mode));
716 buf->ccontext.DataLength = cpu_to_le32(4);
717 buf->ccontext.NameOffset =
718 cpu_to_le16(offsetof(struct create_posix, Name));
719 buf->ccontext.NameLength = cpu_to_le16(16);
720
721 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
722 buf->Name[0] = 0x93;
723 buf->Name[1] = 0xAD;
724 buf->Name[2] = 0x25;
725 buf->Name[3] = 0x50;
726 buf->Name[4] = 0x9C;
727 buf->Name[5] = 0xB4;
728 buf->Name[6] = 0x11;
729 buf->Name[7] = 0xE7;
730 buf->Name[8] = 0xB4;
731 buf->Name[9] = 0x23;
732 buf->Name[10] = 0x83;
733 buf->Name[11] = 0xDE;
734 buf->Name[12] = 0x96;
735 buf->Name[13] = 0x8B;
736 buf->Name[14] = 0xCD;
737 buf->Name[15] = 0x7C;
738 buf->Mode = cpu_to_le32(mode);
Joe Perchesa0a30362020-04-14 22:42:53 -0700739 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
Steve Frenchce558b02018-05-31 19:16:54 -0500740 return buf;
741}
742
743static int
744add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
745{
746 struct smb2_create_req *req = iov[0].iov_base;
747 unsigned int num = *num_iovec;
748
749 iov[num].iov_base = create_posix_buf(mode);
Steve Frenchd0959b02019-10-05 10:53:58 -0500750 if (mode == ACL_NO_MODE)
Joe Perchesa0a30362020-04-14 22:42:53 -0700751 cifs_dbg(FYI, "Invalid mode\n");
Steve Frenchce558b02018-05-31 19:16:54 -0500752 if (iov[num].iov_base == NULL)
753 return -ENOMEM;
754 iov[num].iov_len = sizeof(struct create_posix);
755 if (!req->CreateContextsOffset)
756 req->CreateContextsOffset = cpu_to_le32(
757 sizeof(struct smb2_create_req) +
758 iov[num - 1].iov_len);
759 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
760 *num_iovec = num + 1;
761 return 0;
762}
763
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500764
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400765/*
766 *
767 * SMB2 Worker functions follow:
768 *
769 * The general structure of the worker functions is:
770 * 1) Call smb2_init (assembles SMB2 header)
771 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
772 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
773 * 4) Decode SMB2 command specific fields in the fixed length area
774 * 5) Decode variable length data area (if any for this SMB2 command type)
775 * 6) Call free smb buffer
776 * 7) return
777 *
778 */
779
780int
781SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
782{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000783 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400784 struct smb2_negotiate_req *req;
785 struct smb2_negotiate_rsp *rsp;
786 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700787 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400788 int rc = 0;
789 int resp_buftype;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200790 struct TCP_Server_Info *server = cifs_ses_server(ses);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400791 int blob_offset, blob_length;
792 char *security_blob;
793 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100794 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400795
Joe Perchesf96637b2013-05-04 22:12:25 -0500796 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400797
Jeff Layton3534b852013-05-24 07:41:01 -0400798 if (!server) {
799 WARN(1, "%s: server is NULL!\n", __func__);
800 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400801 }
802
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500803 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
804 (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400805 if (rc)
806 return rc;
807
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100808 req->sync_hdr.SessionId = 0;
Steve French0fdfef92018-06-28 19:30:23 -0500809
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100810 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
811 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400812
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200813 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500814 SMB3ANY_VERSION_STRING) == 0) {
815 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
816 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
817 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100818 total_len += 4;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000819 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500820 SMBDEFAULT_VERSION_STRING) == 0) {
821 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
822 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
823 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600824 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
825 req->DialectCount = cpu_to_le16(4);
826 total_len += 8;
Steve French9764c022017-09-17 10:41:35 -0500827 } else {
828 /* otherwise send specific dialect */
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200829 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
Steve French9764c022017-09-17 10:41:35 -0500830 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100831 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500832 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400833
834 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400835 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500836 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400837 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500838 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400839 else
840 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400841
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000842 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400843
Steve French3c5f9be12014-05-13 13:37:45 -0700844 /* ClientGUID must be zero for SMB2.02 dialect */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000845 if (server->vals->protocol_id == SMB20_PROT_ID)
Steve French3c5f9be12014-05-13 13:37:45 -0700846 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500847 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700848 memcpy(req->ClientGUID, server->client_guid,
849 SMB2_CLIENT_GUID_SIZE);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000850 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
851 (strcmp(server->vals->version_string,
Steve Frenchd5c70762019-01-03 02:37:21 -0600852 SMBDEFAULT_VERSION_STRING) == 0))
Steve French9fe5ff12019-06-24 20:39:04 -0500853 assemble_neg_contexts(req, server, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500854 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400855 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100856 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400857
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000858 memset(&rqst, 0, sizeof(struct smb_rqst));
859 rqst.rq_iov = iov;
860 rqst.rq_nvec = 1;
861
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500862 rc = cifs_send_recv(xid, ses, server,
863 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700864 cifs_small_buf_release(req);
865 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400866 /*
867 * No tcon so can't do
868 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
869 */
Steve French7e682f72017-08-31 21:34:24 -0500870 if (rc == -EOPNOTSUPP) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700871 cifs_server_dbg(VFS, "Dialect not supported by server. Consider specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n");
Steve French7e682f72017-08-31 21:34:24 -0500872 goto neg_exit;
873 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400874 goto neg_exit;
875
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000876 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500877 SMB3ANY_VERSION_STRING) == 0) {
878 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000879 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500880 "SMB2 dialect returned but not requested\n");
881 return -EIO;
882 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000883 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500884 "SMB2.1 dialect returned but not requested\n");
885 return -EIO;
886 }
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000887 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500888 SMBDEFAULT_VERSION_STRING) == 0) {
889 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000890 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500891 "SMB2 dialect returned but not requested\n");
892 return -EIO;
893 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
894 /* ops set to 3.0 by default for default so update */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000895 server->ops = &smb21_operations;
896 server->vals = &smb21_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800897 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000898 server->ops = &smb311_operations;
899 server->vals = &smb311_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800900 }
Steve French590d08d2017-09-19 11:43:47 -0500901 } else if (le16_to_cpu(rsp->DialectRevision) !=
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000902 server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500903 /* if requested single dialect ensure returned dialect matched */
Joe Perchesa0a30362020-04-14 22:42:53 -0700904 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
905 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500906 return -EIO;
907 }
908
Joe Perchesf96637b2013-05-04 22:12:25 -0500909 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400910
Steve Frenche4aa25e2012-10-01 12:26:22 -0500911 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500912 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500913 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500914 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500915 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500916 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500917 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
918 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600919 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
920 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400921 else {
Joe Perchesa0a30362020-04-14 22:42:53 -0700922 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
923 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400924 rc = -EIO;
925 goto neg_exit;
926 }
927 server->dialect = le16_to_cpu(rsp->DialectRevision);
928
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100929 /*
930 * Keep a copy of the hash after negprot. This hash will be
931 * the starting hash value for all sessions made from this
932 * server.
933 */
934 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
935 SMB2_PREAUTH_HASH_SIZE);
Steve French0fdfef92018-06-28 19:30:23 -0500936
Jeff Laytone598d1d82013-05-26 07:00:59 -0400937 /* SMB2 only has an extended negflavor */
938 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400939 /* set it to the maximum buffer size value we can send with 1 credit */
940 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
941 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400942 server->max_read = le32_to_cpu(rsp->MaxReadSize);
943 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400944 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500945 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
946 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
947 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400948 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400949 /* Internal types */
950 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400951
952 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000953 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500954 /*
955 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
956 * for us will be
957 * ses->sectype = RawNTLMSSP;
958 * but for time being this is our only auth choice so doesn't matter.
959 * We just found a server which sets blob length to zero expecting raw.
960 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700961 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500962 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700963 server->sec_ntlmssp = true;
964 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700965
Jeff Layton38d77c52013-05-26 07:01:00 -0400966 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400967 if (rc)
968 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500969 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500970 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500971 if (rc == 1)
972 rc = 0;
973 else if (rc == 0)
974 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400975 }
Steve French5100d8a2018-04-09 10:47:14 -0500976
Steve French5100d8a2018-04-09 10:47:14 -0500977 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
978 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000979 rc = smb311_decode_neg_context(rsp, server,
980 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -0500981 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000982 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
Steve French5100d8a2018-04-09 10:47:14 -0500983 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400984neg_exit:
985 free_rsp_buf(resp_buftype, rsp);
986 return rc;
987}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400988
Steve Frenchff1c0382013-11-19 23:44:46 -0600989int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
990{
Long Li2796d302018-04-25 11:30:04 -0700991 int rc;
992 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200993 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600994 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500995 u32 inbuflen; /* max of 4 dialects */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000996 struct TCP_Server_Info *server = tcon->ses->server;
Steve Frenchff1c0382013-11-19 23:44:46 -0600997
998 cifs_dbg(FYI, "validate negotiate\n");
999
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001000 /* In SMB3.11 preauth integrity supersedes validate negotiate */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001001 if (server->dialect == SMB311_PROT_ID)
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001002 return 0;
1003
Steve Frenchff1c0382013-11-19 23:44:46 -06001004 /*
1005 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -05001006 * can not sign it (ie are not known user). Even if signing is not
1007 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -06001008 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -05001009 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -06001010 */
Steve French0603c962017-09-20 19:57:18 -05001011 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -06001012 return 0; /* validation requires signing */
1013
Steve French0603c962017-09-20 19:57:18 -05001014 if (tcon->ses->user_name == NULL) {
1015 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1016 return 0; /* validation requires signing */
1017 }
1018
1019 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001020 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
Steve French0603c962017-09-20 19:57:18 -05001021
Long Li2796d302018-04-25 11:30:04 -07001022 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1023 if (!pneg_inbuf)
1024 return -ENOMEM;
1025
1026 pneg_inbuf->Capabilities =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001027 cpu_to_le32(server->vals->req_capabilities);
1028 memcpy(pneg_inbuf->Guid, server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +01001029 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -06001030
1031 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -07001032 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001033 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1034 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -07001035 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001036 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1037 else
Long Li2796d302018-04-25 11:30:04 -07001038 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001039
Steve French9764c022017-09-17 10:41:35 -05001040
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001041 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001042 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001043 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1044 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1045 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -05001046 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -07001047 inbuflen = sizeof(*pneg_inbuf) -
Steve Frenchd5c70762019-01-03 02:37:21 -06001048 (2 * sizeof(pneg_inbuf->Dialects[0]));
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001049 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001050 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001051 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1052 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1053 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -06001054 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1055 pneg_inbuf->DialectCount = cpu_to_le16(4);
Steve French9764c022017-09-17 10:41:35 -05001056 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -07001057 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -05001058 } else {
1059 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -07001060 pneg_inbuf->Dialects[0] =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001061 cpu_to_le16(server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -07001062 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -05001063 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -07001064 inbuflen = sizeof(*pneg_inbuf) -
1065 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -05001066 }
Steve Frenchff1c0382013-11-19 23:44:46 -06001067
1068 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1069 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001070 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1071 (char **)&pneg_rsp, &rsplen);
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001072 if (rc == -EOPNOTSUPP) {
1073 /*
1074 * Old Windows versions or Netapp SMB server can return
1075 * not supported error. Client should accept it.
1076 */
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001077 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
Colin Ian King21078202019-05-17 09:12:33 +01001078 rc = 0;
1079 goto out_free_inbuf;
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001080 } else if (rc != 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001081 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1082 rc);
Long Li2796d302018-04-25 11:30:04 -07001083 rc = -EIO;
1084 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -06001085 }
1086
Long Li2796d302018-04-25 11:30:04 -07001087 rc = -EIO;
1088 if (rsplen != sizeof(*pneg_rsp)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001089 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1090 rsplen);
Steve French7db0a6e2017-05-03 21:12:20 -05001091
1092 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -07001093 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1094 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001095 }
1096
1097 /* check validate negotiate info response matches what we got earlier */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001098 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -06001099 goto vneg_out;
1100
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001101 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
Steve Frenchff1c0382013-11-19 23:44:46 -06001102 goto vneg_out;
1103
1104 /* do not validate server guid because not saved at negprot time yet */
1105
1106 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001107 SMB2_LARGE_FILES) != server->capabilities)
Steve Frenchff1c0382013-11-19 23:44:46 -06001108 goto vneg_out;
1109
1110 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -07001111 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001112 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -07001113 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001114
1115vneg_out:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001116 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -07001117out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001118 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -07001119out_free_inbuf:
1120 kfree(pneg_inbuf);
1121 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -06001122}
1123
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301124enum securityEnum
1125smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1126{
1127 switch (requested) {
1128 case Kerberos:
1129 case RawNTLMSSP:
1130 return requested;
1131 case NTLMv2:
1132 return RawNTLMSSP;
1133 case Unspecified:
1134 if (server->sec_ntlmssp &&
1135 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1136 return RawNTLMSSP;
1137 if ((server->sec_kerberos || server->sec_mskerberos) &&
1138 (global_secflags & CIFSSEC_MAY_KRB5))
1139 return Kerberos;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001140 fallthrough;
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301141 default:
1142 return Unspecified;
1143 }
1144}
1145
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001146struct SMB2_sess_data {
1147 unsigned int xid;
1148 struct cifs_ses *ses;
1149 struct nls_table *nls_cp;
1150 void (*func)(struct SMB2_sess_data *);
1151 int result;
1152 u64 previous_session;
1153
1154 /* we will send the SMB in three pieces:
1155 * a fixed length beginning part, an optional
1156 * SPNEGO blob (which can be zero length), and a
1157 * last part which will include the strings
1158 * and rest of bcc area. This allows us to avoid
1159 * a large buffer 17K allocation
1160 */
1161 int buf0_type;
1162 struct kvec iov[2];
1163};
1164
1165static int
1166SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1167{
1168 int rc;
1169 struct cifs_ses *ses = sess_data->ses;
1170 struct smb2_sess_setup_req *req;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001171 struct TCP_Server_Info *server = cifs_ses_server(ses);
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001172 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001173
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001174 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1175 (void **) &req,
1176 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001177 if (rc)
1178 return rc;
1179
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001180 if (sess_data->ses->binding) {
1181 req->sync_hdr.SessionId = sess_data->ses->Suid;
1182 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1183 req->PreviousSessionId = 0;
1184 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1185 } else {
1186 /* First session, not a reauthenticate */
1187 req->sync_hdr.SessionId = 0;
1188 /*
1189 * if reconnect, we need to send previous sess id
1190 * otherwise it is 0
1191 */
1192 req->PreviousSessionId = sess_data->previous_session;
1193 req->Flags = 0; /* MBZ */
1194 }
Steve Frenchd4090142018-06-13 17:05:58 -05001195
1196 /* enough to enable echos and oplocks and one max size write */
1197 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001198
1199 /* only one of SMB2 signing flags may be set in SMB2 request */
1200 if (server->sign)
1201 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1202 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1203 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1204 else
1205 req->SecurityMode = 0;
1206
Steve French8d330962019-07-25 18:13:10 -05001207#ifdef CONFIG_CIFS_DFS_UPCALL
1208 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1209#else
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001210 req->Capabilities = 0;
Steve French8d330962019-07-25 18:13:10 -05001211#endif /* DFS_UPCALL */
1212
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001213 req->Channel = 0; /* MBZ */
1214
1215 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001216 /* 1 for pad */
1217 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001218 /*
1219 * This variable will be used to clear the buffer
1220 * allocated above in case of any error in the calling function.
1221 */
1222 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1223
1224 return 0;
1225}
1226
1227static void
1228SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1229{
1230 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1231 sess_data->buf0_type = CIFS_NO_BUFFER;
1232}
1233
1234static int
1235SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1236{
1237 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001238 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001239 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001240 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001241
1242 /* Testing shows that buffer offset must be at location of Buffer[0] */
1243 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001244 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001245 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1246
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001247 memset(&rqst, 0, sizeof(struct smb_rqst));
1248 rqst.rq_iov = sess_data->iov;
1249 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001250
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001251 /* BB add code to build os and lm fields */
1252 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001253 cifs_ses_server(sess_data->ses),
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001254 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001255 &sess_data->buf0_type,
1256 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001257 cifs_small_buf_release(sess_data->iov[0].iov_base);
1258 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001259
1260 return rc;
1261}
1262
1263static int
1264SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1265{
1266 int rc = 0;
1267 struct cifs_ses *ses = sess_data->ses;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001268 struct TCP_Server_Info *server = cifs_ses_server(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001269
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001270 mutex_lock(&server->srv_mutex);
1271 if (server->ops->generate_signingkey) {
1272 rc = server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001273 if (rc) {
1274 cifs_dbg(FYI,
1275 "SMB3 session key generation failed\n");
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001276 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001277 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001278 }
1279 }
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001280 if (!server->session_estab) {
1281 server->sequence_number = 0x2;
1282 server->session_estab = true;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001283 }
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001284 mutex_unlock(&server->srv_mutex);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001285
1286 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001287 /* keep existing ses state if binding */
1288 if (!ses->binding) {
1289 spin_lock(&GlobalMid_Lock);
1290 ses->status = CifsGood;
1291 ses->need_reconnect = false;
1292 spin_unlock(&GlobalMid_Lock);
1293 }
1294
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001295 return rc;
1296}
1297
1298#ifdef CONFIG_CIFS_UPCALL
1299static void
1300SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1301{
1302 int rc;
1303 struct cifs_ses *ses = sess_data->ses;
1304 struct cifs_spnego_msg *msg;
1305 struct key *spnego_key = NULL;
1306 struct smb2_sess_setup_rsp *rsp = NULL;
1307
1308 rc = SMB2_sess_alloc_buffer(sess_data);
1309 if (rc)
1310 goto out;
1311
1312 spnego_key = cifs_get_spnego_key(ses);
1313 if (IS_ERR(spnego_key)) {
1314 rc = PTR_ERR(spnego_key);
Steve French0a018942020-07-16 00:34:21 -05001315 if (rc == -ENOKEY)
1316 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001317 spnego_key = NULL;
1318 goto out;
1319 }
1320
1321 msg = spnego_key->payload.data[0];
1322 /*
1323 * check version field to make sure that cifs.upcall is
1324 * sending us a response in an expected form
1325 */
1326 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001327 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1328 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001329 rc = -EKEYREJECTED;
1330 goto out_put_spnego_key;
1331 }
1332
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001333 /* keep session key if binding */
1334 if (!ses->binding) {
1335 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1336 GFP_KERNEL);
1337 if (!ses->auth_key.response) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001338 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001339 msg->sesskey_len);
1340 rc = -ENOMEM;
1341 goto out_put_spnego_key;
1342 }
1343 ses->auth_key.len = msg->sesskey_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001344 }
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001345
1346 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1347 sess_data->iov[1].iov_len = msg->secblob_len;
1348
1349 rc = SMB2_sess_sendreceive(sess_data);
1350 if (rc)
1351 goto out_put_spnego_key;
1352
1353 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001354 /* keep session id and flags if binding */
1355 if (!ses->binding) {
1356 ses->Suid = rsp->sync_hdr.SessionId;
1357 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1358 }
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001359
1360 rc = SMB2_sess_establish_session(sess_data);
1361out_put_spnego_key:
1362 key_invalidate(spnego_key);
1363 key_put(spnego_key);
1364out:
1365 sess_data->result = rc;
1366 sess_data->func = NULL;
1367 SMB2_sess_free_buffer(sess_data);
1368}
1369#else
1370static void
1371SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1372{
1373 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1374 sess_data->result = -EOPNOTSUPP;
1375 sess_data->func = NULL;
1376}
1377#endif
1378
Sachin Prabhu166cea42016-10-07 19:11:22 +01001379static void
1380SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1381
1382static void
1383SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1384{
1385 int rc;
1386 struct cifs_ses *ses = sess_data->ses;
1387 struct smb2_sess_setup_rsp *rsp = NULL;
1388 char *ntlmssp_blob = NULL;
1389 bool use_spnego = false; /* else use raw ntlmssp */
1390 u16 blob_length = 0;
1391
1392 /*
1393 * If memory allocation is successful, caller of this function
1394 * frees it.
1395 */
1396 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1397 if (!ses->ntlmssp) {
1398 rc = -ENOMEM;
1399 goto out_err;
1400 }
1401 ses->ntlmssp->sesskey_per_smbsess = true;
1402
1403 rc = SMB2_sess_alloc_buffer(sess_data);
1404 if (rc)
1405 goto out_err;
1406
1407 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1408 GFP_KERNEL);
1409 if (ntlmssp_blob == NULL) {
1410 rc = -ENOMEM;
1411 goto out;
1412 }
1413
1414 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1415 if (use_spnego) {
1416 /* BB eventually need to add this */
1417 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1418 rc = -EOPNOTSUPP;
1419 goto out;
1420 } else {
1421 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1422 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1423 }
1424 sess_data->iov[1].iov_base = ntlmssp_blob;
1425 sess_data->iov[1].iov_len = blob_length;
1426
1427 rc = SMB2_sess_sendreceive(sess_data);
1428 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1429
1430 /* If true, rc here is expected and not an error */
1431 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001432 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001433 rc = 0;
1434
1435 if (rc)
1436 goto out;
1437
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001438 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001439 le16_to_cpu(rsp->SecurityBufferOffset)) {
1440 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1441 le16_to_cpu(rsp->SecurityBufferOffset));
1442 rc = -EIO;
1443 goto out;
1444 }
1445 rc = decode_ntlmssp_challenge(rsp->Buffer,
1446 le16_to_cpu(rsp->SecurityBufferLength), ses);
1447 if (rc)
1448 goto out;
1449
1450 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1451
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001452 /* keep existing ses id and flags if binding */
1453 if (!ses->binding) {
1454 ses->Suid = rsp->sync_hdr.SessionId;
1455 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1456 }
Sachin Prabhu166cea42016-10-07 19:11:22 +01001457
1458out:
1459 kfree(ntlmssp_blob);
1460 SMB2_sess_free_buffer(sess_data);
1461 if (!rc) {
1462 sess_data->result = 0;
1463 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1464 return;
1465 }
1466out_err:
1467 kfree(ses->ntlmssp);
1468 ses->ntlmssp = NULL;
1469 sess_data->result = rc;
1470 sess_data->func = NULL;
1471}
1472
1473static void
1474SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1475{
1476 int rc;
1477 struct cifs_ses *ses = sess_data->ses;
1478 struct smb2_sess_setup_req *req;
1479 struct smb2_sess_setup_rsp *rsp = NULL;
1480 unsigned char *ntlmssp_blob = NULL;
1481 bool use_spnego = false; /* else use raw ntlmssp */
1482 u16 blob_length = 0;
1483
1484 rc = SMB2_sess_alloc_buffer(sess_data);
1485 if (rc)
1486 goto out;
1487
1488 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001489 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001490
1491 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1492 sess_data->nls_cp);
1493 if (rc) {
1494 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1495 goto out;
1496 }
1497
1498 if (use_spnego) {
1499 /* BB eventually need to add this */
1500 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1501 rc = -EOPNOTSUPP;
1502 goto out;
1503 }
1504 sess_data->iov[1].iov_base = ntlmssp_blob;
1505 sess_data->iov[1].iov_len = blob_length;
1506
1507 rc = SMB2_sess_sendreceive(sess_data);
1508 if (rc)
1509 goto out;
1510
1511 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1512
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001513 /* keep existing ses id and flags if binding */
1514 if (!ses->binding) {
1515 ses->Suid = rsp->sync_hdr.SessionId;
1516 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1517 }
Sachin Prabhu166cea42016-10-07 19:11:22 +01001518
1519 rc = SMB2_sess_establish_session(sess_data);
Ronnie Sahlbergf560cda2020-04-12 16:09:26 +10001520#ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1521 if (ses->server->dialect < SMB30_PROT_ID) {
1522 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1523 /*
1524 * The session id is opaque in terms of endianness, so we can't
1525 * print it as a long long. we dump it as we got it on the wire
1526 */
1527 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1528 &ses->Suid);
1529 cifs_dbg(VFS, "Session Key %*ph\n",
1530 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1531 cifs_dbg(VFS, "Signing Key %*ph\n",
1532 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1533 }
1534#endif
Sachin Prabhu166cea42016-10-07 19:11:22 +01001535out:
1536 kfree(ntlmssp_blob);
1537 SMB2_sess_free_buffer(sess_data);
1538 kfree(ses->ntlmssp);
1539 ses->ntlmssp = NULL;
1540 sess_data->result = rc;
1541 sess_data->func = NULL;
1542}
1543
1544static int
1545SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1546{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301547 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001548
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001549 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301550 cifs_dbg(FYI, "sess setup type %d\n", type);
1551 if (type == Unspecified) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001552 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301553 return -EINVAL;
1554 }
1555
1556 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001557 case Kerberos:
1558 sess_data->func = SMB2_auth_kerberos;
1559 break;
1560 case RawNTLMSSP:
1561 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1562 break;
1563 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301564 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001565 return -EOPNOTSUPP;
1566 }
1567
1568 return 0;
1569}
1570
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001571int
1572SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1573 const struct nls_table *nls_cp)
1574{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001575 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001576 struct TCP_Server_Info *server = cifs_ses_server(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001577 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001578
Joe Perchesf96637b2013-05-04 22:12:25 -05001579 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001580
Jeff Layton3534b852013-05-24 07:41:01 -04001581 if (!server) {
1582 WARN(1, "%s: server is NULL!\n", __func__);
1583 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001584 }
1585
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001586 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1587 if (!sess_data)
1588 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001589
1590 rc = SMB2_select_sec(ses, sess_data);
1591 if (rc)
1592 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001593 sess_data->xid = xid;
1594 sess_data->ses = ses;
1595 sess_data->buf0_type = CIFS_NO_BUFFER;
1596 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001597 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001598
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001599 /*
1600 * Initialize the session hash with the server one.
1601 */
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001602 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001603 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001604
Sachin Prabhu166cea42016-10-07 19:11:22 +01001605 while (sess_data->func)
1606 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001607
Steve Frenchc721c382017-09-19 18:40:03 -05001608 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001609 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001610 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001611out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001612 kfree(sess_data);
1613 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001614}
1615
1616int
1617SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1618{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001619 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001620 struct smb2_logoff_req *req; /* response is also trivial struct */
1621 int rc = 0;
1622 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001623 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001624 unsigned int total_len;
1625 struct kvec iov[1];
1626 struct kvec rsp_iov;
1627 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001628
Joe Perchesf96637b2013-05-04 22:12:25 -05001629 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001630
1631 if (ses && (ses->server))
1632 server = ses->server;
1633 else
1634 return -EIO;
1635
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001636 /* no need to send SMB logoff if uid already closed due to reconnect */
1637 if (ses->need_reconnect)
1638 goto smb2_session_already_dead;
1639
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001640 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1641 (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001642 if (rc)
1643 return rc;
1644
1645 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001646 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001647
1648 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1649 flags |= CIFS_TRANSFORM_REQ;
1650 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001651 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001652
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001653 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001654
1655 iov[0].iov_base = (char *)req;
1656 iov[0].iov_len = total_len;
1657
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001658 memset(&rqst, 0, sizeof(struct smb_rqst));
1659 rqst.rq_iov = iov;
1660 rqst.rq_nvec = 1;
1661
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001662 rc = cifs_send_recv(xid, ses, ses->server,
1663 &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001664 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001665 /*
1666 * No tcon so can't do
1667 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1668 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001669
1670smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001671 return rc;
1672}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001673
1674static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1675{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001676 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001677}
1678
1679#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1680
Steve Frenchde9f68df2013-11-15 11:26:24 -06001681/* These are similar values to what Windows uses */
1682static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1683{
1684 tcon->max_chunks = 256;
1685 tcon->max_bytes_chunk = 1048576;
1686 tcon->max_bytes_copy = 16777216;
1687}
1688
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001689int
1690SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1691 struct cifs_tcon *tcon, const struct nls_table *cp)
1692{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001693 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001694 struct smb2_tree_connect_req *req;
1695 struct smb2_tree_connect_rsp *rsp = NULL;
1696 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001697 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001698 int rc = 0;
1699 int resp_buftype;
1700 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001701 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001702 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001703 unsigned int total_len;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001704 struct TCP_Server_Info *server;
1705
1706 /* always use master channel */
1707 server = ses->server;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001708
Joe Perchesf96637b2013-05-04 22:12:25 -05001709 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001710
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001711 if (!server || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001712 return -EIO;
1713
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001714 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1715 if (unc_path == NULL)
1716 return -ENOMEM;
1717
1718 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1719 unc_path_len *= 2;
1720 if (unc_path_len < 2) {
1721 kfree(unc_path);
1722 return -EINVAL;
1723 }
1724
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001725 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001726 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001727 atomic_set(&tcon->num_remote_opens, 0);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001728 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1729 (void **) &req, &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001730 if (rc) {
1731 kfree(unc_path);
1732 return rc;
1733 }
1734
Steve French5a77e752018-05-09 17:43:08 -05001735 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001736 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001737
1738 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001739 /* 1 for pad */
1740 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001741
1742 /* Testing shows that buffer offset must be at location of Buffer[0] */
1743 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001744 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001745 req->PathLength = cpu_to_le16(unc_path_len - 2);
1746 iov[1].iov_base = unc_path;
1747 iov[1].iov_len = unc_path_len;
1748
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001749 /*
1750 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1751 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
Steve French8c11a602019-03-22 22:31:17 -05001752 * (Samba servers don't always set the flag so also check if null user)
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001753 */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001754 if ((server->dialect == SMB311_PROT_ID) &&
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001755 !smb3_encryption_required(tcon) &&
Steve French8c11a602019-03-22 22:31:17 -05001756 !(ses->session_flags &
1757 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1758 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
Steve French6188f282018-03-13 02:29:36 -05001759 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1760
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001761 memset(&rqst, 0, sizeof(struct smb_rqst));
1762 rqst.rq_iov = iov;
1763 rqst.rq_nvec = 2;
1764
Steve French4fe75c42019-02-14 01:19:02 -06001765 /* Need 64 for max size write so ask for more in case not there yet */
1766 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1767
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001768 rc = cifs_send_recv(xid, ses, server,
1769 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001770 cifs_small_buf_release(req);
1771 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001772 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001773 if (rc != 0) {
1774 if (tcon) {
1775 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1776 tcon->need_reconnect = true;
1777 }
1778 goto tcon_error_exit;
1779 }
1780
Christophe JAILLETcd123002017-05-12 17:59:32 +02001781 switch (rsp->ShareType) {
1782 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001783 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001784 break;
1785 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001786 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001787 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001788 break;
1789 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001790 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001791 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001792 break;
1793 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001794 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001795 rc = -EOPNOTSUPP;
1796 goto tcon_error_exit;
1797 }
1798
1799 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001800 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001801 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1802 tcon->tidStatus = CifsGood;
1803 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001804 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001805 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001806
1807 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1808 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001809 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001810
1811 if (tcon->seal &&
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001812 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001813 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001814
Steve Frenchde9f68df2013-11-15 11:26:24 -06001815 init_copy_chunk_defaults(tcon);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001816 if (server->ops->validate_negotiate)
1817 rc = server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001818tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001819
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001820 free_rsp_buf(resp_buftype, rsp);
1821 kfree(unc_path);
1822 return rc;
1823
1824tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001825 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001826 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001827 }
1828 goto tcon_exit;
1829}
1830
1831int
1832SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1833{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001834 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001835 struct smb2_tree_disconnect_req *req; /* response is trivial */
1836 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001837 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001838 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001839 unsigned int total_len;
1840 struct kvec iov[1];
1841 struct kvec rsp_iov;
1842 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001843
Joe Perchesf96637b2013-05-04 22:12:25 -05001844 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001845
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001846 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001847 return -EIO;
1848
1849 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1850 return 0;
1851
Pavel Shilovskyd9191312019-12-10 11:44:52 -08001852 close_shroot_lease(&tcon->crfid);
Ronnie Sahlberg72e73c72019-11-07 17:00:38 +10001853
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001854 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
1855 (void **) &req,
1856 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001857 if (rc)
1858 return rc;
1859
Steve French5a77e752018-05-09 17:43:08 -05001860 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001861 flags |= CIFS_TRANSFORM_REQ;
1862
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001863 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001864
1865 iov[0].iov_base = (char *)req;
1866 iov[0].iov_len = total_len;
1867
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001868 memset(&rqst, 0, sizeof(struct smb_rqst));
1869 rqst.rq_iov = iov;
1870 rqst.rq_nvec = 1;
1871
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001872 rc = cifs_send_recv(xid, ses, ses->server,
1873 &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001874 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001875 if (rc)
1876 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1877
1878 return rc;
1879}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001880
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001881
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001882static struct create_durable *
1883create_durable_buf(void)
1884{
1885 struct create_durable *buf;
1886
1887 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1888 if (!buf)
1889 return NULL;
1890
1891 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001892 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001893 buf->ccontext.DataLength = cpu_to_le32(16);
1894 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1895 (struct create_durable, Name));
1896 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001897 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001898 buf->Name[0] = 'D';
1899 buf->Name[1] = 'H';
1900 buf->Name[2] = 'n';
1901 buf->Name[3] = 'Q';
1902 return buf;
1903}
1904
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001905static struct create_durable *
1906create_reconnect_durable_buf(struct cifs_fid *fid)
1907{
1908 struct create_durable *buf;
1909
1910 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1911 if (!buf)
1912 return NULL;
1913
1914 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1915 (struct create_durable, Data));
1916 buf->ccontext.DataLength = cpu_to_le32(16);
1917 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1918 (struct create_durable, Name));
1919 buf->ccontext.NameLength = cpu_to_le16(4);
1920 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1921 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001922 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001923 buf->Name[0] = 'D';
1924 buf->Name[1] = 'H';
1925 buf->Name[2] = 'n';
1926 buf->Name[3] = 'C';
1927 return buf;
1928}
1929
Steve French89a5bfa2019-07-18 17:22:18 -05001930static void
1931parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1932{
1933 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1934
1935 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1936 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1937 buf->IndexNumber = pdisk_id->DiskFileId;
1938}
1939
Steve Frenchab3459d2020-02-06 17:31:56 -06001940static void
Aurelien Aptel69dda302020-03-02 17:53:22 +01001941parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
1942 struct create_posix_rsp *posix)
Steve Frenchab3459d2020-02-06 17:31:56 -06001943{
Aurelien Aptel69dda302020-03-02 17:53:22 +01001944 int sid_len;
1945 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
1946 u8 *end = beg + le32_to_cpu(cc->DataLength);
1947 u8 *sid;
Steve Frenchab3459d2020-02-06 17:31:56 -06001948
Aurelien Aptel69dda302020-03-02 17:53:22 +01001949 memset(posix, 0, sizeof(*posix));
Aurelien Aptel2e8af972020-02-08 15:50:56 +01001950
Aurelien Aptel69dda302020-03-02 17:53:22 +01001951 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
1952 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
1953 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
1954
1955 sid = beg + 12;
1956 sid_len = posix_info_sid_size(sid, end);
1957 if (sid_len < 0) {
1958 cifs_dbg(VFS, "bad owner sid in posix create response\n");
1959 return;
1960 }
1961 memcpy(&posix->owner, sid, sid_len);
1962
1963 sid = sid + sid_len;
1964 sid_len = posix_info_sid_size(sid, end);
1965 if (sid_len < 0) {
1966 cifs_dbg(VFS, "bad group sid in posix create response\n");
1967 return;
1968 }
1969 memcpy(&posix->group, sid, sid_len);
1970
1971 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
1972 posix->nlink, posix->mode, posix->reparse_tag);
Steve Frenchab3459d2020-02-06 17:31:56 -06001973}
1974
Steve French89a5bfa2019-07-18 17:22:18 -05001975void
1976smb2_parse_contexts(struct TCP_Server_Info *server,
Aurelien Aptel69dda302020-03-02 17:53:22 +01001977 struct smb2_create_rsp *rsp,
1978 unsigned int *epoch, char *lease_key, __u8 *oplock,
1979 struct smb2_file_all_info *buf,
1980 struct create_posix_rsp *posix)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001981{
1982 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001983 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001984 unsigned int next;
1985 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001986 char *name;
Colin Ian King3ece60e2020-10-20 15:19:36 +01001987 static const char smb3_create_tag_posix[] = {
1988 0x93, 0xAD, 0x25, 0x50, 0x9C,
1989 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
1990 0xDE, 0x96, 0x8B, 0xCD, 0x7C
1991 };
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001992
Steve French89a5bfa2019-07-18 17:22:18 -05001993 *oplock = 0;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001994 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001995 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001996 cc = (struct create_context *)data_offset;
Steve French89a5bfa2019-07-18 17:22:18 -05001997
1998 /* Initialize inode number to 0 in case no valid data in qfid context */
1999 if (buf)
2000 buf->IndexNumber = 0;
2001
Justin Maggarddeb7def2016-02-09 15:52:08 -08002002 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002003 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08002004 if (le16_to_cpu(cc->NameLength) == 4 &&
Steve French89a5bfa2019-07-18 17:22:18 -05002005 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2006 *oplock = server->ops->parse_lease_buf(cc, epoch,
2007 lease_key);
2008 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2009 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2010 parse_query_id_ctxt(cc, buf);
Steve Frenchab3459d2020-02-06 17:31:56 -06002011 else if ((le16_to_cpu(cc->NameLength) == 16)) {
Aurelien Aptel69dda302020-03-02 17:53:22 +01002012 if (posix &&
2013 memcmp(name, smb3_create_tag_posix, 16) == 0)
2014 parse_posix_ctxt(cc, buf, posix);
Steve Frenchab3459d2020-02-06 17:31:56 -06002015 }
2016 /* else {
2017 cifs_dbg(FYI, "Context not matched with len %d\n",
2018 le16_to_cpu(cc->NameLength));
2019 cifs_dump_mem("Cctxt name: ", name, 4);
2020 } */
Justin Maggarddeb7def2016-02-09 15:52:08 -08002021
2022 next = le32_to_cpu(cc->Next);
2023 if (!next)
2024 break;
2025 remaining -= next;
2026 cc = (struct create_context *)((char *)cc + next);
2027 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002028
Steve French89a5bfa2019-07-18 17:22:18 -05002029 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2030 *oplock = rsp->OplockLevel;
2031
2032 return;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002033}
2034
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002035static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002036add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02002037 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002038{
2039 struct smb2_create_req *req = iov[0].iov_base;
2040 unsigned int num = *num_iovec;
2041
Stefano Brivio729c0c92018-07-05 15:10:02 +02002042 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002043 if (iov[num].iov_base == NULL)
2044 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002045 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002046 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2047 if (!req->CreateContextsOffset)
2048 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002049 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002050 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002051 le32_add_cpu(&req->CreateContextsLength,
2052 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002053 *num_iovec = num + 1;
2054 return 0;
2055}
2056
Steve Frenchb56eae42015-11-03 09:26:27 -06002057static struct create_durable_v2 *
Steve Frenchca567eb2019-03-29 16:31:07 -05002058create_durable_v2_buf(struct cifs_open_parms *oparms)
Steve Frenchb56eae42015-11-03 09:26:27 -06002059{
Steve Frenchca567eb2019-03-29 16:31:07 -05002060 struct cifs_fid *pfid = oparms->fid;
Steve Frenchb56eae42015-11-03 09:26:27 -06002061 struct create_durable_v2 *buf;
2062
2063 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2064 if (!buf)
2065 return NULL;
2066
2067 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2068 (struct create_durable_v2, dcontext));
2069 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2070 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2071 (struct create_durable_v2, Name));
2072 buf->ccontext.NameLength = cpu_to_le16(4);
2073
Steve Frenchca567eb2019-03-29 16:31:07 -05002074 /*
2075 * NB: Handle timeout defaults to 0, which allows server to choose
2076 * (most servers default to 120 seconds) and most clients default to 0.
2077 * This can be overridden at mount ("handletimeout=") if the user wants
2078 * a different persistent (or resilient) handle timeout for all opens
2079 * opens on a particular SMB3 mount.
2080 */
2081 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
Steve Frenchb56eae42015-11-03 09:26:27 -06002082 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05002083 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06002084 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2085
2086 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2087 buf->Name[0] = 'D';
2088 buf->Name[1] = 'H';
2089 buf->Name[2] = '2';
2090 buf->Name[3] = 'Q';
2091 return buf;
2092}
2093
2094static struct create_durable_handle_reconnect_v2 *
2095create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2096{
2097 struct create_durable_handle_reconnect_v2 *buf;
2098
2099 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2100 GFP_KERNEL);
2101 if (!buf)
2102 return NULL;
2103
2104 buf->ccontext.DataOffset =
2105 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2106 dcontext));
2107 buf->ccontext.DataLength =
2108 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2109 buf->ccontext.NameOffset =
2110 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2111 Name));
2112 buf->ccontext.NameLength = cpu_to_le16(4);
2113
2114 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2115 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2116 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2117 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2118
2119 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2120 buf->Name[0] = 'D';
2121 buf->Name[1] = 'H';
2122 buf->Name[2] = '2';
2123 buf->Name[3] = 'C';
2124 return buf;
2125}
2126
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002127static int
Steve Frenchb56eae42015-11-03 09:26:27 -06002128add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002129 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002130{
2131 struct smb2_create_req *req = iov[0].iov_base;
2132 unsigned int num = *num_iovec;
2133
Steve Frenchca567eb2019-03-29 16:31:07 -05002134 iov[num].iov_base = create_durable_v2_buf(oparms);
Steve Frenchb56eae42015-11-03 09:26:27 -06002135 if (iov[num].iov_base == NULL)
2136 return -ENOMEM;
2137 iov[num].iov_len = sizeof(struct create_durable_v2);
2138 if (!req->CreateContextsOffset)
2139 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002140 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002141 iov[1].iov_len);
2142 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002143 *num_iovec = num + 1;
2144 return 0;
2145}
2146
2147static int
2148add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2149 struct cifs_open_parms *oparms)
2150{
2151 struct smb2_create_req *req = iov[0].iov_base;
2152 unsigned int num = *num_iovec;
2153
2154 /* indicate that we don't need to relock the file */
2155 oparms->reconnect = false;
2156
2157 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2158 if (iov[num].iov_base == NULL)
2159 return -ENOMEM;
2160 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2161 if (!req->CreateContextsOffset)
2162 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002163 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002164 iov[1].iov_len);
2165 le32_add_cpu(&req->CreateContextsLength,
2166 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002167 *num_iovec = num + 1;
2168 return 0;
2169}
2170
2171static int
2172add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2173 struct cifs_open_parms *oparms, bool use_persistent)
2174{
2175 struct smb2_create_req *req = iov[0].iov_base;
2176 unsigned int num = *num_iovec;
2177
2178 if (use_persistent) {
2179 if (oparms->reconnect)
2180 return add_durable_reconnect_v2_context(iov, num_iovec,
2181 oparms);
2182 else
2183 return add_durable_v2_context(iov, num_iovec, oparms);
2184 }
2185
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002186 if (oparms->reconnect) {
2187 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2188 /* indicate that we don't need to relock the file */
2189 oparms->reconnect = false;
2190 } else
2191 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002192 if (iov[num].iov_base == NULL)
2193 return -ENOMEM;
2194 iov[num].iov_len = sizeof(struct create_durable);
2195 if (!req->CreateContextsOffset)
2196 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002197 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002198 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08002199 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002200 *num_iovec = num + 1;
2201 return 0;
2202}
2203
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002204/* See MS-SMB2 2.2.13.2.7 */
2205static struct crt_twarp_ctxt *
2206create_twarp_buf(__u64 timewarp)
2207{
2208 struct crt_twarp_ctxt *buf;
2209
2210 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2211 if (!buf)
2212 return NULL;
2213
2214 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2215 (struct crt_twarp_ctxt, Timestamp));
2216 buf->ccontext.DataLength = cpu_to_le32(8);
2217 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2218 (struct crt_twarp_ctxt, Name));
2219 buf->ccontext.NameLength = cpu_to_le16(4);
2220 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2221 buf->Name[0] = 'T';
2222 buf->Name[1] = 'W';
2223 buf->Name[2] = 'r';
2224 buf->Name[3] = 'p';
2225 buf->Timestamp = cpu_to_le64(timewarp);
2226 return buf;
2227}
2228
2229/* See MS-SMB2 2.2.13.2.7 */
2230static int
2231add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2232{
2233 struct smb2_create_req *req = iov[0].iov_base;
2234 unsigned int num = *num_iovec;
2235
2236 iov[num].iov_base = create_twarp_buf(timewarp);
2237 if (iov[num].iov_base == NULL)
2238 return -ENOMEM;
2239 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2240 if (!req->CreateContextsOffset)
2241 req->CreateContextsOffset = cpu_to_le32(
2242 sizeof(struct smb2_create_req) +
2243 iov[num - 1].iov_len);
2244 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2245 *num_iovec = num + 1;
2246 return 0;
2247}
2248
Steve French975221e2020-06-12 09:25:21 -05002249/* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2250static void setup_owner_group_sids(char *buf)
2251{
2252 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2253
2254 /* Populate the user ownership fields S-1-5-88-1 */
2255 sids->owner.Revision = 1;
2256 sids->owner.NumAuth = 3;
2257 sids->owner.Authority[5] = 5;
2258 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2259 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2260 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2261
2262 /* Populate the group ownership fields S-1-5-88-2 */
2263 sids->group.Revision = 1;
2264 sids->group.NumAuth = 3;
2265 sids->group.Authority[5] = 5;
2266 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2267 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2268 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
Steve Frencha7a519a2020-06-12 14:49:47 -05002269
2270 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
Steve French975221e2020-06-12 09:25:21 -05002271}
2272
Steve Frenchfdef6652019-12-06 02:02:38 -06002273/* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2274static struct crt_sd_ctxt *
Steve French975221e2020-06-12 09:25:21 -05002275create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
Steve Frenchfdef6652019-12-06 02:02:38 -06002276{
2277 struct crt_sd_ctxt *buf;
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002278 __u8 *ptr, *aclptr;
2279 unsigned int acelen, acl_size, ace_count;
Steve French975221e2020-06-12 09:25:21 -05002280 unsigned int owner_offset = 0;
2281 unsigned int group_offset = 0;
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002282 struct smb3_acl acl;
Steve Frenchfdef6652019-12-06 02:02:38 -06002283
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002284 *len = roundup(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
Steve French975221e2020-06-12 09:25:21 -05002285
2286 if (set_owner) {
Steve French975221e2020-06-12 09:25:21 -05002287 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2288 *len += sizeof(struct owner_group_sids);
2289 }
2290
Steve Frenchfdef6652019-12-06 02:02:38 -06002291 buf = kzalloc(*len, GFP_KERNEL);
2292 if (buf == NULL)
2293 return buf;
2294
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002295 ptr = (__u8 *)&buf[1];
Steve French975221e2020-06-12 09:25:21 -05002296 if (set_owner) {
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002297 /* offset fields are from beginning of security descriptor not of create context */
2298 owner_offset = ptr - (__u8 *)&buf->sd;
Steve French975221e2020-06-12 09:25:21 -05002299 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002300 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
Steve French975221e2020-06-12 09:25:21 -05002301 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002302
2303 setup_owner_group_sids(ptr);
2304 ptr += sizeof(struct owner_group_sids);
Steve French975221e2020-06-12 09:25:21 -05002305 } else {
2306 buf->sd.OffsetOwner = 0;
2307 buf->sd.OffsetGroup = 0;
2308 }
2309
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002310 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
Steve French975221e2020-06-12 09:25:21 -05002311 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
Steve Frenchfdef6652019-12-06 02:02:38 -06002312 buf->ccontext.NameLength = cpu_to_le16(4);
2313 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2314 buf->Name[0] = 'S';
2315 buf->Name[1] = 'e';
2316 buf->Name[2] = 'c';
2317 buf->Name[3] = 'D';
2318 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002319
Steve Frenchfdef6652019-12-06 02:02:38 -06002320 /*
2321 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2322 * and "DP" ie the DACL is present
2323 */
2324 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2325
2326 /* offset owner, group and Sbz1 and SACL are all zero */
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002327 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2328 /* Ship the ACL for now. we will copy it into buf later. */
2329 aclptr = ptr;
2330 ptr += sizeof(struct cifs_acl);
Steve Frenchfdef6652019-12-06 02:02:38 -06002331
2332 /* create one ACE to hold the mode embedded in reserved special SID */
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002333 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2334 ptr += acelen;
2335 acl_size = acelen + sizeof(struct smb3_acl);
2336 ace_count = 1;
Steve French975221e2020-06-12 09:25:21 -05002337
2338 if (set_owner) {
2339 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002340 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2341 ptr += acelen;
2342 acl_size += acelen;
2343 ace_count += 1;
2344 }
Steve French975221e2020-06-12 09:25:21 -05002345
Steve French643fbce2020-01-16 19:55:33 -06002346 /* and one more ACE to allow access for authenticated users */
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002347 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2348 ptr += acelen;
2349 acl_size += acelen;
2350 ace_count += 1;
Steve French975221e2020-06-12 09:25:21 -05002351
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002352 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2353 acl.AclSize = cpu_to_le16(acl_size);
2354 acl.AceCount = cpu_to_le16(ace_count);
2355 memcpy(aclptr, &acl, sizeof(struct cifs_acl));
2356
2357 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2358 *len = ptr - (__u8 *)buf;
Steve French975221e2020-06-12 09:25:21 -05002359
Steve Frenchfdef6652019-12-06 02:02:38 -06002360 return buf;
2361}
2362
2363static int
Steve French975221e2020-06-12 09:25:21 -05002364add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
Steve Frenchfdef6652019-12-06 02:02:38 -06002365{
2366 struct smb2_create_req *req = iov[0].iov_base;
2367 unsigned int num = *num_iovec;
2368 unsigned int len = 0;
2369
Steve French975221e2020-06-12 09:25:21 -05002370 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
Steve Frenchfdef6652019-12-06 02:02:38 -06002371 if (iov[num].iov_base == NULL)
2372 return -ENOMEM;
2373 iov[num].iov_len = len;
2374 if (!req->CreateContextsOffset)
2375 req->CreateContextsOffset = cpu_to_le32(
2376 sizeof(struct smb2_create_req) +
2377 iov[num - 1].iov_len);
2378 le32_add_cpu(&req->CreateContextsLength, len);
2379 *num_iovec = num + 1;
2380 return 0;
2381}
2382
Steve Frenchff2a09e2019-07-06 14:41:38 -05002383static struct crt_query_id_ctxt *
2384create_query_id_buf(void)
2385{
2386 struct crt_query_id_ctxt *buf;
2387
2388 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2389 if (!buf)
2390 return NULL;
2391
2392 buf->ccontext.DataOffset = cpu_to_le16(0);
2393 buf->ccontext.DataLength = cpu_to_le32(0);
2394 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2395 (struct crt_query_id_ctxt, Name));
2396 buf->ccontext.NameLength = cpu_to_le16(4);
2397 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2398 buf->Name[0] = 'Q';
2399 buf->Name[1] = 'F';
2400 buf->Name[2] = 'i';
2401 buf->Name[3] = 'd';
2402 return buf;
2403}
2404
2405/* See MS-SMB2 2.2.13.2.9 */
2406static int
2407add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2408{
2409 struct smb2_create_req *req = iov[0].iov_base;
2410 unsigned int num = *num_iovec;
2411
2412 iov[num].iov_base = create_query_id_buf();
2413 if (iov[num].iov_base == NULL)
2414 return -ENOMEM;
2415 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2416 if (!req->CreateContextsOffset)
2417 req->CreateContextsOffset = cpu_to_le32(
2418 sizeof(struct smb2_create_req) +
2419 iov[num - 1].iov_len);
2420 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2421 *num_iovec = num + 1;
2422 return 0;
2423}
2424
Aurelien Aptelf0712922017-02-22 14:47:17 +01002425static int
2426alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2427 const char *treename, const __le16 *path)
2428{
2429 int treename_len, path_len;
2430 struct nls_table *cp;
2431 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2432
2433 /*
2434 * skip leading "\\"
2435 */
2436 treename_len = strlen(treename);
2437 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2438 return -EINVAL;
2439
2440 treename += 2;
2441 treename_len -= 2;
2442
2443 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2444
2445 /*
2446 * make room for one path separator between the treename and
2447 * path
2448 */
2449 *out_len = treename_len + 1 + path_len;
2450
2451 /*
2452 * final path needs to be null-terminated UTF16 with a
2453 * size aligned to 8
2454 */
2455
2456 *out_size = roundup((*out_len+1)*2, 8);
2457 *out_path = kzalloc(*out_size, GFP_KERNEL);
2458 if (!*out_path)
2459 return -ENOMEM;
2460
2461 cp = load_nls_default();
2462 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2463 UniStrcat(*out_path, sep);
2464 UniStrcat(*out_path, path);
2465 unload_nls(cp);
2466
2467 return 0;
2468}
2469
Steve Frenchbea851b2018-06-14 21:56:32 -05002470int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2471 umode_t mode, struct cifs_tcon *tcon,
2472 const char *full_path,
2473 struct cifs_sb_info *cifs_sb)
2474{
2475 struct smb_rqst rqst;
2476 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002477 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002478 struct cifs_ses *ses = tcon->ses;
2479 struct kvec iov[3]; /* make sure at least one for each open context */
2480 struct kvec rsp_iov = {NULL, 0};
2481 int resp_buftype;
2482 int uni_path_len;
2483 __le16 *copy_path = NULL;
2484 int copy_size;
2485 int rc = 0;
2486 unsigned int n_iov = 2;
2487 __u32 file_attributes = 0;
2488 char *pc_buf = NULL;
2489 int flags = 0;
2490 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002491 __le16 *utf16_path = NULL;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002492 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Steve Frenchbea851b2018-06-14 21:56:32 -05002493
2494 cifs_dbg(FYI, "mkdir\n");
2495
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002496 /* resource #1: path allocation */
2497 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2498 if (!utf16_path)
2499 return -ENOMEM;
2500
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002501 if (!ses || !server) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002502 rc = -EIO;
2503 goto err_free_path;
2504 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002505
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002506 /* resource #2: request */
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002507 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2508 (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002509 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002510 goto err_free_path;
2511
Steve Frenchbea851b2018-06-14 21:56:32 -05002512
2513 if (smb3_encryption_required(tcon))
2514 flags |= CIFS_TRANSFORM_REQ;
2515
Steve Frenchbea851b2018-06-14 21:56:32 -05002516 req->ImpersonationLevel = IL_IMPERSONATION;
2517 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2518 /* File attributes ignored on open (used in create though) */
2519 req->FileAttributes = cpu_to_le32(file_attributes);
2520 req->ShareAccess = FILE_SHARE_ALL_LE;
2521 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2522 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2523
2524 iov[0].iov_base = (char *)req;
2525 /* -1 since last byte is buf[0] which is sent below (path) */
2526 iov[0].iov_len = total_len - 1;
2527
2528 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2529
2530 /* [MS-SMB2] 2.2.13 NameOffset:
2531 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2532 * the SMB2 header, the file name includes a prefix that will
2533 * be processed during DFS name normalization as specified in
2534 * section 3.3.5.9. Otherwise, the file name is relative to
2535 * the share that is identified by the TreeId in the SMB2
2536 * header.
2537 */
2538 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2539 int name_len;
2540
2541 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2542 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2543 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002544 tcon->treeName, utf16_path);
2545 if (rc)
2546 goto err_free_req;
2547
Steve Frenchbea851b2018-06-14 21:56:32 -05002548 req->NameLength = cpu_to_le16(name_len * 2);
2549 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002550 /* free before overwriting resource */
2551 kfree(utf16_path);
2552 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002553 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002554 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002555 /* MUST set path len (NameLength) to 0 opening root of share */
2556 req->NameLength = cpu_to_le16(uni_path_len - 2);
2557 if (uni_path_len % 8 != 0) {
2558 copy_size = roundup(uni_path_len, 8);
2559 copy_path = kzalloc(copy_size, GFP_KERNEL);
2560 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002561 rc = -ENOMEM;
2562 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002563 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002564 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002565 uni_path_len);
2566 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002567 /* free before overwriting resource */
2568 kfree(utf16_path);
2569 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002570 }
2571 }
2572
2573 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002574 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002575 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2576
2577 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002578 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002579 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002580 if (rc)
2581 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002582 pc_buf = iov[n_iov-1].iov_base;
2583 }
2584
2585
2586 memset(&rqst, 0, sizeof(struct smb_rqst));
2587 rqst.rq_iov = iov;
2588 rqst.rq_nvec = n_iov;
2589
Steve Frenchd2f15422019-09-22 00:55:46 -05002590 /* no need to inc num_remote_opens because we close it just below */
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002591 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2592 FILE_WRITE_ATTRIBUTES);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002593 /* resource #4: response buffer */
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002594 rc = cifs_send_recv(xid, ses, server,
2595 &rqst, &resp_buftype, flags, &rsp_iov);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002596 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002597 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2598 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002599 CREATE_NOT_FILE,
2600 FILE_WRITE_ATTRIBUTES, rc);
2601 goto err_free_rsp_buf;
2602 }
2603
2604 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2605 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2606 ses->Suid, CREATE_NOT_FILE,
2607 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002608
2609 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2610
2611 /* Eventually save off posix specific response info and timestaps */
2612
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002613err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002614 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002615 kfree(pc_buf);
2616err_free_req:
2617 cifs_small_buf_release(req);
2618err_free_path:
2619 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002620 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002621}
Steve Frenchbea851b2018-06-14 21:56:32 -05002622
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002623int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002624SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2625 struct smb_rqst *rqst, __u8 *oplock,
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002626 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002627{
2628 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002629 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002630 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002631 int copy_size;
2632 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002633 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002634 struct kvec *iov = rqst->rq_iov;
2635 __le16 *copy_path;
2636 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002637
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002638 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2639 (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002640 if (rc)
2641 return rc;
2642
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002643 iov[0].iov_base = (char *)req;
2644 /* -1 since last byte is buf[0] which is sent below (path) */
2645 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002646
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002647 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002648 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002649 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2650 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002651
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002652 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002653 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002654 /* File attributes ignored on open (used in create though) */
2655 req->FileAttributes = cpu_to_le32(file_attributes);
2656 req->ShareAccess = FILE_SHARE_ALL_LE;
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002657
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002658 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2659 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002660 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002661
2662 /* [MS-SMB2] 2.2.13 NameOffset:
2663 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2664 * the SMB2 header, the file name includes a prefix that will
2665 * be processed during DFS name normalization as specified in
2666 * section 3.3.5.9. Otherwise, the file name is relative to
2667 * the share that is identified by the TreeId in the SMB2
2668 * header.
2669 */
2670 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2671 int name_len;
2672
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002673 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002674 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2675 &name_len,
2676 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002677 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002678 return rc;
2679 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002680 uni_path_len = copy_size;
2681 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002682 } else {
2683 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2684 /* MUST set path len (NameLength) to 0 opening root of share */
2685 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002686 copy_size = uni_path_len;
2687 if (copy_size % 8 != 0)
2688 copy_size = roundup(copy_size, 8);
2689 copy_path = kzalloc(copy_size, GFP_KERNEL);
2690 if (!copy_path)
2691 return -ENOMEM;
2692 memcpy((char *)copy_path, (const char *)path,
2693 uni_path_len);
2694 uni_path_len = copy_size;
2695 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002696 }
2697
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002698 iov[1].iov_len = uni_path_len;
2699 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002700
Steve French3e7a02d2019-09-11 21:46:20 -05002701 if ((!server->oplocks) || (tcon->no_lease))
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002702 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2703
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002704 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002705 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2706 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002707 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2708 (oparms->create_options & CREATE_NOT_FILE))
2709 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002710 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002711 rc = add_lease_context(server, iov, &n_iov,
2712 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002713 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002714 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002715 }
2716
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002717 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2718 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002719 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002720 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002721 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002722 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002723 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002724 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002725
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002726 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002727 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002728 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002729 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002730 }
2731
Steve Frenchce558b02018-05-31 19:16:54 -05002732 if (tcon->posix_extensions) {
2733 if (n_iov > 2) {
2734 struct create_context *ccontext =
2735 (struct create_context *)iov[n_iov-1].iov_base;
2736 ccontext->Next =
2737 cpu_to_le32(iov[n_iov-1].iov_len);
2738 }
2739
2740 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002741 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002742 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002743 }
Steve Frenchce558b02018-05-31 19:16:54 -05002744
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002745 if (tcon->snapshot_time) {
2746 cifs_dbg(FYI, "adding snapshot context\n");
2747 if (n_iov > 2) {
2748 struct create_context *ccontext =
2749 (struct create_context *)iov[n_iov-1].iov_base;
2750 ccontext->Next =
2751 cpu_to_le32(iov[n_iov-1].iov_len);
2752 }
2753
2754 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2755 if (rc)
2756 return rc;
2757 }
2758
Steve French975221e2020-06-12 09:25:21 -05002759 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
2760 bool set_mode;
2761 bool set_owner;
2762
2763 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2764 (oparms->mode != ACL_NO_MODE))
2765 set_mode = true;
2766 else {
2767 set_mode = false;
2768 oparms->mode = ACL_NO_MODE;
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002769 }
2770
Steve French975221e2020-06-12 09:25:21 -05002771 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
2772 set_owner = true;
2773 else
2774 set_owner = false;
2775
2776 if (set_owner | set_mode) {
2777 if (n_iov > 2) {
2778 struct create_context *ccontext =
2779 (struct create_context *)iov[n_iov-1].iov_base;
2780 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2781 }
2782
2783 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2784 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
2785 if (rc)
2786 return rc;
2787 }
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002788 }
2789
Steve Frenchff2a09e2019-07-06 14:41:38 -05002790 if (n_iov > 2) {
2791 struct create_context *ccontext =
2792 (struct create_context *)iov[n_iov-1].iov_base;
2793 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2794 }
2795 add_query_id_context(iov, &n_iov);
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002796
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002797 rqst->rq_nvec = n_iov;
2798 return 0;
2799}
2800
2801/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2802 * All other vectors are freed by kfree().
2803 */
2804void
2805SMB2_open_free(struct smb_rqst *rqst)
2806{
2807 int i;
2808
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002809 if (rqst && rqst->rq_iov) {
2810 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2811 for (i = 1; i < rqst->rq_nvec; i++)
2812 if (rqst->rq_iov[i].iov_base != smb2_padding)
2813 kfree(rqst->rq_iov[i].iov_base);
2814 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002815}
2816
2817int
2818SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2819 __u8 *oplock, struct smb2_file_all_info *buf,
Aurelien Aptel69dda302020-03-02 17:53:22 +01002820 struct create_posix_rsp *posix,
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002821 struct kvec *err_iov, int *buftype)
2822{
2823 struct smb_rqst rqst;
2824 struct smb2_create_rsp *rsp = NULL;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002825 struct cifs_tcon *tcon = oparms->tcon;
2826 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002827 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002828 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002829 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002830 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002831 int rc = 0;
2832 int flags = 0;
2833
2834 cifs_dbg(FYI, "create/open\n");
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002835 if (!ses || !server)
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002836 return -EIO;
2837
2838 if (smb3_encryption_required(tcon))
2839 flags |= CIFS_TRANSFORM_REQ;
2840
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002841 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002842 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002843 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002844 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002845
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002846 rc = SMB2_open_init(tcon, server,
2847 &rqst, oplock, oparms, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002848 if (rc)
2849 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002850
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002851 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2852 oparms->create_options, oparms->desired_access);
2853
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002854 rc = cifs_send_recv(xid, ses, server,
2855 &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002856 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002857 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002858
2859 if (rc != 0) {
2860 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002861 if (err_iov && rsp) {
2862 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002863 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002864 resp_buftype = CIFS_NO_BUFFER;
2865 rsp = NULL;
2866 }
Steve French28d59362018-05-30 21:42:34 -05002867 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2868 oparms->create_options, oparms->desired_access, rc);
Steve French7dcc82c2019-09-11 00:07:36 -05002869 if (rc == -EREMCHG) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002870 pr_warn_once("server share %s deleted\n",
2871 tcon->treeName);
Steve French7dcc82c2019-09-11 00:07:36 -05002872 tcon->need_reconnect = true;
2873 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002874 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002875 } else
2876 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2877 ses->Suid, oparms->create_options,
2878 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002879
Steve Frenchfae80442018-10-19 17:14:32 -05002880 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002881 oparms->fid->persistent_fid = rsp->PersistentFileId;
2882 oparms->fid->volatile_fid = rsp->VolatileFileId;
Aurelien Aptel86f740f2020-02-21 11:19:06 +01002883 oparms->fid->access = oparms->desired_access;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002884#ifdef CONFIG_CIFS_DEBUG2
2885 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2886#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002887
2888 if (buf) {
2889 memcpy(buf, &rsp->CreationTime, 32);
2890 buf->AllocationSize = rsp->AllocationSize;
2891 buf->EndOfFile = rsp->EndofFile;
2892 buf->Attributes = rsp->FileAttributes;
2893 buf->NumberOfLinks = cpu_to_le32(1);
2894 buf->DeletePending = 0;
2895 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002896
Steve French89a5bfa2019-07-18 17:22:18 -05002897
2898 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
Aurelien Aptel69dda302020-03-02 17:53:22 +01002899 oparms->fid->lease_key, oplock, buf, posix);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002900creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002901 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002902 free_rsp_buf(resp_buftype, rsp);
2903 return rc;
2904}
2905
Steve French4a72daf2013-06-25 00:20:49 -05002906int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002907SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2908 struct smb_rqst *rqst,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002909 u64 persistent_fid, u64 volatile_fid, u32 opcode,
Steve French153322f2019-03-28 22:32:49 -05002910 bool is_fsctl, char *in_data, u32 indatalen,
2911 __u32 max_response_size)
Steve French4a72daf2013-06-25 00:20:49 -05002912{
2913 struct smb2_ioctl_req *req;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002914 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002915 unsigned int total_len;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002916 int rc;
Long Li2c87d6a2019-05-15 14:09:05 -07002917 char *in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002918
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002919 rc = smb2_ioctl_req_init(opcode, tcon, server,
2920 (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002921 if (rc)
2922 return rc;
2923
Long Li2c87d6a2019-05-15 14:09:05 -07002924 if (indatalen) {
2925 /*
2926 * indatalen is usually small at a couple of bytes max, so
2927 * just allocate through generic pool
2928 */
YueHaibingd81f0972019-06-01 03:31:10 +00002929 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
Long Li2c87d6a2019-05-15 14:09:05 -07002930 if (!in_data_buf) {
2931 cifs_small_buf_release(req);
2932 return -ENOMEM;
2933 }
Long Li2c87d6a2019-05-15 14:09:05 -07002934 }
2935
Steve French4a72daf2013-06-25 00:20:49 -05002936 req->CtlCode = cpu_to_le32(opcode);
2937 req->PersistentFileId = persistent_fid;
2938 req->VolatileFileId = volatile_fid;
2939
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002940 iov[0].iov_base = (char *)req;
2941 /*
2942 * If no input data, the size of ioctl struct in
2943 * protocol spec still includes a 1 byte data buffer,
2944 * but if input data passed to ioctl, we do not
2945 * want to double count this, so we do not send
2946 * the dummy one byte of data in iovec[0] if sending
2947 * input data (in iovec[1]).
2948 */
Steve French4a72daf2013-06-25 00:20:49 -05002949 if (indatalen) {
2950 req->InputCount = cpu_to_le32(indatalen);
2951 /* do not set InputOffset if no input data */
2952 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002953 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002954 rqst->rq_nvec = 2;
2955 iov[0].iov_len = total_len - 1;
Long Li2c87d6a2019-05-15 14:09:05 -07002956 iov[1].iov_base = in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002957 iov[1].iov_len = indatalen;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002958 } else {
2959 rqst->rq_nvec = 1;
2960 iov[0].iov_len = total_len;
2961 }
Steve French4a72daf2013-06-25 00:20:49 -05002962
2963 req->OutputOffset = 0;
2964 req->OutputCount = 0; /* MBZ */
2965
2966 /*
Steve French153322f2019-03-28 22:32:49 -05002967 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2968 * We Could increase default MaxOutputResponse, but that could require
2969 * more credits. Windows typically sets this smaller, but for some
Steve French4a72daf2013-06-25 00:20:49 -05002970 * ioctls it may be useful to allow server to send more. No point
2971 * limiting what the server can send as long as fits in one credit
Steve French153322f2019-03-28 22:32:49 -05002972 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2973 * to increase this limit up in the future.
2974 * Note that for snapshot queries that servers like Azure expect that
2975 * the first query be minimal size (and just used to get the number/size
2976 * of previous versions) so response size must be specified as EXACTLY
2977 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2978 * of eight bytes. Currently that is the only case where we set max
2979 * response size smaller.
Steve French4a72daf2013-06-25 00:20:49 -05002980 */
Steve French153322f2019-03-28 22:32:49 -05002981 req->MaxOutputResponse = cpu_to_le32(max_response_size);
Namjae Jeonebf57442020-06-11 11:21:19 +09002982 req->sync_hdr.CreditCharge =
2983 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
2984 SMB2_MAX_BUFFER_SIZE));
Steve French4a72daf2013-06-25 00:20:49 -05002985 if (is_fsctl)
2986 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2987 else
2988 req->Flags = 0;
2989
Steve French4587eee2017-10-25 15:58:31 -05002990 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2991 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002992 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002993
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002994 return 0;
2995}
2996
2997void
2998SMB2_ioctl_free(struct smb_rqst *rqst)
2999{
Murphy Zhou6457c202019-05-23 12:12:43 +08003000 int i;
Long Li2c87d6a2019-05-15 14:09:05 -07003001 if (rqst && rqst->rq_iov) {
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003002 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Murphy Zhou6457c202019-05-23 12:12:43 +08003003 for (i = 1; i < rqst->rq_nvec; i++)
3004 if (rqst->rq_iov[i].iov_base != smb2_padding)
3005 kfree(rqst->rq_iov[i].iov_base);
Long Li2c87d6a2019-05-15 14:09:05 -07003006 }
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003007}
3008
Steve French153322f2019-03-28 22:32:49 -05003009
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003010/*
3011 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3012 */
3013int
3014SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3015 u64 volatile_fid, u32 opcode, bool is_fsctl,
Steve French153322f2019-03-28 22:32:49 -05003016 char *in_data, u32 indatalen, u32 max_out_data_len,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003017 char **out_data, u32 *plen /* returned data len */)
3018{
3019 struct smb_rqst rqst;
3020 struct smb2_ioctl_rsp *rsp = NULL;
3021 struct cifs_ses *ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003022 struct TCP_Server_Info *server;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003023 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003024 struct kvec rsp_iov = {NULL, 0};
3025 int resp_buftype = CIFS_NO_BUFFER;
3026 int rc = 0;
3027 int flags = 0;
3028
3029 cifs_dbg(FYI, "SMB2 IOCTL\n");
3030
3031 if (out_data != NULL)
3032 *out_data = NULL;
3033
3034 /* zero out returned data len, in case of error */
3035 if (plen)
3036 *plen = 0;
3037
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003038 if (!tcon)
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003039 return -EIO;
3040
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003041 ses = tcon->ses;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003042 if (!ses)
3043 return -EIO;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003044
3045 server = cifs_pick_channel(ses);
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003046 if (!server)
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003047 return -EIO;
3048
3049 if (smb3_encryption_required(tcon))
3050 flags |= CIFS_TRANSFORM_REQ;
3051
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003052 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003053 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003054 rqst.rq_iov = iov;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003055 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003056
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003057 rc = SMB2_ioctl_init(tcon, server,
3058 &rqst, persistent_fid, volatile_fid, opcode,
Steve French153322f2019-03-28 22:32:49 -05003059 is_fsctl, in_data, indatalen, max_out_data_len);
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003060 if (rc)
3061 goto ioctl_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003062
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003063 rc = cifs_send_recv(xid, ses, server,
3064 &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11003065 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003066 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05003067
Steve Frencheccb4422018-05-17 21:16:55 -05003068 if (rc != 0)
3069 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3070 ses->Suid, 0, opcode, rc);
3071
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003072 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
Steve French8e353102015-03-26 19:47:02 -05003073 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05003074 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06003075 } else if (rc == -EINVAL) {
3076 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3077 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05003078 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06003079 goto ioctl_exit;
3080 }
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003081 } else if (rc == -E2BIG) {
3082 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3083 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3084 goto ioctl_exit;
3085 }
Steve French4a72daf2013-06-25 00:20:49 -05003086 }
3087
3088 /* check if caller wants to look at return data or just return rc */
3089 if ((plen == NULL) || (out_data == NULL))
3090 goto ioctl_exit;
3091
3092 *plen = le32_to_cpu(rsp->OutputCount);
3093
3094 /* We check for obvious errors in the output buffer length and offset */
3095 if (*plen == 0)
3096 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03003097 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003098 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
Steve French4a72daf2013-06-25 00:20:49 -05003099 *plen = 0;
3100 rc = -EIO;
3101 goto ioctl_exit;
3102 }
3103
Dan Carpenter2d204ee2018-09-10 14:12:07 +03003104 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003105 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
Steve French4a72daf2013-06-25 00:20:49 -05003106 le32_to_cpu(rsp->OutputOffset));
3107 *plen = 0;
3108 rc = -EIO;
3109 goto ioctl_exit;
3110 }
3111
YueHaibingd034fee2018-09-10 01:33:06 +00003112 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3113 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05003114 if (*out_data == NULL) {
3115 rc = -ENOMEM;
3116 goto ioctl_exit;
3117 }
3118
Steve French4a72daf2013-06-25 00:20:49 -05003119ioctl_exit:
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003120 SMB2_ioctl_free(&rqst);
Steve French4a72daf2013-06-25 00:20:49 -05003121 free_rsp_buf(resp_buftype, rsp);
3122 return rc;
3123}
3124
Steve French64a5cfa2013-10-14 15:31:32 -05003125/*
3126 * Individual callers to ioctl worker function follow
3127 */
3128
3129int
3130SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3131 u64 persistent_fid, u64 volatile_fid)
3132{
3133 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05003134 struct compress_ioctl fsctl_input;
3135 char *ret_data = NULL;
3136
3137 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08003138 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05003139
3140 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3141 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3142 (char *)&fsctl_input /* data input */,
Steve French153322f2019-03-28 22:32:49 -05003143 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3144 &ret_data /* out data */, NULL);
Steve French64a5cfa2013-10-14 15:31:32 -05003145
3146 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05003147
3148 return rc;
3149}
3150
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003151int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003152SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3153 struct smb_rqst *rqst,
Steve French43f8a6a2019-12-02 21:46:54 -06003154 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003155{
3156 struct smb2_close_req *req;
3157 struct kvec *iov = rqst->rq_iov;
3158 unsigned int total_len;
3159 int rc;
3160
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003161 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3162 (void **) &req, &total_len);
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003163 if (rc)
3164 return rc;
3165
3166 req->PersistentFileId = persistent_fid;
3167 req->VolatileFileId = volatile_fid;
Steve French43f8a6a2019-12-02 21:46:54 -06003168 if (query_attrs)
3169 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3170 else
3171 req->Flags = 0;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003172 iov[0].iov_base = (char *)req;
3173 iov[0].iov_len = total_len;
3174
3175 return 0;
3176}
3177
3178void
3179SMB2_close_free(struct smb_rqst *rqst)
3180{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003181 if (rqst && rqst->rq_iov)
3182 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003183}
3184
3185int
Steve French43f8a6a2019-12-02 21:46:54 -06003186__SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3187 u64 persistent_fid, u64 volatile_fid,
3188 struct smb2_file_network_open_info *pbuf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003189{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003190 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003191 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003192 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003193 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003194 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003195 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01003196 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003197 int rc = 0;
Steve French9e8fae22019-12-02 17:55:41 -06003198 int flags = 0;
Steve French43f8a6a2019-12-02 21:46:54 -06003199 bool query_attrs = false;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003200
Joe Perchesf96637b2013-05-04 22:12:25 -05003201 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003202
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003203 if (!ses || !server)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003204 return -EIO;
3205
Steve French5a77e752018-05-09 17:43:08 -05003206 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003207 flags |= CIFS_TRANSFORM_REQ;
3208
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003209 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003210 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003211 rqst.rq_iov = iov;
3212 rqst.rq_nvec = 1;
3213
Steve French43f8a6a2019-12-02 21:46:54 -06003214 /* check if need to ask server to return timestamps in close response */
3215 if (pbuf)
3216 query_attrs = true;
3217
Steve Frenchf90f9792019-09-03 18:35:42 -05003218 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003219 rc = SMB2_close_init(tcon, server,
3220 &rqst, persistent_fid, volatile_fid,
Steve French43f8a6a2019-12-02 21:46:54 -06003221 query_attrs);
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003222 if (rc)
3223 goto close_exit;
3224
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003225 rc = cifs_send_recv(xid, ses, server,
3226 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003227 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003228
3229 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09003230 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003231 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3232 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003233 goto close_exit;
Steve French43f8a6a2019-12-02 21:46:54 -06003234 } else {
Steve Frenchf90f9792019-09-03 18:35:42 -05003235 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3236 ses->Suid);
Steve French43f8a6a2019-12-02 21:46:54 -06003237 /*
3238 * Note that have to subtract 4 since struct network_open_info
3239 * has a final 4 byte pad that close response does not have
3240 */
3241 if (pbuf)
3242 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3243 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003244
Steve Frenchfae80442018-10-19 17:14:32 -05003245 atomic_dec(&tcon->num_remote_opens);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003246close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003247 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003248 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003249
3250 /* retry close in a worker thread if this one is interrupted */
Paulo Alcantara531c88c2021-01-13 14:16:16 -03003251 if (is_interrupt_error(rc)) {
Steve French9e8fae22019-12-02 17:55:41 -06003252 int tmp_rc;
3253
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003254 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3255 volatile_fid);
3256 if (tmp_rc)
3257 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3258 persistent_fid, tmp_rc);
3259 }
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003260 return rc;
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06003261}
3262
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003263int
Steve French43f8a6a2019-12-02 21:46:54 -06003264SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3265 u64 persistent_fid, u64 volatile_fid)
3266{
3267 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3268}
3269
3270int
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003271smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3272 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003273{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003274 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003275 char *end_of_smb = smb_len + (char *)iov->iov_base;
3276 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003277 char *end_of_buf = begin_of_buf + buffer_length;
3278
3279
3280 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003281 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3282 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003283 return -EINVAL;
3284 }
3285
3286 /* check if beyond RFC1001 maximum length */
3287 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003288 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3289 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003290 return -EINVAL;
3291 }
3292
3293 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07003294 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003295 return -EINVAL;
3296 }
3297
3298 return 0;
3299}
3300
3301/*
3302 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3303 * Caller must free buffer.
3304 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003305int
3306smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3307 struct kvec *iov, unsigned int minbufsize,
3308 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003309{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003310 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003311 int rc;
3312
3313 if (!data)
3314 return -EINVAL;
3315
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003316 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003317 if (rc)
3318 return rc;
3319
3320 memcpy(data, begin_of_buf, buffer_length);
3321
3322 return 0;
3323}
3324
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003325int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003326SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3327 struct smb_rqst *rqst,
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003328 u64 persistent_fid, u64 volatile_fid,
3329 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003330 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003331{
3332 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003333 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003334 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003335 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003336
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003337 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3338 (void **) &req, &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003339 if (rc)
3340 return rc;
3341
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003342 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003343 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003344 req->PersistentFileId = persistent_fid;
3345 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003346 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02003347
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003348 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003349 if (input_len) {
3350 req->InputBufferLength = cpu_to_le32(input_len);
3351 /* total_len for smb query request never close to le16 max */
3352 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3353 memcpy(req->Buffer, input, input_len);
3354 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003355
3356 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003357 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003358 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003359 return 0;
3360}
3361
3362void
3363SMB2_query_info_free(struct smb_rqst *rqst)
3364{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003365 if (rqst && rqst->rq_iov)
3366 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003367}
3368
3369static int
3370query_info(const unsigned int xid, struct cifs_tcon *tcon,
3371 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3372 u32 additional_info, size_t output_len, size_t min_len, void **data,
3373 u32 *dlen)
3374{
3375 struct smb_rqst rqst;
3376 struct smb2_query_info_rsp *rsp = NULL;
3377 struct kvec iov[1];
3378 struct kvec rsp_iov;
3379 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01003380 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003381 struct cifs_ses *ses = tcon->ses;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003382 struct TCP_Server_Info *server;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003383 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003384 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003385
3386 cifs_dbg(FYI, "Query Info\n");
3387
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003388 if (!ses)
3389 return -EIO;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003390 server = cifs_pick_channel(ses);
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003391 if (!server)
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003392 return -EIO;
3393
3394 if (smb3_encryption_required(tcon))
3395 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003396
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003397 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003398 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003399 rqst.rq_iov = iov;
3400 rqst.rq_nvec = 1;
3401
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003402 rc = SMB2_query_info_init(tcon, server,
3403 &rqst, persistent_fid, volatile_fid,
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003404 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003405 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003406 if (rc)
3407 goto qinf_exit;
3408
Steve Frenchd42043a2019-02-26 21:58:30 -06003409 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3410 ses->Suid, info_class, (__u32)info_type);
3411
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003412 rc = cifs_send_recv(xid, ses, server,
3413 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003414 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003415
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003416 if (rc) {
3417 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003418 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3419 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003420 goto qinf_exit;
3421 }
3422
Steve Frenchd42043a2019-02-26 21:58:30 -06003423 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3424 ses->Suid, info_class, (__u32)info_type);
3425
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003426 if (dlen) {
3427 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3428 if (!*data) {
3429 *data = kmalloc(*dlen, GFP_KERNEL);
3430 if (!*data) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003431 cifs_tcon_dbg(VFS,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003432 "Error %d allocating memory for acl\n",
3433 rc);
3434 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003435 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003436 goto qinf_exit;
3437 }
Colin Ian King73aaf922019-01-16 16:28:59 +00003438 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003439 }
3440 }
3441
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003442 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3443 le32_to_cpu(rsp->OutputBufferLength),
3444 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00003445 if (rc && allocated) {
3446 kfree(*data);
3447 *data = NULL;
3448 *dlen = 0;
3449 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003450
3451qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003452 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003453 free_rsp_buf(resp_buftype, rsp);
3454 return rc;
3455}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003456
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003457int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3458 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003459{
3460 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003461 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04003462 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003463 sizeof(struct smb2_file_all_info), (void **)&data,
3464 NULL);
3465}
3466
3467int
Steve Frenchb1bc1872020-06-11 20:23:38 -05003468SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3469 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3470{
3471 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3472 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3473 *plen = 0;
3474
3475 return query_info(xid, tcon, persistent_fid, volatile_fid,
3476 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3477 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3478}
3479
3480int
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003481SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3482 u64 persistent_fid, u64 volatile_fid,
3483 void **data, u32 *plen)
3484{
3485 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3486 *plen = 0;
3487
3488 return query_info(xid, tcon, persistent_fid, volatile_fid,
3489 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05003490 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003491}
3492
3493int
3494SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3495 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3496{
3497 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003498 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003499 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003500 sizeof(struct smb2_file_internal_info),
3501 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003502}
3503
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003504/*
Steve Frenchc3498182019-09-15 22:38:52 -05003505 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3506 * See MS-SMB2 2.2.35 and 2.2.36
3507 */
3508
zhengbin388962e2019-09-23 15:06:18 +08003509static int
Steve Frenchc3498182019-09-15 22:38:52 -05003510SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003511 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3512 u64 persistent_fid, u64 volatile_fid,
3513 u32 completion_filter, bool watch_tree)
Steve Frenchc3498182019-09-15 22:38:52 -05003514{
3515 struct smb2_change_notify_req *req;
3516 struct kvec *iov = rqst->rq_iov;
3517 unsigned int total_len;
3518 int rc;
3519
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003520 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3521 (void **) &req, &total_len);
Steve Frenchc3498182019-09-15 22:38:52 -05003522 if (rc)
3523 return rc;
3524
3525 req->PersistentFileId = persistent_fid;
3526 req->VolatileFileId = volatile_fid;
Steve Frenchd26c2dd2020-02-06 06:00:14 -06003527 /* See note 354 of MS-SMB2, 64K max */
Steve French52870d52019-10-01 21:25:46 -05003528 req->OutputBufferLength =
3529 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
Steve Frenchc3498182019-09-15 22:38:52 -05003530 req->CompletionFilter = cpu_to_le32(completion_filter);
3531 if (watch_tree)
3532 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3533 else
3534 req->Flags = 0;
3535
3536 iov[0].iov_base = (char *)req;
3537 iov[0].iov_len = total_len;
3538
3539 return 0;
3540}
3541
3542int
3543SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3544 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3545 u32 completion_filter)
3546{
3547 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003548 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Steve Frenchc3498182019-09-15 22:38:52 -05003549 struct smb_rqst rqst;
3550 struct kvec iov[1];
3551 struct kvec rsp_iov = {NULL, 0};
3552 int resp_buftype = CIFS_NO_BUFFER;
3553 int flags = 0;
3554 int rc = 0;
3555
3556 cifs_dbg(FYI, "change notify\n");
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003557 if (!ses || !server)
Steve Frenchc3498182019-09-15 22:38:52 -05003558 return -EIO;
3559
3560 if (smb3_encryption_required(tcon))
3561 flags |= CIFS_TRANSFORM_REQ;
3562
3563 memset(&rqst, 0, sizeof(struct smb_rqst));
3564 memset(&iov, 0, sizeof(iov));
3565 rqst.rq_iov = iov;
3566 rqst.rq_nvec = 1;
3567
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003568 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3569 persistent_fid, volatile_fid,
Steve Frenchc3498182019-09-15 22:38:52 -05003570 completion_filter, watch_tree);
3571 if (rc)
3572 goto cnotify_exit;
3573
3574 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3575 (u8)watch_tree, completion_filter);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003576 rc = cifs_send_recv(xid, ses, server,
3577 &rqst, &resp_buftype, flags, &rsp_iov);
Steve Frenchc3498182019-09-15 22:38:52 -05003578
3579 if (rc != 0) {
3580 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3581 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3582 (u8)watch_tree, completion_filter, rc);
3583 } else
3584 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3585 ses->Suid, (u8)watch_tree, completion_filter);
3586
3587 cnotify_exit:
3588 if (rqst.rq_iov)
3589 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3590 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3591 return rc;
3592}
3593
3594
3595
3596/*
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003597 * This is a no-op for now. We're not really interested in the reply, but
3598 * rather in the fact that the server sent one and that server->lstrp
3599 * gets updated.
3600 *
3601 * FIXME: maybe we should consider checking that the reply matches request?
3602 */
3603static void
3604smb2_echo_callback(struct mid_q_entry *mid)
3605{
3606 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003607 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003608 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003609
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003610 if (mid->mid_state == MID_RESPONSE_RECEIVED
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003611 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3612 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3613 credits.instance = server->reconnect_instance;
3614 }
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003615
3616 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003617 add_credits(server, &credits, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003618}
3619
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003620void smb2_reconnect_server(struct work_struct *work)
3621{
3622 struct TCP_Server_Info *server = container_of(work,
3623 struct TCP_Server_Info, reconnect.work);
3624 struct cifs_ses *ses;
3625 struct cifs_tcon *tcon, *tcon2;
3626 struct list_head tmp_list;
3627 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01003628 int rc;
3629 int resched = false;
3630
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003631
3632 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3633 mutex_lock(&server->reconnect_mutex);
3634
3635 INIT_LIST_HEAD(&tmp_list);
3636 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3637
3638 spin_lock(&cifs_tcp_ses_lock);
3639 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3640 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003641 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003642 tcon->tc_count++;
3643 list_add_tail(&tcon->rlist, &tmp_list);
3644 tcon_exist = true;
3645 }
3646 }
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003647 /*
3648 * IPC has the same lifetime as its session and uses its
3649 * refcount.
3650 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003651 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3652 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3653 tcon_exist = true;
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003654 ses->ses_count++;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003655 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003656 }
3657 /*
3658 * Get the reference to server struct to be sure that the last call of
3659 * cifs_put_tcon() in the loop below won't release the server pointer.
3660 */
3661 if (tcon_exist)
3662 server->srv_count++;
3663
3664 spin_unlock(&cifs_tcp_ses_lock);
3665
3666 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003667 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
Germano Percossi18ea4312017-04-07 12:29:36 +01003668 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003669 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01003670 else
3671 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003672 list_del_init(&tcon->rlist);
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003673 if (tcon->ipc)
3674 cifs_put_smb_ses(tcon->ses);
3675 else
3676 cifs_put_tcon(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003677 }
3678
3679 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01003680 if (resched)
3681 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003682 mutex_unlock(&server->reconnect_mutex);
3683
3684 /* now we can safely release srv struct */
3685 if (tcon_exist)
3686 cifs_put_tcp_session(server, 1);
3687}
3688
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003689int
3690SMB2_echo(struct TCP_Server_Info *server)
3691{
3692 struct smb2_echo_req *req;
3693 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003694 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003695 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003696 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003697 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003698
Joe Perchesf96637b2013-05-04 22:12:25 -05003699 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003700
Steve French4fcd1812016-06-22 20:12:05 -05003701 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003702 /* No need to send echo on newly established connections */
Stefan Metzmacherb08484d2020-02-24 14:14:59 +01003703 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003704 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003705 }
3706
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003707 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
3708 (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003709 if (rc)
3710 return rc;
3711
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003712 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003713
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003714 iov[0].iov_len = total_len;
3715 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003716
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003717 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003718 server, CIFS_ECHO_OP, NULL);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003719 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003720 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003721
3722 cifs_small_buf_release(req);
3723 return rc;
3724}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003725
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003726void
3727SMB2_flush_free(struct smb_rqst *rqst)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003728{
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003729 if (rqst && rqst->rq_iov)
3730 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3731}
3732
3733int
3734SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003735 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3736 u64 persistent_fid, u64 volatile_fid)
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003737{
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003738 struct smb2_flush_req *req;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003739 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003740 unsigned int total_len;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003741 int rc;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003742
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003743 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
3744 (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003745 if (rc)
3746 return rc;
3747
3748 req->PersistentFileId = persistent_fid;
3749 req->VolatileFileId = volatile_fid;
3750
3751 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003752 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003753
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003754 return 0;
3755}
3756
3757int
3758SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3759 u64 volatile_fid)
3760{
3761 struct cifs_ses *ses = tcon->ses;
3762 struct smb_rqst rqst;
3763 struct kvec iov[1];
3764 struct kvec rsp_iov = {NULL, 0};
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003765 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003766 int resp_buftype = CIFS_NO_BUFFER;
3767 int flags = 0;
3768 int rc = 0;
3769
3770 cifs_dbg(FYI, "flush\n");
3771 if (!ses || !(ses->server))
3772 return -EIO;
3773
3774 if (smb3_encryption_required(tcon))
3775 flags |= CIFS_TRANSFORM_REQ;
3776
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003777 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003778 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003779 rqst.rq_iov = iov;
3780 rqst.rq_nvec = 1;
3781
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003782 rc = SMB2_flush_init(xid, &rqst, tcon, server,
3783 persistent_fid, volatile_fid);
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003784 if (rc)
3785 goto flush_exit;
3786
Steve Frenchf90f9792019-09-03 18:35:42 -05003787 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003788 rc = cifs_send_recv(xid, ses, server,
3789 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003790
Steve Frencheccb4422018-05-17 21:16:55 -05003791 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003792 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003793 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3794 rc);
Steve Frenchf90f9792019-09-03 18:35:42 -05003795 } else
3796 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3797 ses->Suid);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003798
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003799 flush_exit:
3800 SMB2_flush_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003801 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003802 return rc;
3803}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003804
3805/*
3806 * To form a chain of read requests, any read requests after the first should
3807 * have the end_of_chain boolean set to true.
3808 */
3809static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003810smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003811 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3812 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003813{
3814 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003815 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003816 struct smb2_sync_hdr *shdr;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003817 struct TCP_Server_Info *server = io_parms->server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003818
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003819 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
3820 (void **) &req, total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003821 if (rc)
3822 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003823
Long Li2dabfd52017-11-07 01:54:53 -07003824 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003825 return -ECONNABORTED;
3826
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003827 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003828 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003829
3830 req->PersistentFileId = io_parms->persistent_fid;
3831 req->VolatileFileId = io_parms->volatile_fid;
3832 req->ReadChannelInfoOffset = 0; /* reserved */
3833 req->ReadChannelInfoLength = 0; /* reserved */
3834 req->Channel = 0; /* reserved */
3835 req->MinimumCount = 0;
3836 req->Length = cpu_to_le32(io_parms->length);
3837 req->Offset = cpu_to_le64(io_parms->offset);
Steve Frenchd323c2462019-02-25 00:52:43 -06003838
3839 trace_smb3_read_enter(0 /* xid */,
3840 io_parms->persistent_fid,
3841 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3842 io_parms->offset, io_parms->length);
Long Libd3dcc62017-11-22 17:38:47 -07003843#ifdef CONFIG_CIFS_SMB_DIRECT
3844 /*
3845 * If we want to do a RDMA write, fill in and append
3846 * smbd_buffer_descriptor_v1 to the end of read request
3847 */
Long Libb4c0412018-04-17 12:17:08 -07003848 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003849 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003850
Long Libd3dcc62017-11-22 17:38:47 -07003851 struct smbd_buffer_descriptor_v1 *v1;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003852 bool need_invalidate = server->dialect == SMB30_PROT_ID;
Long Libd3dcc62017-11-22 17:38:47 -07003853
3854 rdata->mr = smbd_register_mr(
3855 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003856 rdata->nr_pages, rdata->page_offset,
3857 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003858 if (!rdata->mr)
Long Lib7972092019-04-05 21:36:34 +00003859 return -EAGAIN;
Long Libd3dcc62017-11-22 17:38:47 -07003860
3861 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3862 if (need_invalidate)
3863 req->Channel = SMB2_CHANNEL_RDMA_V1;
3864 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003865 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003866 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003867 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003868 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003869 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3870 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3871 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003872
3873 *total_len += sizeof(*v1) - 1;
3874 }
3875#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003876 if (request_type & CHAINED_REQUEST) {
3877 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003878 /* next 8-byte aligned request */
3879 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3880 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003881 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003882 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003883 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003884 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003885 /*
3886 * Related requests use info from previous read request
3887 * in chain.
3888 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003889 shdr->SessionId = 0xFFFFFFFF;
3890 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003891 req->PersistentFileId = 0xFFFFFFFF;
3892 req->VolatileFileId = 0xFFFFFFFF;
3893 }
3894 }
3895 if (remaining_bytes > io_parms->length)
3896 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3897 else
3898 req->RemainingBytes = 0;
3899
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003900 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003901 return rc;
3902}
3903
3904static void
3905smb2_readv_callback(struct mid_q_entry *mid)
3906{
3907 struct cifs_readdata *rdata = mid->callback_data;
3908 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003909 struct TCP_Server_Info *server = rdata->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003910 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003911 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003912 struct cifs_credits credits = { .value = 0, .instance = 0 };
Steve French46f17d12019-09-04 23:07:52 -05003913 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3914 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07003915 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003916 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003917 .rq_npages = rdata->nr_pages,
3918 .rq_pagesz = rdata->pagesz,
3919 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003920
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003921 WARN_ONCE(rdata->server != mid->server,
3922 "rdata server %p != mid server %p",
3923 rdata->server, mid->server);
3924
Joe Perchesf96637b2013-05-04 22:12:25 -05003925 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3926 __func__, mid->mid, mid->mid_state, rdata->result,
3927 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003928
3929 switch (mid->mid_state) {
3930 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003931 credits.value = le16_to_cpu(shdr->CreditRequest);
3932 credits.instance = server->reconnect_instance;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003933 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003934 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003935 int rc;
3936
Jeff Layton0b688cf2012-09-18 16:20:34 -07003937 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003938 if (rc)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003939 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003940 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003941 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003942 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003943 task_io_account_read(rdata->got_bytes);
3944 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003945 break;
3946 case MID_REQUEST_SUBMITTED:
3947 case MID_RETRY_NEEDED:
3948 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003949 if (server->sign && rdata->got_bytes)
3950 /* reset bytes number since we can not check a sign */
3951 rdata->got_bytes = 0;
3952 /* FIXME: should this be counted toward the initiating task? */
3953 task_io_account_read(rdata->got_bytes);
3954 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003955 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003956 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003957 credits.value = le16_to_cpu(shdr->CreditRequest);
3958 credits.instance = server->reconnect_instance;
Miaohe Lin30b5ae22020-08-08 16:36:37 +08003959 fallthrough;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003960 default:
Pavel Shilovsky6b15eb18c2019-01-18 15:46:14 -08003961 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003962 }
Long Libd3dcc62017-11-22 17:38:47 -07003963#ifdef CONFIG_CIFS_SMB_DIRECT
3964 /*
3965 * If this rdata has a memmory registered, the MR can be freed
3966 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3967 * because they have limited number and are used for future I/Os
3968 */
3969 if (rdata->mr) {
3970 smbd_deregister_mr(rdata->mr);
3971 rdata->mr = NULL;
3972 }
3973#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08003974 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003975 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003976 trace_smb3_read_err(0 /* xid */,
3977 rdata->cfile->fid.persistent_fid,
3978 tcon->tid, tcon->ses->Suid, rdata->offset,
3979 rdata->bytes, rdata->result);
3980 } else
3981 trace_smb3_read_done(0 /* xid */,
3982 rdata->cfile->fid.persistent_fid,
3983 tcon->tid, tcon->ses->Suid,
3984 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003985
3986 queue_work(cifsiod_wq, &rdata->work);
3987 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003988 add_credits(server, &credits, 0);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003989}
3990
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003991/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003992int
3993smb2_async_readv(struct cifs_readdata *rdata)
3994{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003995 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003996 char *buf;
3997 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003998 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003999 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004000 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04004001 struct TCP_Server_Info *server;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004002 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004003 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004004
Joe Perchesf96637b2013-05-04 22:12:25 -05004005 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4006 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004007
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004008 if (!rdata->server)
4009 rdata->server = cifs_pick_channel(tcon->ses);
4010
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004011 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004012 io_parms.server = server = rdata->server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004013 io_parms.offset = rdata->offset;
4014 io_parms.length = rdata->bytes;
4015 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4016 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4017 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04004018
Long Li2dabfd52017-11-07 01:54:53 -07004019 rc = smb2_new_read_req(
4020 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08004021 if (rc)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004022 return rc;
4023
Steve French5a77e752018-05-09 17:43:08 -05004024 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004025 flags |= CIFS_TRANSFORM_REQ;
4026
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004027 rdata->iov[0].iov_base = buf;
4028 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08004029
4030 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004031
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004032 if (rdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004033 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04004034 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00004035 shdr->CreditRequest =
4036 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004037
4038 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4039 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004040 goto async_readv_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004041
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004042 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04004043 }
4044
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004045 kref_get(&rdata->refcount);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004046 rc = cifs_call_async(server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004047 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08004048 smb3_handle_read_data, rdata, flags,
4049 &rdata->credits);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004050 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004051 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004052 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004053 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4054 io_parms.tcon->tid,
4055 io_parms.tcon->ses->Suid,
4056 io_parms.offset, io_parms.length, rc);
4057 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004058
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004059async_readv_out:
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004060 cifs_small_buf_release(buf);
4061 return rc;
4062}
Pavel Shilovsky33319142012-09-18 16:20:29 -07004063
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004064int
4065SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4066 unsigned int *nbytes, char **buf, int *buf_type)
4067{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004068 struct smb_rqst rqst;
Colin Ian King1efd4fc2019-07-31 10:05:26 +01004069 int resp_buftype, rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08004070 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004071 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004072 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004073 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004074 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004075 int flags = CIFS_LOG_ERROR;
4076 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004077
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004078 if (!io_parms->server)
4079 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4080
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004081 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07004082 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004083 if (rc)
4084 return rc;
4085
Steve French5a77e752018-05-09 17:43:08 -05004086 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004087 flags |= CIFS_TRANSFORM_REQ;
4088
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004089 iov[0].iov_base = (char *)req;
4090 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004091
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004092 memset(&rqst, 0, sizeof(struct smb_rqst));
4093 rqst.rq_iov = iov;
4094 rqst.rq_nvec = 1;
4095
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004096 rc = cifs_send_recv(xid, ses, io_parms->server,
4097 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004098 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004099
4100 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004101 if (rc != -ENODATA) {
4102 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4103 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004104 trace_smb3_read_err(xid, req->PersistentFileId,
4105 io_parms->tcon->tid, ses->Suid,
4106 io_parms->offset, io_parms->length,
4107 rc);
Steve Frenchb0a42f22019-02-25 15:02:58 -06004108 } else
4109 trace_smb3_read_done(xid, req->PersistentFileId,
4110 io_parms->tcon->tid, ses->Suid,
4111 io_parms->offset, 0);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004112 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Ronnie Sahlberg05fd5c22019-04-23 16:39:45 +10004113 cifs_small_buf_release(req);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004114 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05004115 } else
4116 trace_smb3_read_done(xid, req->PersistentFileId,
4117 io_parms->tcon->tid, ses->Suid,
4118 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004119
ZhangXiaoxu088aaf12019-04-06 15:47:39 +08004120 cifs_small_buf_release(req);
4121
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004122 *nbytes = le32_to_cpu(rsp->DataLength);
4123 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4124 (*nbytes > io_parms->length)) {
4125 cifs_dbg(FYI, "bad length %d for count %d\n",
4126 *nbytes, io_parms->length);
4127 rc = -EIO;
4128 *nbytes = 0;
4129 }
4130
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004131 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004132 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004133 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004134 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004135 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004136 if (resp_buftype == CIFS_SMALL_BUFFER)
4137 *buf_type = CIFS_SMALL_BUFFER;
4138 else if (resp_buftype == CIFS_LARGE_BUFFER)
4139 *buf_type = CIFS_LARGE_BUFFER;
4140 }
4141 return rc;
4142}
4143
Pavel Shilovsky33319142012-09-18 16:20:29 -07004144/*
4145 * Check the mid_state and signature on received buffer (if any), and queue the
4146 * workqueue completion task.
4147 */
4148static void
4149smb2_writev_callback(struct mid_q_entry *mid)
4150{
4151 struct cifs_writedata *wdata = mid->callback_data;
4152 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004153 struct TCP_Server_Info *server = wdata->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004154 unsigned int written;
4155 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004156 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky33319142012-09-18 16:20:29 -07004157
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004158 WARN_ONCE(wdata->server != mid->server,
4159 "wdata server %p != mid server %p",
4160 wdata->server, mid->server);
4161
Pavel Shilovsky33319142012-09-18 16:20:29 -07004162 switch (mid->mid_state) {
4163 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004164 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4165 credits.instance = server->reconnect_instance;
4166 wdata->result = smb2_check_receive(mid, server, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004167 if (wdata->result != 0)
4168 break;
4169
4170 written = le32_to_cpu(rsp->DataLength);
4171 /*
4172 * Mask off high 16 bits when bytes written as returned
4173 * by the server is greater than bytes requested by the
4174 * client. OS/2 servers are known to set incorrect
4175 * CountHigh values.
4176 */
4177 if (written > wdata->bytes)
4178 written &= 0xFFFF;
4179
4180 if (written < wdata->bytes)
4181 wdata->result = -ENOSPC;
4182 else
4183 wdata->bytes = written;
4184 break;
4185 case MID_REQUEST_SUBMITTED:
4186 case MID_RETRY_NEEDED:
4187 wdata->result = -EAGAIN;
4188 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08004189 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004190 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4191 credits.instance = server->reconnect_instance;
Miaohe Lin30b5ae22020-08-08 16:36:37 +08004192 fallthrough;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004193 default:
4194 wdata->result = -EIO;
4195 break;
4196 }
Long Lidb223a52017-11-22 17:38:45 -07004197#ifdef CONFIG_CIFS_SMB_DIRECT
4198 /*
4199 * If this wdata has a memory registered, the MR can be freed
4200 * The number of MRs available is limited, it's important to recover
4201 * used MR as soon as I/O is finished. Hold MR longer in the later
4202 * I/O process can possibly result in I/O deadlock due to lack of MR
4203 * to send request on I/O retry
4204 */
4205 if (wdata->mr) {
4206 smbd_deregister_mr(wdata->mr);
4207 wdata->mr = NULL;
4208 }
4209#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004210 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07004211 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004212 trace_smb3_write_err(0 /* no xid */,
4213 wdata->cfile->fid.persistent_fid,
4214 tcon->tid, tcon->ses->Suid, wdata->offset,
4215 wdata->bytes, wdata->result);
Steve Frenchd6fd4192020-02-05 16:52:11 -06004216 if (wdata->result == -ENOSPC)
Joe Perchesa0a30362020-04-14 22:42:53 -07004217 pr_warn_once("Out of space writing to %s\n",
4218 tcon->treeName);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004219 } else
4220 trace_smb3_write_done(0 /* no xid */,
4221 wdata->cfile->fid.persistent_fid,
4222 tcon->tid, tcon->ses->Suid,
4223 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004224
4225 queue_work(cifsiod_wq, &wdata->work);
4226 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004227 add_credits(server, &credits, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004228}
4229
4230/* smb2_async_writev - send an async write, and set up mid to handle result */
4231int
Steve French4a5c80d2014-02-07 20:45:12 -06004232smb2_async_writev(struct cifs_writedata *wdata,
4233 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07004234{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004235 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004236 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004237 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004238 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004239 struct TCP_Server_Info *server = wdata->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004240 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004241 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004242 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004243
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004244 if (!wdata->server)
4245 server = wdata->server = cifs_pick_channel(tcon->ses);
4246
4247 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4248 (void **) &req, &total_len);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08004249 if (rc)
4250 return rc;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004251
Steve French5a77e752018-05-09 17:43:08 -05004252 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004253 flags |= CIFS_TRANSFORM_REQ;
4254
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004255 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004256 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004257
4258 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4259 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4260 req->WriteChannelInfoOffset = 0;
4261 req->WriteChannelInfoLength = 0;
4262 req->Channel = 0;
4263 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004264 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004265 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07004266 req->RemainingBytes = 0;
Steve Frenchd323c2462019-02-25 00:52:43 -06004267
4268 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4269 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07004270#ifdef CONFIG_CIFS_SMB_DIRECT
4271 /*
4272 * If we want to do a server RDMA read, fill in and append
4273 * smbd_buffer_descriptor_v1 to the end of write request
4274 */
Long Libb4c0412018-04-17 12:17:08 -07004275 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07004276 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07004277
Long Lidb223a52017-11-22 17:38:45 -07004278 struct smbd_buffer_descriptor_v1 *v1;
4279 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4280
4281 wdata->mr = smbd_register_mr(
4282 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07004283 wdata->nr_pages, wdata->page_offset,
4284 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07004285 if (!wdata->mr) {
Long Lib7972092019-04-05 21:36:34 +00004286 rc = -EAGAIN;
Long Lidb223a52017-11-22 17:38:45 -07004287 goto async_writev_out;
4288 }
4289 req->Length = 0;
4290 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07004291 if (wdata->nr_pages > 1)
4292 req->RemainingBytes =
4293 cpu_to_le32(
4294 (wdata->nr_pages - 1) * wdata->pagesz -
4295 wdata->page_offset + wdata->tailsz
4296 );
4297 else
4298 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07004299 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4300 if (need_invalidate)
4301 req->Channel = SMB2_CHANNEL_RDMA_V1;
4302 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06004303 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07004304 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06004305 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07004306 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06004307 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4308 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4309 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07004310 }
4311#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004312 iov[0].iov_len = total_len - 1;
4313 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004314
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004315 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004316 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07004317 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07004318 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07004319 rqst.rq_npages = wdata->nr_pages;
4320 rqst.rq_pagesz = wdata->pagesz;
4321 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07004322#ifdef CONFIG_CIFS_SMB_DIRECT
4323 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004324 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07004325 rqst.rq_npages = 0;
4326 }
4327#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05004328 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4329 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004330
Long Lidb223a52017-11-22 17:38:45 -07004331#ifdef CONFIG_CIFS_SMB_DIRECT
4332 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4333 if (!wdata->mr)
4334 req->Length = cpu_to_le32(wdata->bytes);
4335#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07004336 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07004337#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07004338
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004339 if (wdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004340 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004341 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00004342 shdr->CreditRequest =
4343 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004344
4345 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4346 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004347 goto async_writev_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004348
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004349 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004350 }
4351
Pavel Shilovsky33319142012-09-18 16:20:29 -07004352 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08004353 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08004354 wdata, flags, &wdata->credits);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004355
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004356 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004357 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4358 tcon->tid, tcon->ses->Suid, wdata->offset,
4359 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06004360 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004361 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004362 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07004363
Pavel Shilovsky33319142012-09-18 16:20:29 -07004364async_writev_out:
4365 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004366 return rc;
4367}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004368
4369/*
4370 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4371 * The length field from io_parms must be at least 1 and indicates a number of
4372 * elements with data to write that begins with position 1 in iov array. All
4373 * data length is specified by count.
4374 */
4375int
4376SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4377 unsigned int *nbytes, struct kvec *iov, int n_vec)
4378{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004379 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004380 int rc = 0;
4381 struct smb2_write_req *req = NULL;
4382 struct smb2_write_rsp *rsp = NULL;
4383 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004384 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004385 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004386 unsigned int total_len;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004387 struct TCP_Server_Info *server;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004388
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004389 *nbytes = 0;
4390
4391 if (n_vec < 1)
4392 return rc;
4393
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004394 if (!io_parms->server)
4395 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4396 server = io_parms->server;
4397 if (server == NULL)
4398 return -ECONNABORTED;
4399
4400 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4401 (void **) &req, &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004402 if (rc)
4403 return rc;
4404
Steve French5a77e752018-05-09 17:43:08 -05004405 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004406 flags |= CIFS_TRANSFORM_REQ;
4407
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004408 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004409
4410 req->PersistentFileId = io_parms->persistent_fid;
4411 req->VolatileFileId = io_parms->volatile_fid;
4412 req->WriteChannelInfoOffset = 0;
4413 req->WriteChannelInfoLength = 0;
4414 req->Channel = 0;
4415 req->Length = cpu_to_le32(io_parms->length);
4416 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004417 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004418 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004419 req->RemainingBytes = 0;
4420
Steve Frenchd323c2462019-02-25 00:52:43 -06004421 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4422 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4423 io_parms->offset, io_parms->length);
4424
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004425 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004426 /* 1 for Buffer */
4427 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004428
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004429 memset(&rqst, 0, sizeof(struct smb_rqst));
4430 rqst.rq_iov = iov;
4431 rqst.rq_nvec = n_vec + 1;
4432
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004433 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4434 &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004435 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004436 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004437
4438 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004439 trace_smb3_write_err(xid, req->PersistentFileId,
4440 io_parms->tcon->tid,
4441 io_parms->tcon->ses->Suid,
4442 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004443 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004444 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05004445 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004446 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05004447 trace_smb3_write_done(xid, req->PersistentFileId,
4448 io_parms->tcon->tid,
4449 io_parms->tcon->ses->Suid,
4450 io_parms->offset, *nbytes);
4451 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004452
ZhangXiaoxu6a3eb332019-04-06 15:47:38 +08004453 cifs_small_buf_release(req);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004454 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004455 return rc;
4456}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004457
Aurelien Aptel69dda302020-03-02 17:53:22 +01004458int posix_info_sid_size(const void *beg, const void *end)
Aurelien Aptel349e13a2020-02-08 15:50:57 +01004459{
4460 size_t subauth;
4461 int total;
4462
4463 if (beg + 1 > end)
4464 return -1;
4465
4466 subauth = *(u8 *)(beg+1);
4467 if (subauth < 1 || subauth > 15)
4468 return -1;
4469
4470 total = 1 + 1 + 6 + 4*subauth;
4471 if (beg + total > end)
4472 return -1;
4473
4474 return total;
4475}
4476
4477int posix_info_parse(const void *beg, const void *end,
4478 struct smb2_posix_info_parsed *out)
4479
4480{
4481 int total_len = 0;
4482 int sid_len;
4483 int name_len;
4484 const void *owner_sid;
4485 const void *group_sid;
4486 const void *name;
4487
4488 /* if no end bound given, assume payload to be correct */
4489 if (!end) {
4490 const struct smb2_posix_info *p = beg;
4491
4492 end = beg + le32_to_cpu(p->NextEntryOffset);
4493 /* last element will have a 0 offset, pick a sensible bound */
4494 if (end == beg)
4495 end += 0xFFFF;
4496 }
4497
4498 /* check base buf */
4499 if (beg + sizeof(struct smb2_posix_info) > end)
4500 return -1;
4501 total_len = sizeof(struct smb2_posix_info);
4502
4503 /* check owner sid */
4504 owner_sid = beg + total_len;
4505 sid_len = posix_info_sid_size(owner_sid, end);
4506 if (sid_len < 0)
4507 return -1;
4508 total_len += sid_len;
4509
4510 /* check group sid */
4511 group_sid = beg + total_len;
4512 sid_len = posix_info_sid_size(group_sid, end);
4513 if (sid_len < 0)
4514 return -1;
4515 total_len += sid_len;
4516
4517 /* check name len */
4518 if (beg + total_len + 4 > end)
4519 return -1;
4520 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4521 if (name_len < 1 || name_len > 0xFFFF)
4522 return -1;
4523 total_len += 4;
4524
4525 /* check name */
4526 name = beg + total_len;
4527 if (name + name_len > end)
4528 return -1;
4529 total_len += name_len;
4530
4531 if (out) {
4532 out->base = beg;
4533 out->size = total_len;
4534 out->name_len = name_len;
4535 out->name = name;
4536 memcpy(&out->owner, owner_sid,
4537 posix_info_sid_size(owner_sid, end));
4538 memcpy(&out->group, group_sid,
4539 posix_info_sid_size(group_sid, end));
4540 }
4541 return total_len;
4542}
4543
4544static int posix_info_extra_size(const void *beg, const void *end)
4545{
4546 int len = posix_info_parse(beg, end, NULL);
4547
4548 if (len < 0)
4549 return -1;
4550 return len - sizeof(struct smb2_posix_info);
4551}
4552
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004553static unsigned int
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004554num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4555 size_t size)
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004556{
4557 int len;
4558 unsigned int entrycount = 0;
4559 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03004560 char *entryptr;
4561 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004562
4563 if (bufstart == NULL)
4564 return 0;
4565
Dan Carpenter56446f22018-09-06 12:48:22 +03004566 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004567
4568 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03004569 if (entryptr + next_offset < entryptr ||
4570 entryptr + next_offset > end_of_buf ||
4571 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004572 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004573 break;
4574 }
4575
Dan Carpenter56446f22018-09-06 12:48:22 +03004576 entryptr = entryptr + next_offset;
4577 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4578
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004579 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4580 len = posix_info_extra_size(entryptr, end_of_buf);
4581 else
4582 len = le32_to_cpu(dir_info->FileNameLength);
4583
4584 if (len < 0 ||
4585 entryptr + len < entryptr ||
Dan Carpenter56446f22018-09-06 12:48:22 +03004586 entryptr + len > end_of_buf ||
4587 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004588 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4589 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004590 break;
4591 }
4592
Dan Carpenter56446f22018-09-06 12:48:22 +03004593 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004594 entrycount++;
4595
Dan Carpenter56446f22018-09-06 12:48:22 +03004596 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004597 if (!next_offset)
4598 break;
4599 }
4600
4601 return entrycount;
4602}
4603
4604/*
4605 * Readdir/FindFirst
4606 */
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004607int SMB2_query_directory_init(const unsigned int xid,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004608 struct cifs_tcon *tcon,
4609 struct TCP_Server_Info *server,
4610 struct smb_rqst *rqst,
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004611 u64 persistent_fid, u64 volatile_fid,
4612 int index, int info_level)
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004613{
4614 struct smb2_query_directory_req *req;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004615 unsigned char *bufptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004616 __le16 asteriks = cpu_to_le16('*');
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004617 unsigned int output_size = CIFSMaxBufSize -
4618 MAX_SMB2_CREATE_RESPONSE_SIZE -
4619 MAX_SMB2_CLOSE_RESPONSE_SIZE;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004620 unsigned int total_len;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004621 struct kvec *iov = rqst->rq_iov;
4622 int len, rc;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004623
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004624 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4625 (void **) &req, &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004626 if (rc)
4627 return rc;
4628
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004629 switch (info_level) {
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004630 case SMB_FIND_FILE_DIRECTORY_INFO:
4631 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004632 break;
4633 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4634 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004635 break;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004636 case SMB_FIND_FILE_POSIX_INFO:
4637 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4638 break;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004639 default:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004640 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004641 info_level);
4642 return -EINVAL;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004643 }
4644
4645 req->FileIndex = cpu_to_le32(index);
4646 req->PersistentFileId = persistent_fid;
4647 req->VolatileFileId = volatile_fid;
4648
4649 len = 0x2;
4650 bufptr = req->Buffer;
4651 memcpy(bufptr, &asteriks, len);
4652
4653 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004654 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004655 req->FileNameLength = cpu_to_le16(len);
4656 /*
4657 * BB could be 30 bytes or so longer if we used SMB2 specific
4658 * buffer lengths, but this is safe and close enough.
4659 */
4660 output_size = min_t(unsigned int, output_size, server->maxBuf);
4661 output_size = min_t(unsigned int, output_size, 2 << 15);
4662 req->OutputBufferLength = cpu_to_le32(output_size);
4663
4664 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004665 /* 1 for Buffer */
4666 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004667
4668 iov[1].iov_base = (char *)(req->Buffer);
4669 iov[1].iov_len = len;
4670
Steve Frenchd323c2462019-02-25 00:52:43 -06004671 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4672 tcon->ses->Suid, index, output_size);
4673
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004674 return 0;
4675}
4676
4677void SMB2_query_directory_free(struct smb_rqst *rqst)
4678{
4679 if (rqst && rqst->rq_iov) {
4680 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4681 }
4682}
4683
4684int
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004685smb2_parse_query_directory(struct cifs_tcon *tcon,
4686 struct kvec *rsp_iov,
4687 int resp_buftype,
4688 struct cifs_search_info *srch_inf)
4689{
4690 struct smb2_query_directory_rsp *rsp;
4691 size_t info_buf_size;
4692 char *end_of_smb;
4693 int rc;
4694
4695 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4696
4697 switch (srch_inf->info_level) {
4698 case SMB_FIND_FILE_DIRECTORY_INFO:
4699 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4700 break;
4701 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4702 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4703 break;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004704 case SMB_FIND_FILE_POSIX_INFO:
4705 /* note that posix payload are variable size */
4706 info_buf_size = sizeof(struct smb2_posix_info);
4707 break;
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004708 default:
4709 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4710 srch_inf->info_level);
4711 return -EINVAL;
4712 }
4713
4714 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4715 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4716 info_buf_size);
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004717 if (rc) {
4718 cifs_tcon_dbg(VFS, "bad info payload");
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004719 return rc;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004720 }
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004721
4722 srch_inf->unicode = true;
4723
4724 if (srch_inf->ntwrk_buf_start) {
4725 if (srch_inf->smallBuf)
4726 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4727 else
4728 cifs_buf_release(srch_inf->ntwrk_buf_start);
4729 }
4730 srch_inf->ntwrk_buf_start = (char *)rsp;
4731 srch_inf->srch_entries_start = srch_inf->last_entry =
4732 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4733 end_of_smb = rsp_iov->iov_len + (char *)rsp;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004734
4735 srch_inf->entries_in_buffer = num_entries(
4736 srch_inf->info_level,
4737 srch_inf->srch_entries_start,
4738 end_of_smb,
4739 &srch_inf->last_entry,
4740 info_buf_size);
4741
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004742 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4743 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4744 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4745 srch_inf->srch_entries_start, srch_inf->last_entry);
4746 if (resp_buftype == CIFS_LARGE_BUFFER)
4747 srch_inf->smallBuf = false;
4748 else if (resp_buftype == CIFS_SMALL_BUFFER)
4749 srch_inf->smallBuf = true;
4750 else
Joe Perchesa0a30362020-04-14 22:42:53 -07004751 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004752
4753 return 0;
4754}
4755
4756int
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004757SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4758 u64 persistent_fid, u64 volatile_fid, int index,
4759 struct cifs_search_info *srch_inf)
4760{
4761 struct smb_rqst rqst;
4762 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4763 struct smb2_query_directory_rsp *rsp = NULL;
4764 int resp_buftype = CIFS_NO_BUFFER;
4765 struct kvec rsp_iov;
4766 int rc = 0;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004767 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004768 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004769 int flags = 0;
4770
YueHaibingc4985c32020-01-17 10:57:17 +08004771 if (!ses || !(ses->server))
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004772 return -EIO;
4773
4774 if (smb3_encryption_required(tcon))
4775 flags |= CIFS_TRANSFORM_REQ;
4776
4777 memset(&rqst, 0, sizeof(struct smb_rqst));
4778 memset(&iov, 0, sizeof(iov));
4779 rqst.rq_iov = iov;
4780 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4781
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004782 rc = SMB2_query_directory_init(xid, tcon, server,
4783 &rqst, persistent_fid,
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004784 volatile_fid, index,
4785 srch_inf->info_level);
4786 if (rc)
4787 goto qdir_exit;
4788
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004789 rc = cifs_send_recv(xid, ses, server,
4790 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004791 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004792
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004793 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004794 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004795 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004796 trace_smb3_query_dir_done(xid, persistent_fid,
4797 tcon->tid, tcon->ses->Suid, index, 0);
Pavel Shilovsky52755802014-08-18 20:49:57 +04004798 srch_inf->endOfSearch = true;
4799 rc = 0;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004800 } else {
4801 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4802 tcon->ses->Suid, index, 0, rc);
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08004803 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004804 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004805 goto qdir_exit;
4806 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004807
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004808 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4809 srch_inf);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004810 if (rc) {
4811 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4812 tcon->ses->Suid, index, 0, rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004813 goto qdir_exit;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004814 }
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004815 resp_buftype = CIFS_NO_BUFFER;
4816
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004817 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4818 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004819
4820qdir_exit:
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004821 SMB2_query_directory_free(&rqst);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004822 free_rsp_buf(resp_buftype, rsp);
4823 return rc;
4824}
4825
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004826int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004827SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4828 struct smb_rqst *rqst,
4829 u64 persistent_fid, u64 volatile_fid, u32 pid,
4830 u8 info_class, u8 info_type, u32 additional_info,
4831 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004832{
4833 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004834 struct kvec *iov = rqst->rq_iov;
4835 unsigned int i, total_len;
4836 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004837
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004838 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
4839 (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004840 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004841 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004842
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004843 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004844 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004845 req->FileInfoClass = info_class;
4846 req->PersistentFileId = persistent_fid;
4847 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004848 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004849
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004850 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004851 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004852 req->BufferLength = cpu_to_le32(*size);
4853
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004854 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004855 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004856
4857 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004858 /* 1 for Buffer */
4859 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004860
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004861 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004862 le32_add_cpu(&req->BufferLength, size[i]);
4863 iov[i].iov_base = (char *)data[i];
4864 iov[i].iov_len = size[i];
4865 }
4866
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004867 return 0;
4868}
4869
4870void
4871SMB2_set_info_free(struct smb_rqst *rqst)
4872{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10004873 if (rqst && rqst->rq_iov)
4874 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004875}
4876
4877static int
4878send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4879 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4880 u8 info_type, u32 additional_info, unsigned int num,
4881 void **data, unsigned int *size)
4882{
4883 struct smb_rqst rqst;
4884 struct smb2_set_info_rsp *rsp = NULL;
4885 struct kvec *iov;
4886 struct kvec rsp_iov;
4887 int rc = 0;
4888 int resp_buftype;
4889 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004890 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004891 int flags = 0;
4892
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004893 if (!ses || !server)
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004894 return -EIO;
4895
4896 if (!num)
4897 return -EINVAL;
4898
4899 if (smb3_encryption_required(tcon))
4900 flags |= CIFS_TRANSFORM_REQ;
4901
4902 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4903 if (!iov)
4904 return -ENOMEM;
4905
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004906 memset(&rqst, 0, sizeof(struct smb_rqst));
4907 rqst.rq_iov = iov;
4908 rqst.rq_nvec = num;
4909
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004910 rc = SMB2_set_info_init(tcon, server,
4911 &rqst, persistent_fid, volatile_fid, pid,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004912 info_class, info_type, additional_info,
4913 data, size);
4914 if (rc) {
4915 kfree(iov);
4916 return rc;
4917 }
4918
4919
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004920 rc = cifs_send_recv(xid, ses, server,
4921 &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004922 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004923 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004924 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004925
Steve Frencheccb4422018-05-17 21:16:55 -05004926 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004927 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004928 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4929 ses->Suid, info_class, (__u32)info_type, rc);
4930 }
Steve French7d3fb242013-11-18 09:56:28 -06004931
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004932 free_rsp_buf(resp_buftype, rsp);
4933 kfree(iov);
4934 return rc;
4935}
4936
4937int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004938SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004939 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004940{
4941 struct smb2_file_eof_info info;
4942 void *data;
4943 unsigned int size;
4944
4945 info.EndOfFile = *eof;
4946
4947 data = &info;
4948 size = sizeof(struct smb2_file_eof_info);
4949
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004950 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004951 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4952 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004953}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004954
4955int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004956SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4957 u64 persistent_fid, u64 volatile_fid,
4958 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4959{
4960 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4961 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4962 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004963}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004964
4965int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004966SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4967 u64 persistent_fid, u64 volatile_fid,
4968 struct smb2_file_full_ea_info *buf, int len)
4969{
4970 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4971 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4972 0, 1, (void **)&buf, &len);
4973}
4974
4975int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004976SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4977 const u64 persistent_fid, const u64 volatile_fid,
4978 __u8 oplock_level)
4979{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004980 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004981 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10004982 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004983 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004984 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004985 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004986 unsigned int total_len;
4987 struct kvec iov[1];
4988 struct kvec rsp_iov;
4989 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004990
Joe Perchesf96637b2013-05-04 22:12:25 -05004991 cifs_dbg(FYI, "SMB2_oplock_break\n");
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004992 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
4993 (void **) &req, &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004994 if (rc)
4995 return rc;
4996
Steve French5a77e752018-05-09 17:43:08 -05004997 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004998 flags |= CIFS_TRANSFORM_REQ;
4999
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005000 req->VolatileFid = volatile_fid;
5001 req->PersistentFid = persistent_fid;
5002 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11005003 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005004
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10005005 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11005006
5007 iov[0].iov_base = (char *)req;
5008 iov[0].iov_len = total_len;
5009
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005010 memset(&rqst, 0, sizeof(struct smb_rqst));
5011 rqst.rq_iov = iov;
5012 rqst.rq_nvec = 1;
5013
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005014 rc = cifs_send_recv(xid, ses, server,
5015 &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005016 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005017
5018 if (rc) {
5019 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05005020 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005021 }
5022
5023 return rc;
5024}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005025
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005026void
5027smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5028 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005029{
5030 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5031 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5032 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05305033 kst->f_bfree = kst->f_bavail =
5034 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005035 return;
5036}
5037
Steve French2d304212018-06-24 23:28:12 -05005038static void
5039copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5040 struct kstatfs *kst)
5041{
5042 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5043 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5044 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5045 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5046 kst->f_bavail = kst->f_bfree;
5047 else
5048 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5049 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5050 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5051 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5052 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5053
5054 return;
5055}
Steve French2d304212018-06-24 23:28:12 -05005056
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005057static int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005058build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5059 struct TCP_Server_Info *server,
5060 int level, int outbuf_len, u64 persistent_fid,
5061 u64 volatile_fid)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005062{
5063 int rc;
5064 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11005065 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005066
Joe Perchesf96637b2013-05-04 22:12:25 -05005067 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005068
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005069 if ((tcon->ses == NULL) || server == NULL)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005070 return -EIO;
5071
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005072 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5073 (void **) &req, &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005074 if (rc)
5075 return rc;
5076
5077 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5078 req->FileInfoClass = level;
5079 req->PersistentFileId = persistent_fid;
5080 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11005081 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005082 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11005083 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005084 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005085 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005086
5087 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11005088 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005089 return 0;
5090}
5091
Steve French2d304212018-06-24 23:28:12 -05005092int
5093SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5094 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5095{
5096 struct smb_rqst rqst;
5097 struct smb2_query_info_rsp *rsp = NULL;
5098 struct kvec iov;
5099 struct kvec rsp_iov;
5100 int rc = 0;
5101 int resp_buftype;
5102 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005103 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Steve French2d304212018-06-24 23:28:12 -05005104 FILE_SYSTEM_POSIX_INFO *info = NULL;
5105 int flags = 0;
5106
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005107 rc = build_qfs_info_req(&iov, tcon, server,
5108 FS_POSIX_INFORMATION,
Steve French2d304212018-06-24 23:28:12 -05005109 sizeof(FILE_SYSTEM_POSIX_INFO),
5110 persistent_fid, volatile_fid);
5111 if (rc)
5112 return rc;
5113
5114 if (smb3_encryption_required(tcon))
5115 flags |= CIFS_TRANSFORM_REQ;
5116
5117 memset(&rqst, 0, sizeof(struct smb_rqst));
5118 rqst.rq_iov = &iov;
5119 rqst.rq_nvec = 1;
5120
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005121 rc = cifs_send_recv(xid, ses, server,
5122 &rqst, &resp_buftype, flags, &rsp_iov);
Steve French2d304212018-06-24 23:28:12 -05005123 cifs_small_buf_release(iov.iov_base);
5124 if (rc) {
5125 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5126 goto posix_qfsinf_exit;
5127 }
5128 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5129
5130 info = (FILE_SYSTEM_POSIX_INFO *)(
5131 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005132 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5133 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5134 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05005135 if (!rc)
5136 copy_posix_fs_info_to_kstatfs(info, fsdata);
5137
5138posix_qfsinf_exit:
5139 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5140 return rc;
5141}
Steve French2d304212018-06-24 23:28:12 -05005142
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005143int
5144SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5145 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5146{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005147 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005148 struct smb2_query_info_rsp *rsp = NULL;
5149 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005150 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005151 int rc = 0;
5152 int resp_buftype;
5153 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005154 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005155 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005156 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005157
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005158 rc = build_qfs_info_req(&iov, tcon, server,
5159 FS_FULL_SIZE_INFORMATION,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005160 sizeof(struct smb2_fs_full_size_info),
5161 persistent_fid, volatile_fid);
5162 if (rc)
5163 return rc;
5164
Steve French5a77e752018-05-09 17:43:08 -05005165 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005166 flags |= CIFS_TRANSFORM_REQ;
5167
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005168 memset(&rqst, 0, sizeof(struct smb_rqst));
5169 rqst.rq_iov = &iov;
5170 rqst.rq_nvec = 1;
5171
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005172 rc = cifs_send_recv(xid, ses, server,
5173 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005174 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005175 if (rc) {
5176 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05005177 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005178 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005179 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005180
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005181 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10005182 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005183 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5184 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5185 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005186 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005187 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005188
Steve French34f62642013-10-09 02:07:00 -05005189qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005190 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05005191 return rc;
5192}
5193
5194int
5195SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05005196 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05005197{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005198 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05005199 struct smb2_query_info_rsp *rsp = NULL;
5200 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005201 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05005202 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05005203 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05005204 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005205 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Steve French34f62642013-10-09 02:07:00 -05005206 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005207 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05005208
Steven French21671142013-10-09 13:36:35 -05005209 if (level == FS_DEVICE_INFORMATION) {
5210 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5211 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5212 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5213 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5214 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005215 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5216 max_len = sizeof(struct smb3_fs_ss_info);
5217 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05005218 } else if (level == FS_VOLUME_INFORMATION) {
5219 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5220 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05005221 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005222 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05005223 return -EINVAL;
5224 }
5225
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005226 rc = build_qfs_info_req(&iov, tcon, server,
5227 level, max_len,
Steve French34f62642013-10-09 02:07:00 -05005228 persistent_fid, volatile_fid);
5229 if (rc)
5230 return rc;
5231
Steve French5a77e752018-05-09 17:43:08 -05005232 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005233 flags |= CIFS_TRANSFORM_REQ;
5234
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005235 memset(&rqst, 0, sizeof(struct smb_rqst));
5236 rqst.rq_iov = &iov;
5237 rqst.rq_nvec = 1;
5238
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005239 rc = cifs_send_recv(xid, ses, server,
5240 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005241 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05005242 if (rc) {
5243 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5244 goto qfsattr_exit;
5245 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005246 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05005247
5248 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5249 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005250 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05005251 if (rc)
5252 goto qfsattr_exit;
5253
5254 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005255 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10005256 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05005257 rsp_len, max_len));
5258 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005259 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10005260 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005261 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5262 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005263 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005264 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5265 tcon->perf_sector_size =
5266 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05005267 } else if (level == FS_VOLUME_INFORMATION) {
5268 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5269 (offset + (char *)rsp);
5270 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5271 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005272 }
Steve French34f62642013-10-09 02:07:00 -05005273
5274qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005275 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005276 return rc;
5277}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005278
5279int
5280smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5281 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5282 const __u32 num_lock, struct smb2_lock_element *buf)
5283{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005284 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005285 int rc = 0;
5286 struct smb2_lock_req *req = NULL;
5287 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005288 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005289 int resp_buf_type;
5290 unsigned int count;
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10005291 int flags = CIFS_NO_RSP_BUF;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005292 unsigned int total_len;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005293 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005294
Joe Perchesf96637b2013-05-04 22:12:25 -05005295 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005296
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005297 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5298 (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005299 if (rc)
5300 return rc;
5301
Steve French5a77e752018-05-09 17:43:08 -05005302 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005303 flags |= CIFS_TRANSFORM_REQ;
5304
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005305 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005306 req->LockCount = cpu_to_le16(num_lock);
5307
5308 req->PersistentFileId = persist_fid;
5309 req->VolatileFileId = volatile_fid;
5310
5311 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005312
5313 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005314 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005315 iov[1].iov_base = (char *)buf;
5316 iov[1].iov_len = count;
5317
5318 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005319
5320 memset(&rqst, 0, sizeof(struct smb_rqst));
5321 rqst.rq_iov = iov;
5322 rqst.rq_nvec = 2;
5323
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005324 rc = cifs_send_recv(xid, tcon->ses, server,
5325 &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005326 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005327 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005328 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005329 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005330 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05005331 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5332 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005333 }
5334
5335 return rc;
5336}
5337
5338int
5339SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5340 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5341 const __u64 length, const __u64 offset, const __u32 lock_flags,
5342 const bool wait)
5343{
5344 struct smb2_lock_element lock;
5345
5346 lock.Offset = cpu_to_le64(offset);
5347 lock.Length = cpu_to_le64(length);
5348 lock.Flags = cpu_to_le32(lock_flags);
5349 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5350 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5351
5352 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5353}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005354
5355int
5356SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5357 __u8 *lease_key, const __le32 lease_state)
5358{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005359 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005360 int rc;
5361 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005362 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005363 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005364 unsigned int total_len;
5365 struct kvec iov[1];
5366 struct kvec rsp_iov;
5367 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05005368 __u64 *please_key_high;
5369 __u64 *please_key_low;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005370 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005371
Joe Perchesf96637b2013-05-04 22:12:25 -05005372 cifs_dbg(FYI, "SMB2_lease_break\n");
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005373 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5374 (void **) &req, &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005375 if (rc)
5376 return rc;
5377
Steve French5a77e752018-05-09 17:43:08 -05005378 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005379 flags |= CIFS_TRANSFORM_REQ;
5380
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005381 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005382 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005383 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005384
5385 memcpy(req->LeaseKey, lease_key, 16);
5386 req->LeaseState = lease_state;
5387
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10005388 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005389
5390 iov[0].iov_base = (char *)req;
5391 iov[0].iov_len = total_len;
5392
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005393 memset(&rqst, 0, sizeof(struct smb_rqst));
5394 rqst.rq_iov = iov;
5395 rqst.rq_nvec = 1;
5396
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005397 rc = cifs_send_recv(xid, ses, server,
5398 &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005399 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005400
Aurelien Apteld339adc2019-01-31 13:46:07 +01005401 please_key_low = (__u64 *)lease_key;
5402 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005403 if (rc) {
5404 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05005405 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5406 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05005407 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05005408 } else
5409 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5410 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005411
5412 return rc;
5413}