blob: 24c2ac360591b8692cba07cbef034a1f6d5a9463 [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);
430 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
431 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
432 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 French9ac63ec2019-06-07 08:59:40 -0500452 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
453 pneg_ctxt->CipherCount = cpu_to_le16(2);
454 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
455 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500456}
457
Steve French96d3cca2019-06-25 04:39:51 -0500458static unsigned int
459build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
460{
461 struct nls_table *cp = load_nls_default();
462
463 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
464
465 /* copy up to max of first 100 bytes of server name to NetName field */
Steve Frenchdf58fae2019-08-05 17:07:26 -0500466 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
Steve French96d3cca2019-06-25 04:39:51 -0500467 /* context size is DataLength + minimal smb2_neg_context */
468 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
469 sizeof(struct smb2_neg_context), 8) * 8;
470}
471
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500472static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500473build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
474{
475 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
476 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
Steve French0d481322019-02-24 17:56:33 -0600477 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
478 pneg_ctxt->Name[0] = 0x93;
479 pneg_ctxt->Name[1] = 0xAD;
480 pneg_ctxt->Name[2] = 0x25;
481 pneg_ctxt->Name[3] = 0x50;
482 pneg_ctxt->Name[4] = 0x9C;
483 pneg_ctxt->Name[5] = 0xB4;
484 pneg_ctxt->Name[6] = 0x11;
485 pneg_ctxt->Name[7] = 0xE7;
486 pneg_ctxt->Name[8] = 0xB4;
487 pneg_ctxt->Name[9] = 0x23;
488 pneg_ctxt->Name[10] = 0x83;
489 pneg_ctxt->Name[11] = 0xDE;
490 pneg_ctxt->Name[12] = 0x96;
491 pneg_ctxt->Name[13] = 0x8B;
492 pneg_ctxt->Name[14] = 0xCD;
493 pneg_ctxt->Name[15] = 0x7C;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500494}
495
496static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100497assemble_neg_contexts(struct smb2_negotiate_req *req,
Steve French9fe5ff12019-06-24 20:39:04 -0500498 struct TCP_Server_Info *server, unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500499{
Colin Ian Kinga9f76cf2019-12-02 18:59:42 +0000500 char *pneg_ctxt;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500501 unsigned int ctxt_len;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500502
Steve Frenchd5c70762019-01-03 02:37:21 -0600503 if (*total_len > 200) {
504 /* In case length corrupted don't want to overrun smb buffer */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000505 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
Steve Frenchd5c70762019-01-03 02:37:21 -0600506 return;
507 }
508
509 /*
510 * round up total_len of fixed part of SMB3 negotiate request to 8
511 * byte boundary before adding negotiate contexts
512 */
513 *total_len = roundup(*total_len, 8);
514
515 pneg_ctxt = (*total_len) + (char *)req;
516 req->NegotiateContextOffset = cpu_to_le32(*total_len);
517
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500518 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500519 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
520 *total_len += ctxt_len;
521 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100522
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500523 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500524 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
525 *total_len += ctxt_len;
526 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100527
Steve French9fe5ff12019-06-24 20:39:04 -0500528 if (server->compress_algorithm) {
529 build_compression_ctxt((struct smb2_compression_capabilities_context *)
Steve French26ea8882019-04-26 20:36:08 -0700530 pneg_ctxt);
Steve French9fe5ff12019-06-24 20:39:04 -0500531 ctxt_len = DIV_ROUND_UP(
532 sizeof(struct smb2_compression_capabilities_context),
533 8) * 8;
534 *total_len += ctxt_len;
535 pneg_ctxt += ctxt_len;
Steve French96d3cca2019-06-25 04:39:51 -0500536 req->NegotiateContextCount = cpu_to_le16(5);
Steve French9fe5ff12019-06-24 20:39:04 -0500537 } else
Steve French96d3cca2019-06-25 04:39:51 -0500538 req->NegotiateContextCount = cpu_to_le16(4);
539
540 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
541 server->hostname);
542 *total_len += ctxt_len;
543 pneg_ctxt += ctxt_len;
544
Steve Frenchfcef0db2018-05-19 20:45:27 -0500545 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
546 *total_len += sizeof(struct smb2_posix_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500547}
Steve French5100d8a2018-04-09 10:47:14 -0500548
549static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
550{
551 unsigned int len = le16_to_cpu(ctxt->DataLength);
552
553 /* If invalid preauth context warn but use what we requested, SHA-512 */
554 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700555 pr_warn_once("server sent bad preauth context\n");
Steve French5100d8a2018-04-09 10:47:14 -0500556 return;
557 }
558 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
Joe Perchesa0a30362020-04-14 22:42:53 -0700559 pr_warn_once("Invalid SMB3 hash algorithm count\n");
Steve French5100d8a2018-04-09 10:47:14 -0500560 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
Joe Perchesa0a30362020-04-14 22:42:53 -0700561 pr_warn_once("unknown SMB3 hash algorithm\n");
Steve French5100d8a2018-04-09 10:47:14 -0500562}
563
Steve French26ea8882019-04-26 20:36:08 -0700564static void decode_compress_ctx(struct TCP_Server_Info *server,
565 struct smb2_compression_capabilities_context *ctxt)
566{
567 unsigned int len = le16_to_cpu(ctxt->DataLength);
568
569 /* sizeof compress context is a one element compression capbility struct */
570 if (len < 10) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700571 pr_warn_once("server sent bad compression cntxt\n");
Steve French26ea8882019-04-26 20:36:08 -0700572 return;
573 }
574 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700575 pr_warn_once("Invalid SMB3 compress algorithm count\n");
Steve French26ea8882019-04-26 20:36:08 -0700576 return;
577 }
578 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700579 pr_warn_once("unknown compression algorithm\n");
Steve French26ea8882019-04-26 20:36:08 -0700580 return;
581 }
582 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
583}
584
Steve French5100d8a2018-04-09 10:47:14 -0500585static int decode_encrypt_ctx(struct TCP_Server_Info *server,
586 struct smb2_encryption_neg_context *ctxt)
587{
588 unsigned int len = le16_to_cpu(ctxt->DataLength);
589
590 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
591 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700592 pr_warn_once("server sent bad crypto ctxt len\n");
Steve French5100d8a2018-04-09 10:47:14 -0500593 return -EINVAL;
594 }
595
596 if (le16_to_cpu(ctxt->CipherCount) != 1) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700597 pr_warn_once("Invalid SMB3.11 cipher count\n");
Steve French5100d8a2018-04-09 10:47:14 -0500598 return -EINVAL;
599 }
600 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
601 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
602 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700603 pr_warn_once("Invalid SMB3.11 cipher returned\n");
Steve French5100d8a2018-04-09 10:47:14 -0500604 return -EINVAL;
605 }
606 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500607 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500608 return 0;
609}
610
611static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000612 struct TCP_Server_Info *server,
613 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500614{
615 struct smb2_neg_context *pctx;
616 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
617 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500618 unsigned int len_of_ctxts, i;
619 int rc = 0;
620
621 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
622 if (len_of_smb <= offset) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000623 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
Steve French5100d8a2018-04-09 10:47:14 -0500624 return -EINVAL;
625 }
626
627 len_of_ctxts = len_of_smb - offset;
628
629 for (i = 0; i < ctxt_cnt; i++) {
630 int clen;
631 /* check that offset is not beyond end of SMB */
632 if (len_of_ctxts == 0)
633 break;
634
635 if (len_of_ctxts < sizeof(struct smb2_neg_context))
636 break;
637
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000638 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500639 clen = le16_to_cpu(pctx->DataLength);
640 if (clen > len_of_ctxts)
641 break;
642
643 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
644 decode_preauth_context(
645 (struct smb2_preauth_neg_context *)pctx);
646 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
647 rc = decode_encrypt_ctx(server,
648 (struct smb2_encryption_neg_context *)pctx);
Steve French26ea8882019-04-26 20:36:08 -0700649 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
650 decode_compress_ctx(server,
651 (struct smb2_compression_capabilities_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500652 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
653 server->posix_ext_supported = true;
Steve French5100d8a2018-04-09 10:47:14 -0500654 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000655 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
Steve French5100d8a2018-04-09 10:47:14 -0500656 le16_to_cpu(pctx->ContextType));
657
658 if (rc)
659 break;
660 /* offsets must be 8 byte aligned */
661 clen = (clen + 7) & ~0x7;
662 offset += clen + sizeof(struct smb2_neg_context);
663 len_of_ctxts -= clen;
664 }
665 return rc;
666}
667
Steve Frenchce558b02018-05-31 19:16:54 -0500668static struct create_posix *
669create_posix_buf(umode_t mode)
670{
671 struct create_posix *buf;
672
673 buf = kzalloc(sizeof(struct create_posix),
674 GFP_KERNEL);
675 if (!buf)
676 return NULL;
677
678 buf->ccontext.DataOffset =
679 cpu_to_le16(offsetof(struct create_posix, Mode));
680 buf->ccontext.DataLength = cpu_to_le32(4);
681 buf->ccontext.NameOffset =
682 cpu_to_le16(offsetof(struct create_posix, Name));
683 buf->ccontext.NameLength = cpu_to_le16(16);
684
685 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
686 buf->Name[0] = 0x93;
687 buf->Name[1] = 0xAD;
688 buf->Name[2] = 0x25;
689 buf->Name[3] = 0x50;
690 buf->Name[4] = 0x9C;
691 buf->Name[5] = 0xB4;
692 buf->Name[6] = 0x11;
693 buf->Name[7] = 0xE7;
694 buf->Name[8] = 0xB4;
695 buf->Name[9] = 0x23;
696 buf->Name[10] = 0x83;
697 buf->Name[11] = 0xDE;
698 buf->Name[12] = 0x96;
699 buf->Name[13] = 0x8B;
700 buf->Name[14] = 0xCD;
701 buf->Name[15] = 0x7C;
702 buf->Mode = cpu_to_le32(mode);
Joe Perchesa0a30362020-04-14 22:42:53 -0700703 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
Steve Frenchce558b02018-05-31 19:16:54 -0500704 return buf;
705}
706
707static int
708add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
709{
710 struct smb2_create_req *req = iov[0].iov_base;
711 unsigned int num = *num_iovec;
712
713 iov[num].iov_base = create_posix_buf(mode);
Steve Frenchd0959b02019-10-05 10:53:58 -0500714 if (mode == ACL_NO_MODE)
Joe Perchesa0a30362020-04-14 22:42:53 -0700715 cifs_dbg(FYI, "Invalid mode\n");
Steve Frenchce558b02018-05-31 19:16:54 -0500716 if (iov[num].iov_base == NULL)
717 return -ENOMEM;
718 iov[num].iov_len = sizeof(struct create_posix);
719 if (!req->CreateContextsOffset)
720 req->CreateContextsOffset = cpu_to_le32(
721 sizeof(struct smb2_create_req) +
722 iov[num - 1].iov_len);
723 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
724 *num_iovec = num + 1;
725 return 0;
726}
727
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500728
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400729/*
730 *
731 * SMB2 Worker functions follow:
732 *
733 * The general structure of the worker functions is:
734 * 1) Call smb2_init (assembles SMB2 header)
735 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
736 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
737 * 4) Decode SMB2 command specific fields in the fixed length area
738 * 5) Decode variable length data area (if any for this SMB2 command type)
739 * 6) Call free smb buffer
740 * 7) return
741 *
742 */
743
744int
745SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
746{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000747 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400748 struct smb2_negotiate_req *req;
749 struct smb2_negotiate_rsp *rsp;
750 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700751 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400752 int rc = 0;
753 int resp_buftype;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200754 struct TCP_Server_Info *server = cifs_ses_server(ses);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400755 int blob_offset, blob_length;
756 char *security_blob;
757 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100758 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400759
Joe Perchesf96637b2013-05-04 22:12:25 -0500760 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400761
Jeff Layton3534b852013-05-24 07:41:01 -0400762 if (!server) {
763 WARN(1, "%s: server is NULL!\n", __func__);
764 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400765 }
766
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500767 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
768 (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400769 if (rc)
770 return rc;
771
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100772 req->sync_hdr.SessionId = 0;
Steve French0fdfef92018-06-28 19:30:23 -0500773
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100774 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
775 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400776
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200777 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500778 SMB3ANY_VERSION_STRING) == 0) {
779 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
780 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
781 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100782 total_len += 4;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000783 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500784 SMBDEFAULT_VERSION_STRING) == 0) {
785 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
786 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
787 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600788 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
789 req->DialectCount = cpu_to_le16(4);
790 total_len += 8;
Steve French9764c022017-09-17 10:41:35 -0500791 } else {
792 /* otherwise send specific dialect */
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200793 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
Steve French9764c022017-09-17 10:41:35 -0500794 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100795 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500796 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400797
798 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400799 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500800 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400801 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500802 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400803 else
804 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400805
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000806 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400807
Steve French3c5f9be12014-05-13 13:37:45 -0700808 /* ClientGUID must be zero for SMB2.02 dialect */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000809 if (server->vals->protocol_id == SMB20_PROT_ID)
Steve French3c5f9be12014-05-13 13:37:45 -0700810 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500811 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700812 memcpy(req->ClientGUID, server->client_guid,
813 SMB2_CLIENT_GUID_SIZE);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000814 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
815 (strcmp(server->vals->version_string,
Steve Frenchd5c70762019-01-03 02:37:21 -0600816 SMBDEFAULT_VERSION_STRING) == 0))
Steve French9fe5ff12019-06-24 20:39:04 -0500817 assemble_neg_contexts(req, server, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500818 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400819 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100820 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400821
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000822 memset(&rqst, 0, sizeof(struct smb_rqst));
823 rqst.rq_iov = iov;
824 rqst.rq_nvec = 1;
825
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500826 rc = cifs_send_recv(xid, ses, server,
827 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700828 cifs_small_buf_release(req);
829 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400830 /*
831 * No tcon so can't do
832 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
833 */
Steve French7e682f72017-08-31 21:34:24 -0500834 if (rc == -EOPNOTSUPP) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700835 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 -0500836 goto neg_exit;
837 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400838 goto neg_exit;
839
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000840 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500841 SMB3ANY_VERSION_STRING) == 0) {
842 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000843 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500844 "SMB2 dialect returned but not requested\n");
845 return -EIO;
846 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000847 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500848 "SMB2.1 dialect returned but not requested\n");
849 return -EIO;
850 }
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000851 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500852 SMBDEFAULT_VERSION_STRING) == 0) {
853 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000854 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500855 "SMB2 dialect returned but not requested\n");
856 return -EIO;
857 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
858 /* ops set to 3.0 by default for default so update */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000859 server->ops = &smb21_operations;
860 server->vals = &smb21_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800861 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000862 server->ops = &smb311_operations;
863 server->vals = &smb311_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800864 }
Steve French590d08d2017-09-19 11:43:47 -0500865 } else if (le16_to_cpu(rsp->DialectRevision) !=
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000866 server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500867 /* if requested single dialect ensure returned dialect matched */
Joe Perchesa0a30362020-04-14 22:42:53 -0700868 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
869 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500870 return -EIO;
871 }
872
Joe Perchesf96637b2013-05-04 22:12:25 -0500873 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400874
Steve Frenche4aa25e2012-10-01 12:26:22 -0500875 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500876 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500877 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500878 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500879 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500880 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500881 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
882 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600883 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
884 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400885 else {
Joe Perchesa0a30362020-04-14 22:42:53 -0700886 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
887 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400888 rc = -EIO;
889 goto neg_exit;
890 }
891 server->dialect = le16_to_cpu(rsp->DialectRevision);
892
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100893 /*
894 * Keep a copy of the hash after negprot. This hash will be
895 * the starting hash value for all sessions made from this
896 * server.
897 */
898 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
899 SMB2_PREAUTH_HASH_SIZE);
Steve French0fdfef92018-06-28 19:30:23 -0500900
Jeff Laytone598d1d82013-05-26 07:00:59 -0400901 /* SMB2 only has an extended negflavor */
902 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400903 /* set it to the maximum buffer size value we can send with 1 credit */
904 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
905 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400906 server->max_read = le32_to_cpu(rsp->MaxReadSize);
907 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400908 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -0500909 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
910 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
911 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400912 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400913 /* Internal types */
914 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400915
916 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000917 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -0500918 /*
919 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
920 * for us will be
921 * ses->sectype = RawNTLMSSP;
922 * but for time being this is our only auth choice so doesn't matter.
923 * We just found a server which sets blob length to zero expecting raw.
924 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700925 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500926 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700927 server->sec_ntlmssp = true;
928 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700929
Jeff Layton38d77c52013-05-26 07:01:00 -0400930 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400931 if (rc)
932 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500933 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500934 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500935 if (rc == 1)
936 rc = 0;
937 else if (rc == 0)
938 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400939 }
Steve French5100d8a2018-04-09 10:47:14 -0500940
Steve French5100d8a2018-04-09 10:47:14 -0500941 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
942 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000943 rc = smb311_decode_neg_context(rsp, server,
944 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -0500945 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000946 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
Steve French5100d8a2018-04-09 10:47:14 -0500947 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400948neg_exit:
949 free_rsp_buf(resp_buftype, rsp);
950 return rc;
951}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400952
Steve Frenchff1c0382013-11-19 23:44:46 -0600953int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
954{
Long Li2796d302018-04-25 11:30:04 -0700955 int rc;
956 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200957 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600958 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500959 u32 inbuflen; /* max of 4 dialects */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000960 struct TCP_Server_Info *server = tcon->ses->server;
Steve Frenchff1c0382013-11-19 23:44:46 -0600961
962 cifs_dbg(FYI, "validate negotiate\n");
963
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100964 /* In SMB3.11 preauth integrity supersedes validate negotiate */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000965 if (server->dialect == SMB311_PROT_ID)
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100966 return 0;
967
Steve Frenchff1c0382013-11-19 23:44:46 -0600968 /*
969 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500970 * can not sign it (ie are not known user). Even if signing is not
971 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600972 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500973 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600974 */
Steve French0603c962017-09-20 19:57:18 -0500975 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600976 return 0; /* validation requires signing */
977
Steve French0603c962017-09-20 19:57:18 -0500978 if (tcon->ses->user_name == NULL) {
979 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
980 return 0; /* validation requires signing */
981 }
982
983 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000984 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
Steve French0603c962017-09-20 19:57:18 -0500985
Long Li2796d302018-04-25 11:30:04 -0700986 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
987 if (!pneg_inbuf)
988 return -ENOMEM;
989
990 pneg_inbuf->Capabilities =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000991 cpu_to_le32(server->vals->req_capabilities);
992 memcpy(pneg_inbuf->Guid, server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100993 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600994
995 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -0700996 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -0600997 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
998 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -0700999 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001000 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1001 else
Long Li2796d302018-04-25 11:30:04 -07001002 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001003
Steve French9764c022017-09-17 10:41:35 -05001004
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001005 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001006 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001007 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1008 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1009 pneg_inbuf->DialectCount = cpu_to_le16(2);
Steve French9764c022017-09-17 10:41:35 -05001010 /* structure is big enough for 3 dialects, sending only 2 */
Long Li2796d302018-04-25 11:30:04 -07001011 inbuflen = sizeof(*pneg_inbuf) -
Steve Frenchd5c70762019-01-03 02:37:21 -06001012 (2 * sizeof(pneg_inbuf->Dialects[0]));
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001013 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001014 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001015 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1016 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1017 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -06001018 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1019 pneg_inbuf->DialectCount = cpu_to_le16(4);
Steve French9764c022017-09-17 10:41:35 -05001020 /* structure is big enough for 3 dialects */
Long Li2796d302018-04-25 11:30:04 -07001021 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -05001022 } else {
1023 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -07001024 pneg_inbuf->Dialects[0] =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001025 cpu_to_le16(server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -07001026 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -05001027 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -07001028 inbuflen = sizeof(*pneg_inbuf) -
1029 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -05001030 }
Steve Frenchff1c0382013-11-19 23:44:46 -06001031
1032 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1033 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001034 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1035 (char **)&pneg_rsp, &rsplen);
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001036 if (rc == -EOPNOTSUPP) {
1037 /*
1038 * Old Windows versions or Netapp SMB server can return
1039 * not supported error. Client should accept it.
1040 */
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001041 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
Colin Ian King21078202019-05-17 09:12:33 +01001042 rc = 0;
1043 goto out_free_inbuf;
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001044 } else if (rc != 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001045 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1046 rc);
Long Li2796d302018-04-25 11:30:04 -07001047 rc = -EIO;
1048 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -06001049 }
1050
Long Li2796d302018-04-25 11:30:04 -07001051 rc = -EIO;
1052 if (rsplen != sizeof(*pneg_rsp)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001053 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1054 rsplen);
Steve French7db0a6e2017-05-03 21:12:20 -05001055
1056 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -07001057 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1058 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001059 }
1060
1061 /* check validate negotiate info response matches what we got earlier */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001062 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -06001063 goto vneg_out;
1064
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001065 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
Steve Frenchff1c0382013-11-19 23:44:46 -06001066 goto vneg_out;
1067
1068 /* do not validate server guid because not saved at negprot time yet */
1069
1070 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001071 SMB2_LARGE_FILES) != server->capabilities)
Steve Frenchff1c0382013-11-19 23:44:46 -06001072 goto vneg_out;
1073
1074 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -07001075 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001076 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -07001077 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001078
1079vneg_out:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001080 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -07001081out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001082 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -07001083out_free_inbuf:
1084 kfree(pneg_inbuf);
1085 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -06001086}
1087
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301088enum securityEnum
1089smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1090{
1091 switch (requested) {
1092 case Kerberos:
1093 case RawNTLMSSP:
1094 return requested;
1095 case NTLMv2:
1096 return RawNTLMSSP;
1097 case Unspecified:
1098 if (server->sec_ntlmssp &&
1099 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1100 return RawNTLMSSP;
1101 if ((server->sec_kerberos || server->sec_mskerberos) &&
1102 (global_secflags & CIFSSEC_MAY_KRB5))
1103 return Kerberos;
1104 /* Fallthrough */
1105 default:
1106 return Unspecified;
1107 }
1108}
1109
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001110struct SMB2_sess_data {
1111 unsigned int xid;
1112 struct cifs_ses *ses;
1113 struct nls_table *nls_cp;
1114 void (*func)(struct SMB2_sess_data *);
1115 int result;
1116 u64 previous_session;
1117
1118 /* we will send the SMB in three pieces:
1119 * a fixed length beginning part, an optional
1120 * SPNEGO blob (which can be zero length), and a
1121 * last part which will include the strings
1122 * and rest of bcc area. This allows us to avoid
1123 * a large buffer 17K allocation
1124 */
1125 int buf0_type;
1126 struct kvec iov[2];
1127};
1128
1129static int
1130SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1131{
1132 int rc;
1133 struct cifs_ses *ses = sess_data->ses;
1134 struct smb2_sess_setup_req *req;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001135 struct TCP_Server_Info *server = cifs_ses_server(ses);
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001136 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001137
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001138 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1139 (void **) &req,
1140 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001141 if (rc)
1142 return rc;
1143
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001144 if (sess_data->ses->binding) {
1145 req->sync_hdr.SessionId = sess_data->ses->Suid;
1146 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1147 req->PreviousSessionId = 0;
1148 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1149 } else {
1150 /* First session, not a reauthenticate */
1151 req->sync_hdr.SessionId = 0;
1152 /*
1153 * if reconnect, we need to send previous sess id
1154 * otherwise it is 0
1155 */
1156 req->PreviousSessionId = sess_data->previous_session;
1157 req->Flags = 0; /* MBZ */
1158 }
Steve Frenchd4090142018-06-13 17:05:58 -05001159
1160 /* enough to enable echos and oplocks and one max size write */
1161 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001162
1163 /* only one of SMB2 signing flags may be set in SMB2 request */
1164 if (server->sign)
1165 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1166 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1167 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1168 else
1169 req->SecurityMode = 0;
1170
Steve French8d330962019-07-25 18:13:10 -05001171#ifdef CONFIG_CIFS_DFS_UPCALL
1172 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1173#else
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001174 req->Capabilities = 0;
Steve French8d330962019-07-25 18:13:10 -05001175#endif /* DFS_UPCALL */
1176
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001177 req->Channel = 0; /* MBZ */
1178
1179 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001180 /* 1 for pad */
1181 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001182 /*
1183 * This variable will be used to clear the buffer
1184 * allocated above in case of any error in the calling function.
1185 */
1186 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1187
1188 return 0;
1189}
1190
1191static void
1192SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1193{
1194 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1195 sess_data->buf0_type = CIFS_NO_BUFFER;
1196}
1197
1198static int
1199SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1200{
1201 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001202 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001203 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001204 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001205
1206 /* Testing shows that buffer offset must be at location of Buffer[0] */
1207 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001208 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001209 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1210
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001211 memset(&rqst, 0, sizeof(struct smb_rqst));
1212 rqst.rq_iov = sess_data->iov;
1213 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001214
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001215 /* BB add code to build os and lm fields */
1216 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001217 cifs_ses_server(sess_data->ses),
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001218 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001219 &sess_data->buf0_type,
1220 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001221 cifs_small_buf_release(sess_data->iov[0].iov_base);
1222 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001223
1224 return rc;
1225}
1226
1227static int
1228SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1229{
1230 int rc = 0;
1231 struct cifs_ses *ses = sess_data->ses;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001232 struct TCP_Server_Info *server = cifs_ses_server(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001233
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001234 mutex_lock(&server->srv_mutex);
1235 if (server->ops->generate_signingkey) {
1236 rc = server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001237 if (rc) {
1238 cifs_dbg(FYI,
1239 "SMB3 session key generation failed\n");
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001240 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001241 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001242 }
1243 }
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001244 if (!server->session_estab) {
1245 server->sequence_number = 0x2;
1246 server->session_estab = true;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001247 }
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001248 mutex_unlock(&server->srv_mutex);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001249
1250 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001251 /* keep existing ses state if binding */
1252 if (!ses->binding) {
1253 spin_lock(&GlobalMid_Lock);
1254 ses->status = CifsGood;
1255 ses->need_reconnect = false;
1256 spin_unlock(&GlobalMid_Lock);
1257 }
1258
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001259 return rc;
1260}
1261
1262#ifdef CONFIG_CIFS_UPCALL
1263static void
1264SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1265{
1266 int rc;
1267 struct cifs_ses *ses = sess_data->ses;
1268 struct cifs_spnego_msg *msg;
1269 struct key *spnego_key = NULL;
1270 struct smb2_sess_setup_rsp *rsp = NULL;
1271
1272 rc = SMB2_sess_alloc_buffer(sess_data);
1273 if (rc)
1274 goto out;
1275
1276 spnego_key = cifs_get_spnego_key(ses);
1277 if (IS_ERR(spnego_key)) {
1278 rc = PTR_ERR(spnego_key);
Steve French0a018942020-07-16 00:34:21 -05001279 if (rc == -ENOKEY)
1280 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001281 spnego_key = NULL;
1282 goto out;
1283 }
1284
1285 msg = spnego_key->payload.data[0];
1286 /*
1287 * check version field to make sure that cifs.upcall is
1288 * sending us a response in an expected form
1289 */
1290 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001291 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1292 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001293 rc = -EKEYREJECTED;
1294 goto out_put_spnego_key;
1295 }
1296
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001297 /* keep session key if binding */
1298 if (!ses->binding) {
1299 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1300 GFP_KERNEL);
1301 if (!ses->auth_key.response) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001302 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001303 msg->sesskey_len);
1304 rc = -ENOMEM;
1305 goto out_put_spnego_key;
1306 }
1307 ses->auth_key.len = msg->sesskey_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001308 }
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001309
1310 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1311 sess_data->iov[1].iov_len = msg->secblob_len;
1312
1313 rc = SMB2_sess_sendreceive(sess_data);
1314 if (rc)
1315 goto out_put_spnego_key;
1316
1317 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001318 /* keep session id and flags if binding */
1319 if (!ses->binding) {
1320 ses->Suid = rsp->sync_hdr.SessionId;
1321 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1322 }
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001323
1324 rc = SMB2_sess_establish_session(sess_data);
1325out_put_spnego_key:
1326 key_invalidate(spnego_key);
1327 key_put(spnego_key);
1328out:
1329 sess_data->result = rc;
1330 sess_data->func = NULL;
1331 SMB2_sess_free_buffer(sess_data);
1332}
1333#else
1334static void
1335SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1336{
1337 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1338 sess_data->result = -EOPNOTSUPP;
1339 sess_data->func = NULL;
1340}
1341#endif
1342
Sachin Prabhu166cea42016-10-07 19:11:22 +01001343static void
1344SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1345
1346static void
1347SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1348{
1349 int rc;
1350 struct cifs_ses *ses = sess_data->ses;
1351 struct smb2_sess_setup_rsp *rsp = NULL;
1352 char *ntlmssp_blob = NULL;
1353 bool use_spnego = false; /* else use raw ntlmssp */
1354 u16 blob_length = 0;
1355
1356 /*
1357 * If memory allocation is successful, caller of this function
1358 * frees it.
1359 */
1360 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1361 if (!ses->ntlmssp) {
1362 rc = -ENOMEM;
1363 goto out_err;
1364 }
1365 ses->ntlmssp->sesskey_per_smbsess = true;
1366
1367 rc = SMB2_sess_alloc_buffer(sess_data);
1368 if (rc)
1369 goto out_err;
1370
1371 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1372 GFP_KERNEL);
1373 if (ntlmssp_blob == NULL) {
1374 rc = -ENOMEM;
1375 goto out;
1376 }
1377
1378 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1379 if (use_spnego) {
1380 /* BB eventually need to add this */
1381 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1382 rc = -EOPNOTSUPP;
1383 goto out;
1384 } else {
1385 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1386 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1387 }
1388 sess_data->iov[1].iov_base = ntlmssp_blob;
1389 sess_data->iov[1].iov_len = blob_length;
1390
1391 rc = SMB2_sess_sendreceive(sess_data);
1392 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1393
1394 /* If true, rc here is expected and not an error */
1395 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001396 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001397 rc = 0;
1398
1399 if (rc)
1400 goto out;
1401
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001402 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001403 le16_to_cpu(rsp->SecurityBufferOffset)) {
1404 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1405 le16_to_cpu(rsp->SecurityBufferOffset));
1406 rc = -EIO;
1407 goto out;
1408 }
1409 rc = decode_ntlmssp_challenge(rsp->Buffer,
1410 le16_to_cpu(rsp->SecurityBufferLength), ses);
1411 if (rc)
1412 goto out;
1413
1414 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1415
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001416 /* keep existing ses id and flags if binding */
1417 if (!ses->binding) {
1418 ses->Suid = rsp->sync_hdr.SessionId;
1419 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1420 }
Sachin Prabhu166cea42016-10-07 19:11:22 +01001421
1422out:
1423 kfree(ntlmssp_blob);
1424 SMB2_sess_free_buffer(sess_data);
1425 if (!rc) {
1426 sess_data->result = 0;
1427 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1428 return;
1429 }
1430out_err:
1431 kfree(ses->ntlmssp);
1432 ses->ntlmssp = NULL;
1433 sess_data->result = rc;
1434 sess_data->func = NULL;
1435}
1436
1437static void
1438SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1439{
1440 int rc;
1441 struct cifs_ses *ses = sess_data->ses;
1442 struct smb2_sess_setup_req *req;
1443 struct smb2_sess_setup_rsp *rsp = NULL;
1444 unsigned char *ntlmssp_blob = NULL;
1445 bool use_spnego = false; /* else use raw ntlmssp */
1446 u16 blob_length = 0;
1447
1448 rc = SMB2_sess_alloc_buffer(sess_data);
1449 if (rc)
1450 goto out;
1451
1452 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001453 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001454
1455 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1456 sess_data->nls_cp);
1457 if (rc) {
1458 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1459 goto out;
1460 }
1461
1462 if (use_spnego) {
1463 /* BB eventually need to add this */
1464 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1465 rc = -EOPNOTSUPP;
1466 goto out;
1467 }
1468 sess_data->iov[1].iov_base = ntlmssp_blob;
1469 sess_data->iov[1].iov_len = blob_length;
1470
1471 rc = SMB2_sess_sendreceive(sess_data);
1472 if (rc)
1473 goto out;
1474
1475 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1476
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001477 /* keep existing ses id and flags if binding */
1478 if (!ses->binding) {
1479 ses->Suid = rsp->sync_hdr.SessionId;
1480 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1481 }
Sachin Prabhu166cea42016-10-07 19:11:22 +01001482
1483 rc = SMB2_sess_establish_session(sess_data);
Ronnie Sahlbergf560cda2020-04-12 16:09:26 +10001484#ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1485 if (ses->server->dialect < SMB30_PROT_ID) {
1486 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1487 /*
1488 * The session id is opaque in terms of endianness, so we can't
1489 * print it as a long long. we dump it as we got it on the wire
1490 */
1491 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1492 &ses->Suid);
1493 cifs_dbg(VFS, "Session Key %*ph\n",
1494 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1495 cifs_dbg(VFS, "Signing Key %*ph\n",
1496 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1497 }
1498#endif
Sachin Prabhu166cea42016-10-07 19:11:22 +01001499out:
1500 kfree(ntlmssp_blob);
1501 SMB2_sess_free_buffer(sess_data);
1502 kfree(ses->ntlmssp);
1503 ses->ntlmssp = NULL;
1504 sess_data->result = rc;
1505 sess_data->func = NULL;
1506}
1507
1508static int
1509SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1510{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301511 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001512
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001513 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301514 cifs_dbg(FYI, "sess setup type %d\n", type);
1515 if (type == Unspecified) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001516 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301517 return -EINVAL;
1518 }
1519
1520 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001521 case Kerberos:
1522 sess_data->func = SMB2_auth_kerberos;
1523 break;
1524 case RawNTLMSSP:
1525 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1526 break;
1527 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301528 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001529 return -EOPNOTSUPP;
1530 }
1531
1532 return 0;
1533}
1534
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001535int
1536SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1537 const struct nls_table *nls_cp)
1538{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001539 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001540 struct TCP_Server_Info *server = cifs_ses_server(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001541 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001542
Joe Perchesf96637b2013-05-04 22:12:25 -05001543 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001544
Jeff Layton3534b852013-05-24 07:41:01 -04001545 if (!server) {
1546 WARN(1, "%s: server is NULL!\n", __func__);
1547 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001548 }
1549
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001550 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1551 if (!sess_data)
1552 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001553
1554 rc = SMB2_select_sec(ses, sess_data);
1555 if (rc)
1556 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001557 sess_data->xid = xid;
1558 sess_data->ses = ses;
1559 sess_data->buf0_type = CIFS_NO_BUFFER;
1560 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001561 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001562
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001563 /*
1564 * Initialize the session hash with the server one.
1565 */
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001566 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001567 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001568
Sachin Prabhu166cea42016-10-07 19:11:22 +01001569 while (sess_data->func)
1570 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001571
Steve Frenchc721c382017-09-19 18:40:03 -05001572 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001573 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001574 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001575out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001576 kfree(sess_data);
1577 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001578}
1579
1580int
1581SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1582{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001583 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001584 struct smb2_logoff_req *req; /* response is also trivial struct */
1585 int rc = 0;
1586 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001587 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001588 unsigned int total_len;
1589 struct kvec iov[1];
1590 struct kvec rsp_iov;
1591 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001592
Joe Perchesf96637b2013-05-04 22:12:25 -05001593 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001594
1595 if (ses && (ses->server))
1596 server = ses->server;
1597 else
1598 return -EIO;
1599
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001600 /* no need to send SMB logoff if uid already closed due to reconnect */
1601 if (ses->need_reconnect)
1602 goto smb2_session_already_dead;
1603
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001604 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1605 (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001606 if (rc)
1607 return rc;
1608
1609 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001610 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001611
1612 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1613 flags |= CIFS_TRANSFORM_REQ;
1614 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001615 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001616
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001617 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001618
1619 iov[0].iov_base = (char *)req;
1620 iov[0].iov_len = total_len;
1621
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001622 memset(&rqst, 0, sizeof(struct smb_rqst));
1623 rqst.rq_iov = iov;
1624 rqst.rq_nvec = 1;
1625
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001626 rc = cifs_send_recv(xid, ses, ses->server,
1627 &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001628 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001629 /*
1630 * No tcon so can't do
1631 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1632 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001633
1634smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001635 return rc;
1636}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001637
1638static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1639{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001640 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001641}
1642
1643#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1644
Steve Frenchde9f68df2013-11-15 11:26:24 -06001645/* These are similar values to what Windows uses */
1646static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1647{
1648 tcon->max_chunks = 256;
1649 tcon->max_bytes_chunk = 1048576;
1650 tcon->max_bytes_copy = 16777216;
1651}
1652
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001653int
1654SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1655 struct cifs_tcon *tcon, const struct nls_table *cp)
1656{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001657 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001658 struct smb2_tree_connect_req *req;
1659 struct smb2_tree_connect_rsp *rsp = NULL;
1660 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001661 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001662 int rc = 0;
1663 int resp_buftype;
1664 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001665 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001666 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001667 unsigned int total_len;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001668 struct TCP_Server_Info *server;
1669
1670 /* always use master channel */
1671 server = ses->server;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001672
Joe Perchesf96637b2013-05-04 22:12:25 -05001673 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001674
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001675 if (!server || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001676 return -EIO;
1677
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001678 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1679 if (unc_path == NULL)
1680 return -ENOMEM;
1681
1682 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1683 unc_path_len *= 2;
1684 if (unc_path_len < 2) {
1685 kfree(unc_path);
1686 return -EINVAL;
1687 }
1688
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001689 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001690 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001691 atomic_set(&tcon->num_remote_opens, 0);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001692 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1693 (void **) &req, &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001694 if (rc) {
1695 kfree(unc_path);
1696 return rc;
1697 }
1698
Steve French5a77e752018-05-09 17:43:08 -05001699 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001700 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001701
1702 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001703 /* 1 for pad */
1704 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001705
1706 /* Testing shows that buffer offset must be at location of Buffer[0] */
1707 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001708 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001709 req->PathLength = cpu_to_le16(unc_path_len - 2);
1710 iov[1].iov_base = unc_path;
1711 iov[1].iov_len = unc_path_len;
1712
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001713 /*
1714 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1715 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
Steve French8c11a602019-03-22 22:31:17 -05001716 * (Samba servers don't always set the flag so also check if null user)
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001717 */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001718 if ((server->dialect == SMB311_PROT_ID) &&
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001719 !smb3_encryption_required(tcon) &&
Steve French8c11a602019-03-22 22:31:17 -05001720 !(ses->session_flags &
1721 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1722 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
Steve French6188f282018-03-13 02:29:36 -05001723 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1724
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001725 memset(&rqst, 0, sizeof(struct smb_rqst));
1726 rqst.rq_iov = iov;
1727 rqst.rq_nvec = 2;
1728
Steve French4fe75c42019-02-14 01:19:02 -06001729 /* Need 64 for max size write so ask for more in case not there yet */
1730 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1731
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001732 rc = cifs_send_recv(xid, ses, server,
1733 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001734 cifs_small_buf_release(req);
1735 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001736 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001737 if (rc != 0) {
1738 if (tcon) {
1739 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1740 tcon->need_reconnect = true;
1741 }
1742 goto tcon_error_exit;
1743 }
1744
Christophe JAILLETcd123002017-05-12 17:59:32 +02001745 switch (rsp->ShareType) {
1746 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001747 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001748 break;
1749 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001750 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001751 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001752 break;
1753 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001754 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001755 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001756 break;
1757 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001758 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001759 rc = -EOPNOTSUPP;
1760 goto tcon_error_exit;
1761 }
1762
1763 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001764 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001765 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1766 tcon->tidStatus = CifsGood;
1767 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001768 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001769 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001770
1771 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1772 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001773 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001774
1775 if (tcon->seal &&
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001776 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001777 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001778
Steve Frenchde9f68df2013-11-15 11:26:24 -06001779 init_copy_chunk_defaults(tcon);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001780 if (server->ops->validate_negotiate)
1781 rc = server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001782tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001783
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001784 free_rsp_buf(resp_buftype, rsp);
1785 kfree(unc_path);
1786 return rc;
1787
1788tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001789 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001790 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001791 }
1792 goto tcon_exit;
1793}
1794
1795int
1796SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1797{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001798 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001799 struct smb2_tree_disconnect_req *req; /* response is trivial */
1800 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001801 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001802 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001803 unsigned int total_len;
1804 struct kvec iov[1];
1805 struct kvec rsp_iov;
1806 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001807
Joe Perchesf96637b2013-05-04 22:12:25 -05001808 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001809
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001810 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001811 return -EIO;
1812
1813 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1814 return 0;
1815
Pavel Shilovskyd9191312019-12-10 11:44:52 -08001816 close_shroot_lease(&tcon->crfid);
Ronnie Sahlberg72e73c72019-11-07 17:00:38 +10001817
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001818 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
1819 (void **) &req,
1820 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001821 if (rc)
1822 return rc;
1823
Steve French5a77e752018-05-09 17:43:08 -05001824 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001825 flags |= CIFS_TRANSFORM_REQ;
1826
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001827 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001828
1829 iov[0].iov_base = (char *)req;
1830 iov[0].iov_len = total_len;
1831
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001832 memset(&rqst, 0, sizeof(struct smb_rqst));
1833 rqst.rq_iov = iov;
1834 rqst.rq_nvec = 1;
1835
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001836 rc = cifs_send_recv(xid, ses, ses->server,
1837 &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001838 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001839 if (rc)
1840 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1841
1842 return rc;
1843}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001844
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001845
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001846static struct create_durable *
1847create_durable_buf(void)
1848{
1849 struct create_durable *buf;
1850
1851 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1852 if (!buf)
1853 return NULL;
1854
1855 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001856 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001857 buf->ccontext.DataLength = cpu_to_le32(16);
1858 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1859 (struct create_durable, Name));
1860 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001861 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001862 buf->Name[0] = 'D';
1863 buf->Name[1] = 'H';
1864 buf->Name[2] = 'n';
1865 buf->Name[3] = 'Q';
1866 return buf;
1867}
1868
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001869static struct create_durable *
1870create_reconnect_durable_buf(struct cifs_fid *fid)
1871{
1872 struct create_durable *buf;
1873
1874 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1875 if (!buf)
1876 return NULL;
1877
1878 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1879 (struct create_durable, Data));
1880 buf->ccontext.DataLength = cpu_to_le32(16);
1881 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1882 (struct create_durable, Name));
1883 buf->ccontext.NameLength = cpu_to_le16(4);
1884 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1885 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001886 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001887 buf->Name[0] = 'D';
1888 buf->Name[1] = 'H';
1889 buf->Name[2] = 'n';
1890 buf->Name[3] = 'C';
1891 return buf;
1892}
1893
Steve French89a5bfa2019-07-18 17:22:18 -05001894static void
1895parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1896{
1897 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1898
1899 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1900 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1901 buf->IndexNumber = pdisk_id->DiskFileId;
1902}
1903
Steve Frenchab3459d2020-02-06 17:31:56 -06001904static void
Aurelien Aptel69dda302020-03-02 17:53:22 +01001905parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
1906 struct create_posix_rsp *posix)
Steve Frenchab3459d2020-02-06 17:31:56 -06001907{
Aurelien Aptel69dda302020-03-02 17:53:22 +01001908 int sid_len;
1909 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
1910 u8 *end = beg + le32_to_cpu(cc->DataLength);
1911 u8 *sid;
Steve Frenchab3459d2020-02-06 17:31:56 -06001912
Aurelien Aptel69dda302020-03-02 17:53:22 +01001913 memset(posix, 0, sizeof(*posix));
Aurelien Aptel2e8af972020-02-08 15:50:56 +01001914
Aurelien Aptel69dda302020-03-02 17:53:22 +01001915 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
1916 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
1917 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
1918
1919 sid = beg + 12;
1920 sid_len = posix_info_sid_size(sid, end);
1921 if (sid_len < 0) {
1922 cifs_dbg(VFS, "bad owner sid in posix create response\n");
1923 return;
1924 }
1925 memcpy(&posix->owner, sid, sid_len);
1926
1927 sid = sid + sid_len;
1928 sid_len = posix_info_sid_size(sid, end);
1929 if (sid_len < 0) {
1930 cifs_dbg(VFS, "bad group sid in posix create response\n");
1931 return;
1932 }
1933 memcpy(&posix->group, sid, sid_len);
1934
1935 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
1936 posix->nlink, posix->mode, posix->reparse_tag);
Steve Frenchab3459d2020-02-06 17:31:56 -06001937}
1938
Steve French89a5bfa2019-07-18 17:22:18 -05001939void
1940smb2_parse_contexts(struct TCP_Server_Info *server,
Aurelien Aptel69dda302020-03-02 17:53:22 +01001941 struct smb2_create_rsp *rsp,
1942 unsigned int *epoch, char *lease_key, __u8 *oplock,
1943 struct smb2_file_all_info *buf,
1944 struct create_posix_rsp *posix)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001945{
1946 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001947 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001948 unsigned int next;
1949 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001950 char *name;
Steve Frenchab3459d2020-02-06 17:31:56 -06001951 const char smb3_create_tag_posix[] = {0x93, 0xAD, 0x25, 0x50, 0x9C,
1952 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
1953 0xDE, 0x96, 0x8B, 0xCD, 0x7C};
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001954
Steve French89a5bfa2019-07-18 17:22:18 -05001955 *oplock = 0;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001956 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001957 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001958 cc = (struct create_context *)data_offset;
Steve French89a5bfa2019-07-18 17:22:18 -05001959
1960 /* Initialize inode number to 0 in case no valid data in qfid context */
1961 if (buf)
1962 buf->IndexNumber = 0;
1963
Justin Maggarddeb7def2016-02-09 15:52:08 -08001964 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001965 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001966 if (le16_to_cpu(cc->NameLength) == 4 &&
Steve French89a5bfa2019-07-18 17:22:18 -05001967 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1968 *oplock = server->ops->parse_lease_buf(cc, epoch,
1969 lease_key);
1970 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1971 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1972 parse_query_id_ctxt(cc, buf);
Steve Frenchab3459d2020-02-06 17:31:56 -06001973 else if ((le16_to_cpu(cc->NameLength) == 16)) {
Aurelien Aptel69dda302020-03-02 17:53:22 +01001974 if (posix &&
1975 memcmp(name, smb3_create_tag_posix, 16) == 0)
1976 parse_posix_ctxt(cc, buf, posix);
Steve Frenchab3459d2020-02-06 17:31:56 -06001977 }
1978 /* else {
1979 cifs_dbg(FYI, "Context not matched with len %d\n",
1980 le16_to_cpu(cc->NameLength));
1981 cifs_dump_mem("Cctxt name: ", name, 4);
1982 } */
Justin Maggarddeb7def2016-02-09 15:52:08 -08001983
1984 next = le32_to_cpu(cc->Next);
1985 if (!next)
1986 break;
1987 remaining -= next;
1988 cc = (struct create_context *)((char *)cc + next);
1989 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001990
Steve French89a5bfa2019-07-18 17:22:18 -05001991 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1992 *oplock = rsp->OplockLevel;
1993
1994 return;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001995}
1996
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001997static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001998add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02001999 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002000{
2001 struct smb2_create_req *req = iov[0].iov_base;
2002 unsigned int num = *num_iovec;
2003
Stefano Brivio729c0c92018-07-05 15:10:02 +02002004 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002005 if (iov[num].iov_base == NULL)
2006 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002007 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002008 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2009 if (!req->CreateContextsOffset)
2010 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002011 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002012 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002013 le32_add_cpu(&req->CreateContextsLength,
2014 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002015 *num_iovec = num + 1;
2016 return 0;
2017}
2018
Steve Frenchb56eae42015-11-03 09:26:27 -06002019static struct create_durable_v2 *
Steve Frenchca567eb2019-03-29 16:31:07 -05002020create_durable_v2_buf(struct cifs_open_parms *oparms)
Steve Frenchb56eae42015-11-03 09:26:27 -06002021{
Steve Frenchca567eb2019-03-29 16:31:07 -05002022 struct cifs_fid *pfid = oparms->fid;
Steve Frenchb56eae42015-11-03 09:26:27 -06002023 struct create_durable_v2 *buf;
2024
2025 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2026 if (!buf)
2027 return NULL;
2028
2029 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2030 (struct create_durable_v2, dcontext));
2031 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2032 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2033 (struct create_durable_v2, Name));
2034 buf->ccontext.NameLength = cpu_to_le16(4);
2035
Steve Frenchca567eb2019-03-29 16:31:07 -05002036 /*
2037 * NB: Handle timeout defaults to 0, which allows server to choose
2038 * (most servers default to 120 seconds) and most clients default to 0.
2039 * This can be overridden at mount ("handletimeout=") if the user wants
2040 * a different persistent (or resilient) handle timeout for all opens
2041 * opens on a particular SMB3 mount.
2042 */
2043 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
Steve Frenchb56eae42015-11-03 09:26:27 -06002044 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05002045 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06002046 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2047
2048 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2049 buf->Name[0] = 'D';
2050 buf->Name[1] = 'H';
2051 buf->Name[2] = '2';
2052 buf->Name[3] = 'Q';
2053 return buf;
2054}
2055
2056static struct create_durable_handle_reconnect_v2 *
2057create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2058{
2059 struct create_durable_handle_reconnect_v2 *buf;
2060
2061 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2062 GFP_KERNEL);
2063 if (!buf)
2064 return NULL;
2065
2066 buf->ccontext.DataOffset =
2067 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2068 dcontext));
2069 buf->ccontext.DataLength =
2070 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2071 buf->ccontext.NameOffset =
2072 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2073 Name));
2074 buf->ccontext.NameLength = cpu_to_le16(4);
2075
2076 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2077 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2078 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2079 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2080
2081 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2082 buf->Name[0] = 'D';
2083 buf->Name[1] = 'H';
2084 buf->Name[2] = '2';
2085 buf->Name[3] = 'C';
2086 return buf;
2087}
2088
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002089static int
Steve Frenchb56eae42015-11-03 09:26:27 -06002090add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002091 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002092{
2093 struct smb2_create_req *req = iov[0].iov_base;
2094 unsigned int num = *num_iovec;
2095
Steve Frenchca567eb2019-03-29 16:31:07 -05002096 iov[num].iov_base = create_durable_v2_buf(oparms);
Steve Frenchb56eae42015-11-03 09:26:27 -06002097 if (iov[num].iov_base == NULL)
2098 return -ENOMEM;
2099 iov[num].iov_len = sizeof(struct create_durable_v2);
2100 if (!req->CreateContextsOffset)
2101 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002102 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002103 iov[1].iov_len);
2104 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002105 *num_iovec = num + 1;
2106 return 0;
2107}
2108
2109static int
2110add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2111 struct cifs_open_parms *oparms)
2112{
2113 struct smb2_create_req *req = iov[0].iov_base;
2114 unsigned int num = *num_iovec;
2115
2116 /* indicate that we don't need to relock the file */
2117 oparms->reconnect = false;
2118
2119 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2120 if (iov[num].iov_base == NULL)
2121 return -ENOMEM;
2122 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2123 if (!req->CreateContextsOffset)
2124 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002125 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002126 iov[1].iov_len);
2127 le32_add_cpu(&req->CreateContextsLength,
2128 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002129 *num_iovec = num + 1;
2130 return 0;
2131}
2132
2133static int
2134add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2135 struct cifs_open_parms *oparms, bool use_persistent)
2136{
2137 struct smb2_create_req *req = iov[0].iov_base;
2138 unsigned int num = *num_iovec;
2139
2140 if (use_persistent) {
2141 if (oparms->reconnect)
2142 return add_durable_reconnect_v2_context(iov, num_iovec,
2143 oparms);
2144 else
2145 return add_durable_v2_context(iov, num_iovec, oparms);
2146 }
2147
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002148 if (oparms->reconnect) {
2149 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2150 /* indicate that we don't need to relock the file */
2151 oparms->reconnect = false;
2152 } else
2153 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002154 if (iov[num].iov_base == NULL)
2155 return -ENOMEM;
2156 iov[num].iov_len = sizeof(struct create_durable);
2157 if (!req->CreateContextsOffset)
2158 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002159 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002160 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08002161 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002162 *num_iovec = num + 1;
2163 return 0;
2164}
2165
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002166/* See MS-SMB2 2.2.13.2.7 */
2167static struct crt_twarp_ctxt *
2168create_twarp_buf(__u64 timewarp)
2169{
2170 struct crt_twarp_ctxt *buf;
2171
2172 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2173 if (!buf)
2174 return NULL;
2175
2176 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2177 (struct crt_twarp_ctxt, Timestamp));
2178 buf->ccontext.DataLength = cpu_to_le32(8);
2179 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2180 (struct crt_twarp_ctxt, Name));
2181 buf->ccontext.NameLength = cpu_to_le16(4);
2182 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2183 buf->Name[0] = 'T';
2184 buf->Name[1] = 'W';
2185 buf->Name[2] = 'r';
2186 buf->Name[3] = 'p';
2187 buf->Timestamp = cpu_to_le64(timewarp);
2188 return buf;
2189}
2190
2191/* See MS-SMB2 2.2.13.2.7 */
2192static int
2193add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2194{
2195 struct smb2_create_req *req = iov[0].iov_base;
2196 unsigned int num = *num_iovec;
2197
2198 iov[num].iov_base = create_twarp_buf(timewarp);
2199 if (iov[num].iov_base == NULL)
2200 return -ENOMEM;
2201 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2202 if (!req->CreateContextsOffset)
2203 req->CreateContextsOffset = cpu_to_le32(
2204 sizeof(struct smb2_create_req) +
2205 iov[num - 1].iov_len);
2206 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2207 *num_iovec = num + 1;
2208 return 0;
2209}
2210
Steve French975221e2020-06-12 09:25:21 -05002211/* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2212static void setup_owner_group_sids(char *buf)
2213{
2214 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2215
2216 /* Populate the user ownership fields S-1-5-88-1 */
2217 sids->owner.Revision = 1;
2218 sids->owner.NumAuth = 3;
2219 sids->owner.Authority[5] = 5;
2220 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2221 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2222 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2223
2224 /* Populate the group ownership fields S-1-5-88-2 */
2225 sids->group.Revision = 1;
2226 sids->group.NumAuth = 3;
2227 sids->group.Authority[5] = 5;
2228 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2229 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2230 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
Steve Frencha7a519a2020-06-12 14:49:47 -05002231
2232 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 -05002233}
2234
Steve Frenchfdef6652019-12-06 02:02:38 -06002235/* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2236static struct crt_sd_ctxt *
Steve French975221e2020-06-12 09:25:21 -05002237create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
Steve Frenchfdef6652019-12-06 02:02:38 -06002238{
2239 struct crt_sd_ctxt *buf;
2240 struct cifs_ace *pace;
2241 unsigned int sdlen, acelen;
Steve French975221e2020-06-12 09:25:21 -05002242 unsigned int owner_offset = 0;
2243 unsigned int group_offset = 0;
Steve Frenchfdef6652019-12-06 02:02:38 -06002244
Steve French975221e2020-06-12 09:25:21 -05002245 *len = roundup(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 2), 8);
2246
2247 if (set_owner) {
2248 /* offset fields are from beginning of security descriptor not of create context */
2249 owner_offset = sizeof(struct smb3_acl) + (sizeof(struct cifs_ace) * 2);
2250
2251 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2252 *len += sizeof(struct owner_group_sids);
2253 }
2254
Steve Frenchfdef6652019-12-06 02:02:38 -06002255 buf = kzalloc(*len, GFP_KERNEL);
2256 if (buf == NULL)
2257 return buf;
2258
Steve French975221e2020-06-12 09:25:21 -05002259 if (set_owner) {
2260 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2261 group_offset = owner_offset + sizeof(struct owner_sid);
2262 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2263 } else {
2264 buf->sd.OffsetOwner = 0;
2265 buf->sd.OffsetGroup = 0;
2266 }
2267
Steve Frenchfdef6652019-12-06 02:02:38 -06002268 sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) +
Steve French643fbce2020-01-16 19:55:33 -06002269 2 * sizeof(struct cifs_ace);
Steve French975221e2020-06-12 09:25:21 -05002270 if (set_owner) {
2271 sdlen += sizeof(struct owner_group_sids);
2272 setup_owner_group_sids(owner_offset + sizeof(struct create_context) + 8 /* name */
2273 + (char *)buf);
2274 }
Steve Frenchfdef6652019-12-06 02:02:38 -06002275
2276 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2277 (struct crt_sd_ctxt, sd));
2278 buf->ccontext.DataLength = cpu_to_le32(sdlen);
Steve French975221e2020-06-12 09:25:21 -05002279 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
Steve Frenchfdef6652019-12-06 02:02:38 -06002280 buf->ccontext.NameLength = cpu_to_le16(4);
2281 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2282 buf->Name[0] = 'S';
2283 buf->Name[1] = 'e';
2284 buf->Name[2] = 'c';
2285 buf->Name[3] = 'D';
2286 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2287 /*
2288 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2289 * and "DP" ie the DACL is present
2290 */
2291 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2292
2293 /* offset owner, group and Sbz1 and SACL are all zero */
2294 buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd));
2295 buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2296
2297 /* create one ACE to hold the mode embedded in reserved special SID */
2298 pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf);
2299 acelen = setup_special_mode_ACE(pace, (__u64)mode);
Steve French975221e2020-06-12 09:25:21 -05002300
2301 if (set_owner) {
2302 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2303 pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) + (char *)buf));
2304 acelen += setup_special_user_owner_ACE(pace);
2305 /* it does not appear necessary to add an ACE for the NFS group SID */
2306 buf->acl.AceCount = cpu_to_le16(3);
2307 } else
2308 buf->acl.AceCount = cpu_to_le16(2);
2309
Steve French643fbce2020-01-16 19:55:33 -06002310 /* and one more ACE to allow access for authenticated users */
2311 pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) +
2312 (char *)buf));
2313 acelen += setup_authusers_ACE(pace);
Steve French975221e2020-06-12 09:25:21 -05002314
Steve Frenchfdef6652019-12-06 02:02:38 -06002315 buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen);
Steve French975221e2020-06-12 09:25:21 -05002316
Steve Frenchfdef6652019-12-06 02:02:38 -06002317 return buf;
2318}
2319
2320static int
Steve French975221e2020-06-12 09:25:21 -05002321add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
Steve Frenchfdef6652019-12-06 02:02:38 -06002322{
2323 struct smb2_create_req *req = iov[0].iov_base;
2324 unsigned int num = *num_iovec;
2325 unsigned int len = 0;
2326
Steve French975221e2020-06-12 09:25:21 -05002327 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
Steve Frenchfdef6652019-12-06 02:02:38 -06002328 if (iov[num].iov_base == NULL)
2329 return -ENOMEM;
2330 iov[num].iov_len = len;
2331 if (!req->CreateContextsOffset)
2332 req->CreateContextsOffset = cpu_to_le32(
2333 sizeof(struct smb2_create_req) +
2334 iov[num - 1].iov_len);
2335 le32_add_cpu(&req->CreateContextsLength, len);
2336 *num_iovec = num + 1;
2337 return 0;
2338}
2339
Steve Frenchff2a09e2019-07-06 14:41:38 -05002340static struct crt_query_id_ctxt *
2341create_query_id_buf(void)
2342{
2343 struct crt_query_id_ctxt *buf;
2344
2345 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2346 if (!buf)
2347 return NULL;
2348
2349 buf->ccontext.DataOffset = cpu_to_le16(0);
2350 buf->ccontext.DataLength = cpu_to_le32(0);
2351 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2352 (struct crt_query_id_ctxt, Name));
2353 buf->ccontext.NameLength = cpu_to_le16(4);
2354 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2355 buf->Name[0] = 'Q';
2356 buf->Name[1] = 'F';
2357 buf->Name[2] = 'i';
2358 buf->Name[3] = 'd';
2359 return buf;
2360}
2361
2362/* See MS-SMB2 2.2.13.2.9 */
2363static int
2364add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2365{
2366 struct smb2_create_req *req = iov[0].iov_base;
2367 unsigned int num = *num_iovec;
2368
2369 iov[num].iov_base = create_query_id_buf();
2370 if (iov[num].iov_base == NULL)
2371 return -ENOMEM;
2372 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2373 if (!req->CreateContextsOffset)
2374 req->CreateContextsOffset = cpu_to_le32(
2375 sizeof(struct smb2_create_req) +
2376 iov[num - 1].iov_len);
2377 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2378 *num_iovec = num + 1;
2379 return 0;
2380}
2381
Aurelien Aptelf0712922017-02-22 14:47:17 +01002382static int
2383alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2384 const char *treename, const __le16 *path)
2385{
2386 int treename_len, path_len;
2387 struct nls_table *cp;
2388 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2389
2390 /*
2391 * skip leading "\\"
2392 */
2393 treename_len = strlen(treename);
2394 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2395 return -EINVAL;
2396
2397 treename += 2;
2398 treename_len -= 2;
2399
2400 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2401
2402 /*
2403 * make room for one path separator between the treename and
2404 * path
2405 */
2406 *out_len = treename_len + 1 + path_len;
2407
2408 /*
2409 * final path needs to be null-terminated UTF16 with a
2410 * size aligned to 8
2411 */
2412
2413 *out_size = roundup((*out_len+1)*2, 8);
2414 *out_path = kzalloc(*out_size, GFP_KERNEL);
2415 if (!*out_path)
2416 return -ENOMEM;
2417
2418 cp = load_nls_default();
2419 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2420 UniStrcat(*out_path, sep);
2421 UniStrcat(*out_path, path);
2422 unload_nls(cp);
2423
2424 return 0;
2425}
2426
Steve Frenchbea851b2018-06-14 21:56:32 -05002427int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2428 umode_t mode, struct cifs_tcon *tcon,
2429 const char *full_path,
2430 struct cifs_sb_info *cifs_sb)
2431{
2432 struct smb_rqst rqst;
2433 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002434 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002435 struct cifs_ses *ses = tcon->ses;
2436 struct kvec iov[3]; /* make sure at least one for each open context */
2437 struct kvec rsp_iov = {NULL, 0};
2438 int resp_buftype;
2439 int uni_path_len;
2440 __le16 *copy_path = NULL;
2441 int copy_size;
2442 int rc = 0;
2443 unsigned int n_iov = 2;
2444 __u32 file_attributes = 0;
2445 char *pc_buf = NULL;
2446 int flags = 0;
2447 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002448 __le16 *utf16_path = NULL;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002449 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Steve Frenchbea851b2018-06-14 21:56:32 -05002450
2451 cifs_dbg(FYI, "mkdir\n");
2452
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002453 /* resource #1: path allocation */
2454 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2455 if (!utf16_path)
2456 return -ENOMEM;
2457
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002458 if (!ses || !server) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002459 rc = -EIO;
2460 goto err_free_path;
2461 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002462
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002463 /* resource #2: request */
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002464 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2465 (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002466 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002467 goto err_free_path;
2468
Steve Frenchbea851b2018-06-14 21:56:32 -05002469
2470 if (smb3_encryption_required(tcon))
2471 flags |= CIFS_TRANSFORM_REQ;
2472
Steve Frenchbea851b2018-06-14 21:56:32 -05002473 req->ImpersonationLevel = IL_IMPERSONATION;
2474 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2475 /* File attributes ignored on open (used in create though) */
2476 req->FileAttributes = cpu_to_le32(file_attributes);
2477 req->ShareAccess = FILE_SHARE_ALL_LE;
2478 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2479 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2480
2481 iov[0].iov_base = (char *)req;
2482 /* -1 since last byte is buf[0] which is sent below (path) */
2483 iov[0].iov_len = total_len - 1;
2484
2485 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2486
2487 /* [MS-SMB2] 2.2.13 NameOffset:
2488 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2489 * the SMB2 header, the file name includes a prefix that will
2490 * be processed during DFS name normalization as specified in
2491 * section 3.3.5.9. Otherwise, the file name is relative to
2492 * the share that is identified by the TreeId in the SMB2
2493 * header.
2494 */
2495 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2496 int name_len;
2497
2498 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2499 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2500 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002501 tcon->treeName, utf16_path);
2502 if (rc)
2503 goto err_free_req;
2504
Steve Frenchbea851b2018-06-14 21:56:32 -05002505 req->NameLength = cpu_to_le16(name_len * 2);
2506 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002507 /* free before overwriting resource */
2508 kfree(utf16_path);
2509 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002510 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002511 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002512 /* MUST set path len (NameLength) to 0 opening root of share */
2513 req->NameLength = cpu_to_le16(uni_path_len - 2);
2514 if (uni_path_len % 8 != 0) {
2515 copy_size = roundup(uni_path_len, 8);
2516 copy_path = kzalloc(copy_size, GFP_KERNEL);
2517 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002518 rc = -ENOMEM;
2519 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002520 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002521 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002522 uni_path_len);
2523 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002524 /* free before overwriting resource */
2525 kfree(utf16_path);
2526 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002527 }
2528 }
2529
2530 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002531 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002532 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2533
2534 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002535 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002536 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002537 if (rc)
2538 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002539 pc_buf = iov[n_iov-1].iov_base;
2540 }
2541
2542
2543 memset(&rqst, 0, sizeof(struct smb_rqst));
2544 rqst.rq_iov = iov;
2545 rqst.rq_nvec = n_iov;
2546
Steve Frenchd2f15422019-09-22 00:55:46 -05002547 /* no need to inc num_remote_opens because we close it just below */
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002548 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2549 FILE_WRITE_ATTRIBUTES);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002550 /* resource #4: response buffer */
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002551 rc = cifs_send_recv(xid, ses, server,
2552 &rqst, &resp_buftype, flags, &rsp_iov);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002553 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002554 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2555 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002556 CREATE_NOT_FILE,
2557 FILE_WRITE_ATTRIBUTES, rc);
2558 goto err_free_rsp_buf;
2559 }
2560
2561 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2562 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2563 ses->Suid, CREATE_NOT_FILE,
2564 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002565
2566 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2567
2568 /* Eventually save off posix specific response info and timestaps */
2569
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002570err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002571 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002572 kfree(pc_buf);
2573err_free_req:
2574 cifs_small_buf_release(req);
2575err_free_path:
2576 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002577 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002578}
Steve Frenchbea851b2018-06-14 21:56:32 -05002579
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002580int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002581SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2582 struct smb_rqst *rqst, __u8 *oplock,
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002583 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002584{
2585 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002586 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002587 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002588 int copy_size;
2589 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002590 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002591 struct kvec *iov = rqst->rq_iov;
2592 __le16 *copy_path;
2593 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002594
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002595 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2596 (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002597 if (rc)
2598 return rc;
2599
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002600 iov[0].iov_base = (char *)req;
2601 /* -1 since last byte is buf[0] which is sent below (path) */
2602 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002603
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002604 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002605 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002606 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2607 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002608
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002609 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002610 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002611 /* File attributes ignored on open (used in create though) */
2612 req->FileAttributes = cpu_to_le32(file_attributes);
2613 req->ShareAccess = FILE_SHARE_ALL_LE;
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002614
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002615 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2616 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002617 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002618
2619 /* [MS-SMB2] 2.2.13 NameOffset:
2620 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2621 * the SMB2 header, the file name includes a prefix that will
2622 * be processed during DFS name normalization as specified in
2623 * section 3.3.5.9. Otherwise, the file name is relative to
2624 * the share that is identified by the TreeId in the SMB2
2625 * header.
2626 */
2627 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2628 int name_len;
2629
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002630 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002631 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2632 &name_len,
2633 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002634 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002635 return rc;
2636 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002637 uni_path_len = copy_size;
2638 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002639 } else {
2640 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2641 /* MUST set path len (NameLength) to 0 opening root of share */
2642 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002643 copy_size = uni_path_len;
2644 if (copy_size % 8 != 0)
2645 copy_size = roundup(copy_size, 8);
2646 copy_path = kzalloc(copy_size, GFP_KERNEL);
2647 if (!copy_path)
2648 return -ENOMEM;
2649 memcpy((char *)copy_path, (const char *)path,
2650 uni_path_len);
2651 uni_path_len = copy_size;
2652 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002653 }
2654
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002655 iov[1].iov_len = uni_path_len;
2656 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002657
Steve French3e7a02d2019-09-11 21:46:20 -05002658 if ((!server->oplocks) || (tcon->no_lease))
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002659 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2660
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002661 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002662 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2663 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002664 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2665 (oparms->create_options & CREATE_NOT_FILE))
2666 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002667 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002668 rc = add_lease_context(server, iov, &n_iov,
2669 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002670 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002671 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002672 }
2673
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002674 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2675 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002676 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002677 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002678 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002679 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002680 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002681 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002682
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002683 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002684 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002685 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002686 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002687 }
2688
Steve Frenchce558b02018-05-31 19:16:54 -05002689 if (tcon->posix_extensions) {
2690 if (n_iov > 2) {
2691 struct create_context *ccontext =
2692 (struct create_context *)iov[n_iov-1].iov_base;
2693 ccontext->Next =
2694 cpu_to_le32(iov[n_iov-1].iov_len);
2695 }
2696
2697 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002698 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002699 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002700 }
Steve Frenchce558b02018-05-31 19:16:54 -05002701
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002702 if (tcon->snapshot_time) {
2703 cifs_dbg(FYI, "adding snapshot context\n");
2704 if (n_iov > 2) {
2705 struct create_context *ccontext =
2706 (struct create_context *)iov[n_iov-1].iov_base;
2707 ccontext->Next =
2708 cpu_to_le32(iov[n_iov-1].iov_len);
2709 }
2710
2711 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2712 if (rc)
2713 return rc;
2714 }
2715
Steve French975221e2020-06-12 09:25:21 -05002716 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
2717 bool set_mode;
2718 bool set_owner;
2719
2720 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2721 (oparms->mode != ACL_NO_MODE))
2722 set_mode = true;
2723 else {
2724 set_mode = false;
2725 oparms->mode = ACL_NO_MODE;
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002726 }
2727
Steve French975221e2020-06-12 09:25:21 -05002728 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
2729 set_owner = true;
2730 else
2731 set_owner = false;
2732
2733 if (set_owner | set_mode) {
2734 if (n_iov > 2) {
2735 struct create_context *ccontext =
2736 (struct create_context *)iov[n_iov-1].iov_base;
2737 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2738 }
2739
2740 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2741 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
2742 if (rc)
2743 return rc;
2744 }
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002745 }
2746
Steve Frenchff2a09e2019-07-06 14:41:38 -05002747 if (n_iov > 2) {
2748 struct create_context *ccontext =
2749 (struct create_context *)iov[n_iov-1].iov_base;
2750 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2751 }
2752 add_query_id_context(iov, &n_iov);
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002753
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002754 rqst->rq_nvec = n_iov;
2755 return 0;
2756}
2757
2758/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2759 * All other vectors are freed by kfree().
2760 */
2761void
2762SMB2_open_free(struct smb_rqst *rqst)
2763{
2764 int i;
2765
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002766 if (rqst && rqst->rq_iov) {
2767 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2768 for (i = 1; i < rqst->rq_nvec; i++)
2769 if (rqst->rq_iov[i].iov_base != smb2_padding)
2770 kfree(rqst->rq_iov[i].iov_base);
2771 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002772}
2773
2774int
2775SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2776 __u8 *oplock, struct smb2_file_all_info *buf,
Aurelien Aptel69dda302020-03-02 17:53:22 +01002777 struct create_posix_rsp *posix,
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002778 struct kvec *err_iov, int *buftype)
2779{
2780 struct smb_rqst rqst;
2781 struct smb2_create_rsp *rsp = NULL;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002782 struct cifs_tcon *tcon = oparms->tcon;
2783 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002784 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002785 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002786 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002787 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002788 int rc = 0;
2789 int flags = 0;
2790
2791 cifs_dbg(FYI, "create/open\n");
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002792 if (!ses || !server)
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002793 return -EIO;
2794
2795 if (smb3_encryption_required(tcon))
2796 flags |= CIFS_TRANSFORM_REQ;
2797
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002798 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002799 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002800 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002801 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002802
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002803 rc = SMB2_open_init(tcon, server,
2804 &rqst, oplock, oparms, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002805 if (rc)
2806 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002807
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002808 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2809 oparms->create_options, oparms->desired_access);
2810
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002811 rc = cifs_send_recv(xid, ses, server,
2812 &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002813 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002814 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002815
2816 if (rc != 0) {
2817 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002818 if (err_iov && rsp) {
2819 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002820 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002821 resp_buftype = CIFS_NO_BUFFER;
2822 rsp = NULL;
2823 }
Steve French28d59362018-05-30 21:42:34 -05002824 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2825 oparms->create_options, oparms->desired_access, rc);
Steve French7dcc82c2019-09-11 00:07:36 -05002826 if (rc == -EREMCHG) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002827 pr_warn_once("server share %s deleted\n",
2828 tcon->treeName);
Steve French7dcc82c2019-09-11 00:07:36 -05002829 tcon->need_reconnect = true;
2830 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002831 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002832 } else
2833 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2834 ses->Suid, oparms->create_options,
2835 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002836
Steve Frenchfae80442018-10-19 17:14:32 -05002837 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002838 oparms->fid->persistent_fid = rsp->PersistentFileId;
2839 oparms->fid->volatile_fid = rsp->VolatileFileId;
Aurelien Aptel86f740f2020-02-21 11:19:06 +01002840 oparms->fid->access = oparms->desired_access;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002841#ifdef CONFIG_CIFS_DEBUG2
2842 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2843#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002844
2845 if (buf) {
2846 memcpy(buf, &rsp->CreationTime, 32);
2847 buf->AllocationSize = rsp->AllocationSize;
2848 buf->EndOfFile = rsp->EndofFile;
2849 buf->Attributes = rsp->FileAttributes;
2850 buf->NumberOfLinks = cpu_to_le32(1);
2851 buf->DeletePending = 0;
2852 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002853
Steve French89a5bfa2019-07-18 17:22:18 -05002854
2855 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
Aurelien Aptel69dda302020-03-02 17:53:22 +01002856 oparms->fid->lease_key, oplock, buf, posix);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002857creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002858 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002859 free_rsp_buf(resp_buftype, rsp);
2860 return rc;
2861}
2862
Steve French4a72daf2013-06-25 00:20:49 -05002863int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002864SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2865 struct smb_rqst *rqst,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002866 u64 persistent_fid, u64 volatile_fid, u32 opcode,
Steve French153322f2019-03-28 22:32:49 -05002867 bool is_fsctl, char *in_data, u32 indatalen,
2868 __u32 max_response_size)
Steve French4a72daf2013-06-25 00:20:49 -05002869{
2870 struct smb2_ioctl_req *req;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002871 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002872 unsigned int total_len;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002873 int rc;
Long Li2c87d6a2019-05-15 14:09:05 -07002874 char *in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002875
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002876 rc = smb2_ioctl_req_init(opcode, tcon, server,
2877 (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002878 if (rc)
2879 return rc;
2880
Long Li2c87d6a2019-05-15 14:09:05 -07002881 if (indatalen) {
2882 /*
2883 * indatalen is usually small at a couple of bytes max, so
2884 * just allocate through generic pool
2885 */
YueHaibingd81f0972019-06-01 03:31:10 +00002886 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
Long Li2c87d6a2019-05-15 14:09:05 -07002887 if (!in_data_buf) {
2888 cifs_small_buf_release(req);
2889 return -ENOMEM;
2890 }
Long Li2c87d6a2019-05-15 14:09:05 -07002891 }
2892
Steve French4a72daf2013-06-25 00:20:49 -05002893 req->CtlCode = cpu_to_le32(opcode);
2894 req->PersistentFileId = persistent_fid;
2895 req->VolatileFileId = volatile_fid;
2896
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002897 iov[0].iov_base = (char *)req;
2898 /*
2899 * If no input data, the size of ioctl struct in
2900 * protocol spec still includes a 1 byte data buffer,
2901 * but if input data passed to ioctl, we do not
2902 * want to double count this, so we do not send
2903 * the dummy one byte of data in iovec[0] if sending
2904 * input data (in iovec[1]).
2905 */
Steve French4a72daf2013-06-25 00:20:49 -05002906 if (indatalen) {
2907 req->InputCount = cpu_to_le32(indatalen);
2908 /* do not set InputOffset if no input data */
2909 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002910 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002911 rqst->rq_nvec = 2;
2912 iov[0].iov_len = total_len - 1;
Long Li2c87d6a2019-05-15 14:09:05 -07002913 iov[1].iov_base = in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002914 iov[1].iov_len = indatalen;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002915 } else {
2916 rqst->rq_nvec = 1;
2917 iov[0].iov_len = total_len;
2918 }
Steve French4a72daf2013-06-25 00:20:49 -05002919
2920 req->OutputOffset = 0;
2921 req->OutputCount = 0; /* MBZ */
2922
2923 /*
Steve French153322f2019-03-28 22:32:49 -05002924 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2925 * We Could increase default MaxOutputResponse, but that could require
2926 * more credits. Windows typically sets this smaller, but for some
Steve French4a72daf2013-06-25 00:20:49 -05002927 * ioctls it may be useful to allow server to send more. No point
2928 * limiting what the server can send as long as fits in one credit
Steve French153322f2019-03-28 22:32:49 -05002929 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2930 * to increase this limit up in the future.
2931 * Note that for snapshot queries that servers like Azure expect that
2932 * the first query be minimal size (and just used to get the number/size
2933 * of previous versions) so response size must be specified as EXACTLY
2934 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2935 * of eight bytes. Currently that is the only case where we set max
2936 * response size smaller.
Steve French4a72daf2013-06-25 00:20:49 -05002937 */
Steve French153322f2019-03-28 22:32:49 -05002938 req->MaxOutputResponse = cpu_to_le32(max_response_size);
Namjae Jeonebf57442020-06-11 11:21:19 +09002939 req->sync_hdr.CreditCharge =
2940 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
2941 SMB2_MAX_BUFFER_SIZE));
Steve French4a72daf2013-06-25 00:20:49 -05002942 if (is_fsctl)
2943 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2944 else
2945 req->Flags = 0;
2946
Steve French4587eee2017-10-25 15:58:31 -05002947 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2948 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002949 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002950
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002951 return 0;
2952}
2953
2954void
2955SMB2_ioctl_free(struct smb_rqst *rqst)
2956{
Murphy Zhou6457c202019-05-23 12:12:43 +08002957 int i;
Long Li2c87d6a2019-05-15 14:09:05 -07002958 if (rqst && rqst->rq_iov) {
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002959 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Murphy Zhou6457c202019-05-23 12:12:43 +08002960 for (i = 1; i < rqst->rq_nvec; i++)
2961 if (rqst->rq_iov[i].iov_base != smb2_padding)
2962 kfree(rqst->rq_iov[i].iov_base);
Long Li2c87d6a2019-05-15 14:09:05 -07002963 }
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002964}
2965
Steve French153322f2019-03-28 22:32:49 -05002966
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002967/*
2968 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2969 */
2970int
2971SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2972 u64 volatile_fid, u32 opcode, bool is_fsctl,
Steve French153322f2019-03-28 22:32:49 -05002973 char *in_data, u32 indatalen, u32 max_out_data_len,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002974 char **out_data, u32 *plen /* returned data len */)
2975{
2976 struct smb_rqst rqst;
2977 struct smb2_ioctl_rsp *rsp = NULL;
2978 struct cifs_ses *ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002979 struct TCP_Server_Info *server;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002980 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002981 struct kvec rsp_iov = {NULL, 0};
2982 int resp_buftype = CIFS_NO_BUFFER;
2983 int rc = 0;
2984 int flags = 0;
2985
2986 cifs_dbg(FYI, "SMB2 IOCTL\n");
2987
2988 if (out_data != NULL)
2989 *out_data = NULL;
2990
2991 /* zero out returned data len, in case of error */
2992 if (plen)
2993 *plen = 0;
2994
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002995 if (!tcon)
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002996 return -EIO;
2997
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002998 ses = tcon->ses;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01002999 if (!ses)
3000 return -EIO;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003001
3002 server = cifs_pick_channel(ses);
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003003 if (!server)
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003004 return -EIO;
3005
3006 if (smb3_encryption_required(tcon))
3007 flags |= CIFS_TRANSFORM_REQ;
3008
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003009 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003010 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003011 rqst.rq_iov = iov;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003012 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003013
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003014 rc = SMB2_ioctl_init(tcon, server,
3015 &rqst, persistent_fid, volatile_fid, opcode,
Steve French153322f2019-03-28 22:32:49 -05003016 is_fsctl, in_data, indatalen, max_out_data_len);
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003017 if (rc)
3018 goto ioctl_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003019
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003020 rc = cifs_send_recv(xid, ses, server,
3021 &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11003022 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003023 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05003024
Steve Frencheccb4422018-05-17 21:16:55 -05003025 if (rc != 0)
3026 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3027 ses->Suid, 0, opcode, rc);
3028
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003029 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
Steve French8e353102015-03-26 19:47:02 -05003030 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05003031 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06003032 } else if (rc == -EINVAL) {
3033 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3034 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05003035 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06003036 goto ioctl_exit;
3037 }
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003038 } else if (rc == -E2BIG) {
3039 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3040 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3041 goto ioctl_exit;
3042 }
Steve French4a72daf2013-06-25 00:20:49 -05003043 }
3044
3045 /* check if caller wants to look at return data or just return rc */
3046 if ((plen == NULL) || (out_data == NULL))
3047 goto ioctl_exit;
3048
3049 *plen = le32_to_cpu(rsp->OutputCount);
3050
3051 /* We check for obvious errors in the output buffer length and offset */
3052 if (*plen == 0)
3053 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03003054 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003055 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
Steve French4a72daf2013-06-25 00:20:49 -05003056 *plen = 0;
3057 rc = -EIO;
3058 goto ioctl_exit;
3059 }
3060
Dan Carpenter2d204ee2018-09-10 14:12:07 +03003061 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003062 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
Steve French4a72daf2013-06-25 00:20:49 -05003063 le32_to_cpu(rsp->OutputOffset));
3064 *plen = 0;
3065 rc = -EIO;
3066 goto ioctl_exit;
3067 }
3068
YueHaibingd034fee2018-09-10 01:33:06 +00003069 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3070 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05003071 if (*out_data == NULL) {
3072 rc = -ENOMEM;
3073 goto ioctl_exit;
3074 }
3075
Steve French4a72daf2013-06-25 00:20:49 -05003076ioctl_exit:
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003077 SMB2_ioctl_free(&rqst);
Steve French4a72daf2013-06-25 00:20:49 -05003078 free_rsp_buf(resp_buftype, rsp);
3079 return rc;
3080}
3081
Steve French64a5cfa2013-10-14 15:31:32 -05003082/*
3083 * Individual callers to ioctl worker function follow
3084 */
3085
3086int
3087SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3088 u64 persistent_fid, u64 volatile_fid)
3089{
3090 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05003091 struct compress_ioctl fsctl_input;
3092 char *ret_data = NULL;
3093
3094 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08003095 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05003096
3097 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3098 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3099 (char *)&fsctl_input /* data input */,
Steve French153322f2019-03-28 22:32:49 -05003100 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3101 &ret_data /* out data */, NULL);
Steve French64a5cfa2013-10-14 15:31:32 -05003102
3103 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05003104
3105 return rc;
3106}
3107
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003108int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003109SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3110 struct smb_rqst *rqst,
Steve French43f8a6a2019-12-02 21:46:54 -06003111 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003112{
3113 struct smb2_close_req *req;
3114 struct kvec *iov = rqst->rq_iov;
3115 unsigned int total_len;
3116 int rc;
3117
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003118 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3119 (void **) &req, &total_len);
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003120 if (rc)
3121 return rc;
3122
3123 req->PersistentFileId = persistent_fid;
3124 req->VolatileFileId = volatile_fid;
Steve French43f8a6a2019-12-02 21:46:54 -06003125 if (query_attrs)
3126 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3127 else
3128 req->Flags = 0;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003129 iov[0].iov_base = (char *)req;
3130 iov[0].iov_len = total_len;
3131
3132 return 0;
3133}
3134
3135void
3136SMB2_close_free(struct smb_rqst *rqst)
3137{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003138 if (rqst && rqst->rq_iov)
3139 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003140}
3141
3142int
Steve French43f8a6a2019-12-02 21:46:54 -06003143__SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3144 u64 persistent_fid, u64 volatile_fid,
3145 struct smb2_file_network_open_info *pbuf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003146{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003147 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003148 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003149 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003150 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003151 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003152 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01003153 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003154 int rc = 0;
Steve French9e8fae22019-12-02 17:55:41 -06003155 int flags = 0;
Steve French43f8a6a2019-12-02 21:46:54 -06003156 bool query_attrs = false;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003157
Joe Perchesf96637b2013-05-04 22:12:25 -05003158 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003159
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003160 if (!ses || !server)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003161 return -EIO;
3162
Steve French5a77e752018-05-09 17:43:08 -05003163 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003164 flags |= CIFS_TRANSFORM_REQ;
3165
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003166 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003167 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003168 rqst.rq_iov = iov;
3169 rqst.rq_nvec = 1;
3170
Steve French43f8a6a2019-12-02 21:46:54 -06003171 /* check if need to ask server to return timestamps in close response */
3172 if (pbuf)
3173 query_attrs = true;
3174
Steve Frenchf90f9792019-09-03 18:35:42 -05003175 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003176 rc = SMB2_close_init(tcon, server,
3177 &rqst, persistent_fid, volatile_fid,
Steve French43f8a6a2019-12-02 21:46:54 -06003178 query_attrs);
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003179 if (rc)
3180 goto close_exit;
3181
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003182 rc = cifs_send_recv(xid, ses, server,
3183 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003184 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003185
3186 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09003187 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003188 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3189 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003190 goto close_exit;
Steve French43f8a6a2019-12-02 21:46:54 -06003191 } else {
Steve Frenchf90f9792019-09-03 18:35:42 -05003192 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3193 ses->Suid);
Steve French43f8a6a2019-12-02 21:46:54 -06003194 /*
3195 * Note that have to subtract 4 since struct network_open_info
3196 * has a final 4 byte pad that close response does not have
3197 */
3198 if (pbuf)
3199 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3200 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003201
Steve Frenchfae80442018-10-19 17:14:32 -05003202 atomic_dec(&tcon->num_remote_opens);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003203close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003204 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003205 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003206
3207 /* retry close in a worker thread if this one is interrupted */
3208 if (rc == -EINTR) {
Steve French9e8fae22019-12-02 17:55:41 -06003209 int tmp_rc;
3210
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003211 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3212 volatile_fid);
3213 if (tmp_rc)
3214 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3215 persistent_fid, tmp_rc);
3216 }
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003217 return rc;
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06003218}
3219
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003220int
Steve French43f8a6a2019-12-02 21:46:54 -06003221SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3222 u64 persistent_fid, u64 volatile_fid)
3223{
3224 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3225}
3226
3227int
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003228smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3229 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003230{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003231 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003232 char *end_of_smb = smb_len + (char *)iov->iov_base;
3233 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003234 char *end_of_buf = begin_of_buf + buffer_length;
3235
3236
3237 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003238 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3239 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003240 return -EINVAL;
3241 }
3242
3243 /* check if beyond RFC1001 maximum length */
3244 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003245 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3246 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003247 return -EINVAL;
3248 }
3249
3250 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07003251 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003252 return -EINVAL;
3253 }
3254
3255 return 0;
3256}
3257
3258/*
3259 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3260 * Caller must free buffer.
3261 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003262int
3263smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3264 struct kvec *iov, unsigned int minbufsize,
3265 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003266{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003267 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003268 int rc;
3269
3270 if (!data)
3271 return -EINVAL;
3272
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003273 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003274 if (rc)
3275 return rc;
3276
3277 memcpy(data, begin_of_buf, buffer_length);
3278
3279 return 0;
3280}
3281
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003282int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003283SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3284 struct smb_rqst *rqst,
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003285 u64 persistent_fid, u64 volatile_fid,
3286 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003287 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003288{
3289 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003290 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003291 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003292 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003293
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003294 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3295 (void **) &req, &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003296 if (rc)
3297 return rc;
3298
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003299 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003300 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003301 req->PersistentFileId = persistent_fid;
3302 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003303 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02003304
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003305 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003306 if (input_len) {
3307 req->InputBufferLength = cpu_to_le32(input_len);
3308 /* total_len for smb query request never close to le16 max */
3309 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3310 memcpy(req->Buffer, input, input_len);
3311 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003312
3313 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003314 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003315 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003316 return 0;
3317}
3318
3319void
3320SMB2_query_info_free(struct smb_rqst *rqst)
3321{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003322 if (rqst && rqst->rq_iov)
3323 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003324}
3325
3326static int
3327query_info(const unsigned int xid, struct cifs_tcon *tcon,
3328 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3329 u32 additional_info, size_t output_len, size_t min_len, void **data,
3330 u32 *dlen)
3331{
3332 struct smb_rqst rqst;
3333 struct smb2_query_info_rsp *rsp = NULL;
3334 struct kvec iov[1];
3335 struct kvec rsp_iov;
3336 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01003337 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003338 struct cifs_ses *ses = tcon->ses;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003339 struct TCP_Server_Info *server;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003340 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003341 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003342
3343 cifs_dbg(FYI, "Query Info\n");
3344
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003345 if (!ses)
3346 return -EIO;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003347 server = cifs_pick_channel(ses);
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003348 if (!server)
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003349 return -EIO;
3350
3351 if (smb3_encryption_required(tcon))
3352 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003353
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003354 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003355 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003356 rqst.rq_iov = iov;
3357 rqst.rq_nvec = 1;
3358
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003359 rc = SMB2_query_info_init(tcon, server,
3360 &rqst, persistent_fid, volatile_fid,
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003361 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003362 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003363 if (rc)
3364 goto qinf_exit;
3365
Steve Frenchd42043a2019-02-26 21:58:30 -06003366 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3367 ses->Suid, info_class, (__u32)info_type);
3368
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003369 rc = cifs_send_recv(xid, ses, server,
3370 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003371 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003372
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003373 if (rc) {
3374 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003375 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3376 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003377 goto qinf_exit;
3378 }
3379
Steve Frenchd42043a2019-02-26 21:58:30 -06003380 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3381 ses->Suid, info_class, (__u32)info_type);
3382
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003383 if (dlen) {
3384 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3385 if (!*data) {
3386 *data = kmalloc(*dlen, GFP_KERNEL);
3387 if (!*data) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003388 cifs_tcon_dbg(VFS,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003389 "Error %d allocating memory for acl\n",
3390 rc);
3391 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003392 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003393 goto qinf_exit;
3394 }
Colin Ian King73aaf922019-01-16 16:28:59 +00003395 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003396 }
3397 }
3398
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003399 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3400 le32_to_cpu(rsp->OutputBufferLength),
3401 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00003402 if (rc && allocated) {
3403 kfree(*data);
3404 *data = NULL;
3405 *dlen = 0;
3406 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003407
3408qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003409 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003410 free_rsp_buf(resp_buftype, rsp);
3411 return rc;
3412}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003413
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003414int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3415 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003416{
3417 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003418 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04003419 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003420 sizeof(struct smb2_file_all_info), (void **)&data,
3421 NULL);
3422}
3423
3424int
Steve Frenchb1bc1872020-06-11 20:23:38 -05003425SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3426 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3427{
3428 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3429 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3430 *plen = 0;
3431
3432 return query_info(xid, tcon, persistent_fid, volatile_fid,
3433 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3434 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3435}
3436
3437int
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003438SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3439 u64 persistent_fid, u64 volatile_fid,
3440 void **data, u32 *plen)
3441{
3442 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3443 *plen = 0;
3444
3445 return query_info(xid, tcon, persistent_fid, volatile_fid,
3446 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05003447 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003448}
3449
3450int
3451SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3452 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3453{
3454 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003455 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003456 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003457 sizeof(struct smb2_file_internal_info),
3458 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003459}
3460
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003461/*
Steve Frenchc3498182019-09-15 22:38:52 -05003462 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3463 * See MS-SMB2 2.2.35 and 2.2.36
3464 */
3465
zhengbin388962e2019-09-23 15:06:18 +08003466static int
Steve Frenchc3498182019-09-15 22:38:52 -05003467SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003468 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3469 u64 persistent_fid, u64 volatile_fid,
3470 u32 completion_filter, bool watch_tree)
Steve Frenchc3498182019-09-15 22:38:52 -05003471{
3472 struct smb2_change_notify_req *req;
3473 struct kvec *iov = rqst->rq_iov;
3474 unsigned int total_len;
3475 int rc;
3476
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003477 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3478 (void **) &req, &total_len);
Steve Frenchc3498182019-09-15 22:38:52 -05003479 if (rc)
3480 return rc;
3481
3482 req->PersistentFileId = persistent_fid;
3483 req->VolatileFileId = volatile_fid;
Steve Frenchd26c2dd2020-02-06 06:00:14 -06003484 /* See note 354 of MS-SMB2, 64K max */
Steve French52870d52019-10-01 21:25:46 -05003485 req->OutputBufferLength =
3486 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
Steve Frenchc3498182019-09-15 22:38:52 -05003487 req->CompletionFilter = cpu_to_le32(completion_filter);
3488 if (watch_tree)
3489 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3490 else
3491 req->Flags = 0;
3492
3493 iov[0].iov_base = (char *)req;
3494 iov[0].iov_len = total_len;
3495
3496 return 0;
3497}
3498
3499int
3500SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3501 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3502 u32 completion_filter)
3503{
3504 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003505 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Steve Frenchc3498182019-09-15 22:38:52 -05003506 struct smb_rqst rqst;
3507 struct kvec iov[1];
3508 struct kvec rsp_iov = {NULL, 0};
3509 int resp_buftype = CIFS_NO_BUFFER;
3510 int flags = 0;
3511 int rc = 0;
3512
3513 cifs_dbg(FYI, "change notify\n");
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003514 if (!ses || !server)
Steve Frenchc3498182019-09-15 22:38:52 -05003515 return -EIO;
3516
3517 if (smb3_encryption_required(tcon))
3518 flags |= CIFS_TRANSFORM_REQ;
3519
3520 memset(&rqst, 0, sizeof(struct smb_rqst));
3521 memset(&iov, 0, sizeof(iov));
3522 rqst.rq_iov = iov;
3523 rqst.rq_nvec = 1;
3524
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003525 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3526 persistent_fid, volatile_fid,
Steve Frenchc3498182019-09-15 22:38:52 -05003527 completion_filter, watch_tree);
3528 if (rc)
3529 goto cnotify_exit;
3530
3531 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3532 (u8)watch_tree, completion_filter);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003533 rc = cifs_send_recv(xid, ses, server,
3534 &rqst, &resp_buftype, flags, &rsp_iov);
Steve Frenchc3498182019-09-15 22:38:52 -05003535
3536 if (rc != 0) {
3537 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3538 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3539 (u8)watch_tree, completion_filter, rc);
3540 } else
3541 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3542 ses->Suid, (u8)watch_tree, completion_filter);
3543
3544 cnotify_exit:
3545 if (rqst.rq_iov)
3546 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3547 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3548 return rc;
3549}
3550
3551
3552
3553/*
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003554 * This is a no-op for now. We're not really interested in the reply, but
3555 * rather in the fact that the server sent one and that server->lstrp
3556 * gets updated.
3557 *
3558 * FIXME: maybe we should consider checking that the reply matches request?
3559 */
3560static void
3561smb2_echo_callback(struct mid_q_entry *mid)
3562{
3563 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003564 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003565 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003566
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003567 if (mid->mid_state == MID_RESPONSE_RECEIVED
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003568 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3569 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3570 credits.instance = server->reconnect_instance;
3571 }
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003572
3573 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003574 add_credits(server, &credits, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003575}
3576
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003577void smb2_reconnect_server(struct work_struct *work)
3578{
3579 struct TCP_Server_Info *server = container_of(work,
3580 struct TCP_Server_Info, reconnect.work);
3581 struct cifs_ses *ses;
3582 struct cifs_tcon *tcon, *tcon2;
3583 struct list_head tmp_list;
3584 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01003585 int rc;
3586 int resched = false;
3587
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003588
3589 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3590 mutex_lock(&server->reconnect_mutex);
3591
3592 INIT_LIST_HEAD(&tmp_list);
3593 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3594
3595 spin_lock(&cifs_tcp_ses_lock);
3596 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3597 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003598 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003599 tcon->tc_count++;
3600 list_add_tail(&tcon->rlist, &tmp_list);
3601 tcon_exist = true;
3602 }
3603 }
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003604 /*
3605 * IPC has the same lifetime as its session and uses its
3606 * refcount.
3607 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003608 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3609 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3610 tcon_exist = true;
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003611 ses->ses_count++;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003612 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003613 }
3614 /*
3615 * Get the reference to server struct to be sure that the last call of
3616 * cifs_put_tcon() in the loop below won't release the server pointer.
3617 */
3618 if (tcon_exist)
3619 server->srv_count++;
3620
3621 spin_unlock(&cifs_tcp_ses_lock);
3622
3623 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003624 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
Germano Percossi18ea4312017-04-07 12:29:36 +01003625 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003626 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01003627 else
3628 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003629 list_del_init(&tcon->rlist);
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003630 if (tcon->ipc)
3631 cifs_put_smb_ses(tcon->ses);
3632 else
3633 cifs_put_tcon(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003634 }
3635
3636 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01003637 if (resched)
3638 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003639 mutex_unlock(&server->reconnect_mutex);
3640
3641 /* now we can safely release srv struct */
3642 if (tcon_exist)
3643 cifs_put_tcp_session(server, 1);
3644}
3645
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003646int
3647SMB2_echo(struct TCP_Server_Info *server)
3648{
3649 struct smb2_echo_req *req;
3650 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003651 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003652 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003653 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003654 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003655
Joe Perchesf96637b2013-05-04 22:12:25 -05003656 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003657
Steve French4fcd1812016-06-22 20:12:05 -05003658 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003659 /* No need to send echo on newly established connections */
Stefan Metzmacherb08484d2020-02-24 14:14:59 +01003660 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003661 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003662 }
3663
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003664 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
3665 (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003666 if (rc)
3667 return rc;
3668
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003669 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003670
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003671 iov[0].iov_len = total_len;
3672 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003673
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003674 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003675 server, CIFS_ECHO_OP, NULL);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003676 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003677 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003678
3679 cifs_small_buf_release(req);
3680 return rc;
3681}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003682
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003683void
3684SMB2_flush_free(struct smb_rqst *rqst)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003685{
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003686 if (rqst && rqst->rq_iov)
3687 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3688}
3689
3690int
3691SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003692 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3693 u64 persistent_fid, u64 volatile_fid)
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003694{
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003695 struct smb2_flush_req *req;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003696 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003697 unsigned int total_len;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003698 int rc;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003699
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003700 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
3701 (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003702 if (rc)
3703 return rc;
3704
3705 req->PersistentFileId = persistent_fid;
3706 req->VolatileFileId = volatile_fid;
3707
3708 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003709 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003710
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003711 return 0;
3712}
3713
3714int
3715SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3716 u64 volatile_fid)
3717{
3718 struct cifs_ses *ses = tcon->ses;
3719 struct smb_rqst rqst;
3720 struct kvec iov[1];
3721 struct kvec rsp_iov = {NULL, 0};
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003722 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003723 int resp_buftype = CIFS_NO_BUFFER;
3724 int flags = 0;
3725 int rc = 0;
3726
3727 cifs_dbg(FYI, "flush\n");
3728 if (!ses || !(ses->server))
3729 return -EIO;
3730
3731 if (smb3_encryption_required(tcon))
3732 flags |= CIFS_TRANSFORM_REQ;
3733
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003734 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003735 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003736 rqst.rq_iov = iov;
3737 rqst.rq_nvec = 1;
3738
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003739 rc = SMB2_flush_init(xid, &rqst, tcon, server,
3740 persistent_fid, volatile_fid);
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003741 if (rc)
3742 goto flush_exit;
3743
Steve Frenchf90f9792019-09-03 18:35:42 -05003744 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003745 rc = cifs_send_recv(xid, ses, server,
3746 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003747
Steve Frencheccb4422018-05-17 21:16:55 -05003748 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003749 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003750 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3751 rc);
Steve Frenchf90f9792019-09-03 18:35:42 -05003752 } else
3753 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3754 ses->Suid);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003755
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003756 flush_exit:
3757 SMB2_flush_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003758 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003759 return rc;
3760}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003761
3762/*
3763 * To form a chain of read requests, any read requests after the first should
3764 * have the end_of_chain boolean set to true.
3765 */
3766static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003767smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003768 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3769 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003770{
3771 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003772 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003773 struct smb2_sync_hdr *shdr;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003774 struct TCP_Server_Info *server = io_parms->server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003775
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003776 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
3777 (void **) &req, total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003778 if (rc)
3779 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003780
Long Li2dabfd52017-11-07 01:54:53 -07003781 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003782 return -ECONNABORTED;
3783
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003784 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003785 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003786
3787 req->PersistentFileId = io_parms->persistent_fid;
3788 req->VolatileFileId = io_parms->volatile_fid;
3789 req->ReadChannelInfoOffset = 0; /* reserved */
3790 req->ReadChannelInfoLength = 0; /* reserved */
3791 req->Channel = 0; /* reserved */
3792 req->MinimumCount = 0;
3793 req->Length = cpu_to_le32(io_parms->length);
3794 req->Offset = cpu_to_le64(io_parms->offset);
Steve Frenchd323c2462019-02-25 00:52:43 -06003795
3796 trace_smb3_read_enter(0 /* xid */,
3797 io_parms->persistent_fid,
3798 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3799 io_parms->offset, io_parms->length);
Long Libd3dcc62017-11-22 17:38:47 -07003800#ifdef CONFIG_CIFS_SMB_DIRECT
3801 /*
3802 * If we want to do a RDMA write, fill in and append
3803 * smbd_buffer_descriptor_v1 to the end of read request
3804 */
Long Libb4c0412018-04-17 12:17:08 -07003805 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003806 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003807
Long Libd3dcc62017-11-22 17:38:47 -07003808 struct smbd_buffer_descriptor_v1 *v1;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003809 bool need_invalidate = server->dialect == SMB30_PROT_ID;
Long Libd3dcc62017-11-22 17:38:47 -07003810
3811 rdata->mr = smbd_register_mr(
3812 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003813 rdata->nr_pages, rdata->page_offset,
3814 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003815 if (!rdata->mr)
Long Lib7972092019-04-05 21:36:34 +00003816 return -EAGAIN;
Long Libd3dcc62017-11-22 17:38:47 -07003817
3818 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3819 if (need_invalidate)
3820 req->Channel = SMB2_CHANNEL_RDMA_V1;
3821 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003822 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003823 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003824 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003825 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003826 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3827 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3828 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003829
3830 *total_len += sizeof(*v1) - 1;
3831 }
3832#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003833 if (request_type & CHAINED_REQUEST) {
3834 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003835 /* next 8-byte aligned request */
3836 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3837 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003838 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003839 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003840 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003841 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003842 /*
3843 * Related requests use info from previous read request
3844 * in chain.
3845 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003846 shdr->SessionId = 0xFFFFFFFF;
3847 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003848 req->PersistentFileId = 0xFFFFFFFF;
3849 req->VolatileFileId = 0xFFFFFFFF;
3850 }
3851 }
3852 if (remaining_bytes > io_parms->length)
3853 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3854 else
3855 req->RemainingBytes = 0;
3856
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003857 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003858 return rc;
3859}
3860
3861static void
3862smb2_readv_callback(struct mid_q_entry *mid)
3863{
3864 struct cifs_readdata *rdata = mid->callback_data;
3865 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003866 struct TCP_Server_Info *server = rdata->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003867 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003868 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003869 struct cifs_credits credits = { .value = 0, .instance = 0 };
Steve French46f17d12019-09-04 23:07:52 -05003870 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3871 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07003872 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003873 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003874 .rq_npages = rdata->nr_pages,
3875 .rq_pagesz = rdata->pagesz,
3876 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003877
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003878 WARN_ONCE(rdata->server != mid->server,
3879 "rdata server %p != mid server %p",
3880 rdata->server, mid->server);
3881
Joe Perchesf96637b2013-05-04 22:12:25 -05003882 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3883 __func__, mid->mid, mid->mid_state, rdata->result,
3884 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003885
3886 switch (mid->mid_state) {
3887 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003888 credits.value = le16_to_cpu(shdr->CreditRequest);
3889 credits.instance = server->reconnect_instance;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003890 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003891 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003892 int rc;
3893
Jeff Layton0b688cf2012-09-18 16:20:34 -07003894 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003895 if (rc)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003896 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003897 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07003898 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003899 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003900 task_io_account_read(rdata->got_bytes);
3901 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003902 break;
3903 case MID_REQUEST_SUBMITTED:
3904 case MID_RETRY_NEEDED:
3905 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003906 if (server->sign && rdata->got_bytes)
3907 /* reset bytes number since we can not check a sign */
3908 rdata->got_bytes = 0;
3909 /* FIXME: should this be counted toward the initiating task? */
3910 task_io_account_read(rdata->got_bytes);
3911 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003912 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003913 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003914 credits.value = le16_to_cpu(shdr->CreditRequest);
3915 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003916 /* fall through */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003917 default:
Pavel Shilovsky6b15eb18c2019-01-18 15:46:14 -08003918 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003919 }
Long Libd3dcc62017-11-22 17:38:47 -07003920#ifdef CONFIG_CIFS_SMB_DIRECT
3921 /*
3922 * If this rdata has a memmory registered, the MR can be freed
3923 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3924 * because they have limited number and are used for future I/Os
3925 */
3926 if (rdata->mr) {
3927 smbd_deregister_mr(rdata->mr);
3928 rdata->mr = NULL;
3929 }
3930#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08003931 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003932 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08003933 trace_smb3_read_err(0 /* xid */,
3934 rdata->cfile->fid.persistent_fid,
3935 tcon->tid, tcon->ses->Suid, rdata->offset,
3936 rdata->bytes, rdata->result);
3937 } else
3938 trace_smb3_read_done(0 /* xid */,
3939 rdata->cfile->fid.persistent_fid,
3940 tcon->tid, tcon->ses->Suid,
3941 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003942
3943 queue_work(cifsiod_wq, &rdata->work);
3944 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003945 add_credits(server, &credits, 0);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003946}
3947
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003948/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003949int
3950smb2_async_readv(struct cifs_readdata *rdata)
3951{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003952 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003953 char *buf;
3954 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003955 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003956 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003957 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003958 struct TCP_Server_Info *server;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003959 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003960 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003961
Joe Perchesf96637b2013-05-04 22:12:25 -05003962 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3963 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003964
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003965 if (!rdata->server)
3966 rdata->server = cifs_pick_channel(tcon->ses);
3967
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003968 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003969 io_parms.server = server = rdata->server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003970 io_parms.offset = rdata->offset;
3971 io_parms.length = rdata->bytes;
3972 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3973 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3974 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003975
Long Li2dabfd52017-11-07 01:54:53 -07003976 rc = smb2_new_read_req(
3977 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08003978 if (rc)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003979 return rc;
3980
Steve French5a77e752018-05-09 17:43:08 -05003981 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003982 flags |= CIFS_TRANSFORM_REQ;
3983
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003984 rdata->iov[0].iov_base = buf;
3985 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003986
3987 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003988
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003989 if (rdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003990 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003991 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00003992 shdr->CreditRequest =
3993 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003994
3995 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3996 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08003997 goto async_readv_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08003998
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003999 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04004000 }
4001
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004002 kref_get(&rdata->refcount);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004003 rc = cifs_call_async(server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004004 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08004005 smb3_handle_read_data, rdata, flags,
4006 &rdata->credits);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004007 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004008 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004009 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004010 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4011 io_parms.tcon->tid,
4012 io_parms.tcon->ses->Suid,
4013 io_parms.offset, io_parms.length, rc);
4014 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004015
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004016async_readv_out:
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004017 cifs_small_buf_release(buf);
4018 return rc;
4019}
Pavel Shilovsky33319142012-09-18 16:20:29 -07004020
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004021int
4022SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4023 unsigned int *nbytes, char **buf, int *buf_type)
4024{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004025 struct smb_rqst rqst;
Colin Ian King1efd4fc2019-07-31 10:05:26 +01004026 int resp_buftype, rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08004027 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004028 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004029 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004030 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004031 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004032 int flags = CIFS_LOG_ERROR;
4033 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004034
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004035 if (!io_parms->server)
4036 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4037
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004038 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07004039 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004040 if (rc)
4041 return rc;
4042
Steve French5a77e752018-05-09 17:43:08 -05004043 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004044 flags |= CIFS_TRANSFORM_REQ;
4045
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004046 iov[0].iov_base = (char *)req;
4047 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004048
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004049 memset(&rqst, 0, sizeof(struct smb_rqst));
4050 rqst.rq_iov = iov;
4051 rqst.rq_nvec = 1;
4052
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004053 rc = cifs_send_recv(xid, ses, io_parms->server,
4054 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004055 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004056
4057 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004058 if (rc != -ENODATA) {
4059 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4060 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004061 trace_smb3_read_err(xid, req->PersistentFileId,
4062 io_parms->tcon->tid, ses->Suid,
4063 io_parms->offset, io_parms->length,
4064 rc);
Steve Frenchb0a42f22019-02-25 15:02:58 -06004065 } else
4066 trace_smb3_read_done(xid, req->PersistentFileId,
4067 io_parms->tcon->tid, ses->Suid,
4068 io_parms->offset, 0);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004069 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Ronnie Sahlberg05fd5c22019-04-23 16:39:45 +10004070 cifs_small_buf_release(req);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004071 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05004072 } else
4073 trace_smb3_read_done(xid, req->PersistentFileId,
4074 io_parms->tcon->tid, ses->Suid,
4075 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004076
ZhangXiaoxu088aaf12019-04-06 15:47:39 +08004077 cifs_small_buf_release(req);
4078
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004079 *nbytes = le32_to_cpu(rsp->DataLength);
4080 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4081 (*nbytes > io_parms->length)) {
4082 cifs_dbg(FYI, "bad length %d for count %d\n",
4083 *nbytes, io_parms->length);
4084 rc = -EIO;
4085 *nbytes = 0;
4086 }
4087
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004088 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004089 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004090 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004091 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004092 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004093 if (resp_buftype == CIFS_SMALL_BUFFER)
4094 *buf_type = CIFS_SMALL_BUFFER;
4095 else if (resp_buftype == CIFS_LARGE_BUFFER)
4096 *buf_type = CIFS_LARGE_BUFFER;
4097 }
4098 return rc;
4099}
4100
Pavel Shilovsky33319142012-09-18 16:20:29 -07004101/*
4102 * Check the mid_state and signature on received buffer (if any), and queue the
4103 * workqueue completion task.
4104 */
4105static void
4106smb2_writev_callback(struct mid_q_entry *mid)
4107{
4108 struct cifs_writedata *wdata = mid->callback_data;
4109 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004110 struct TCP_Server_Info *server = wdata->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004111 unsigned int written;
4112 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004113 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky33319142012-09-18 16:20:29 -07004114
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004115 WARN_ONCE(wdata->server != mid->server,
4116 "wdata server %p != mid server %p",
4117 wdata->server, mid->server);
4118
Pavel Shilovsky33319142012-09-18 16:20:29 -07004119 switch (mid->mid_state) {
4120 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004121 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4122 credits.instance = server->reconnect_instance;
4123 wdata->result = smb2_check_receive(mid, server, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004124 if (wdata->result != 0)
4125 break;
4126
4127 written = le32_to_cpu(rsp->DataLength);
4128 /*
4129 * Mask off high 16 bits when bytes written as returned
4130 * by the server is greater than bytes requested by the
4131 * client. OS/2 servers are known to set incorrect
4132 * CountHigh values.
4133 */
4134 if (written > wdata->bytes)
4135 written &= 0xFFFF;
4136
4137 if (written < wdata->bytes)
4138 wdata->result = -ENOSPC;
4139 else
4140 wdata->bytes = written;
4141 break;
4142 case MID_REQUEST_SUBMITTED:
4143 case MID_RETRY_NEEDED:
4144 wdata->result = -EAGAIN;
4145 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08004146 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004147 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4148 credits.instance = server->reconnect_instance;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08004149 /* fall through */
Pavel Shilovsky33319142012-09-18 16:20:29 -07004150 default:
4151 wdata->result = -EIO;
4152 break;
4153 }
Long Lidb223a52017-11-22 17:38:45 -07004154#ifdef CONFIG_CIFS_SMB_DIRECT
4155 /*
4156 * If this wdata has a memory registered, the MR can be freed
4157 * The number of MRs available is limited, it's important to recover
4158 * used MR as soon as I/O is finished. Hold MR longer in the later
4159 * I/O process can possibly result in I/O deadlock due to lack of MR
4160 * to send request on I/O retry
4161 */
4162 if (wdata->mr) {
4163 smbd_deregister_mr(wdata->mr);
4164 wdata->mr = NULL;
4165 }
4166#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004167 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07004168 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004169 trace_smb3_write_err(0 /* no xid */,
4170 wdata->cfile->fid.persistent_fid,
4171 tcon->tid, tcon->ses->Suid, wdata->offset,
4172 wdata->bytes, wdata->result);
Steve Frenchd6fd4192020-02-05 16:52:11 -06004173 if (wdata->result == -ENOSPC)
Joe Perchesa0a30362020-04-14 22:42:53 -07004174 pr_warn_once("Out of space writing to %s\n",
4175 tcon->treeName);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004176 } else
4177 trace_smb3_write_done(0 /* no xid */,
4178 wdata->cfile->fid.persistent_fid,
4179 tcon->tid, tcon->ses->Suid,
4180 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004181
4182 queue_work(cifsiod_wq, &wdata->work);
4183 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004184 add_credits(server, &credits, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004185}
4186
4187/* smb2_async_writev - send an async write, and set up mid to handle result */
4188int
Steve French4a5c80d2014-02-07 20:45:12 -06004189smb2_async_writev(struct cifs_writedata *wdata,
4190 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07004191{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004192 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004193 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004194 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004195 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004196 struct TCP_Server_Info *server = wdata->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004197 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004198 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004199 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004200
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004201 if (!wdata->server)
4202 server = wdata->server = cifs_pick_channel(tcon->ses);
4203
4204 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4205 (void **) &req, &total_len);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08004206 if (rc)
4207 return rc;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004208
Steve French5a77e752018-05-09 17:43:08 -05004209 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004210 flags |= CIFS_TRANSFORM_REQ;
4211
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004212 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004213 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004214
4215 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4216 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4217 req->WriteChannelInfoOffset = 0;
4218 req->WriteChannelInfoLength = 0;
4219 req->Channel = 0;
4220 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004221 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004222 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07004223 req->RemainingBytes = 0;
Steve Frenchd323c2462019-02-25 00:52:43 -06004224
4225 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4226 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07004227#ifdef CONFIG_CIFS_SMB_DIRECT
4228 /*
4229 * If we want to do a server RDMA read, fill in and append
4230 * smbd_buffer_descriptor_v1 to the end of write request
4231 */
Long Libb4c0412018-04-17 12:17:08 -07004232 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07004233 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07004234
Long Lidb223a52017-11-22 17:38:45 -07004235 struct smbd_buffer_descriptor_v1 *v1;
4236 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4237
4238 wdata->mr = smbd_register_mr(
4239 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07004240 wdata->nr_pages, wdata->page_offset,
4241 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07004242 if (!wdata->mr) {
Long Lib7972092019-04-05 21:36:34 +00004243 rc = -EAGAIN;
Long Lidb223a52017-11-22 17:38:45 -07004244 goto async_writev_out;
4245 }
4246 req->Length = 0;
4247 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07004248 if (wdata->nr_pages > 1)
4249 req->RemainingBytes =
4250 cpu_to_le32(
4251 (wdata->nr_pages - 1) * wdata->pagesz -
4252 wdata->page_offset + wdata->tailsz
4253 );
4254 else
4255 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07004256 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4257 if (need_invalidate)
4258 req->Channel = SMB2_CHANNEL_RDMA_V1;
4259 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06004260 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07004261 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06004262 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07004263 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06004264 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4265 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4266 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07004267 }
4268#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004269 iov[0].iov_len = total_len - 1;
4270 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004271
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004272 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004273 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07004274 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07004275 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07004276 rqst.rq_npages = wdata->nr_pages;
4277 rqst.rq_pagesz = wdata->pagesz;
4278 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07004279#ifdef CONFIG_CIFS_SMB_DIRECT
4280 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004281 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07004282 rqst.rq_npages = 0;
4283 }
4284#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05004285 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4286 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004287
Long Lidb223a52017-11-22 17:38:45 -07004288#ifdef CONFIG_CIFS_SMB_DIRECT
4289 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4290 if (!wdata->mr)
4291 req->Length = cpu_to_le32(wdata->bytes);
4292#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07004293 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07004294#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07004295
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004296 if (wdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004297 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004298 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovskyb983f7e2018-12-19 22:49:09 +00004299 shdr->CreditRequest =
4300 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004301
4302 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4303 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004304 goto async_writev_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004305
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004306 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004307 }
4308
Pavel Shilovsky33319142012-09-18 16:20:29 -07004309 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08004310 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08004311 wdata, flags, &wdata->credits);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004312
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004313 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004314 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4315 tcon->tid, tcon->ses->Suid, wdata->offset,
4316 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06004317 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004318 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004319 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07004320
Pavel Shilovsky33319142012-09-18 16:20:29 -07004321async_writev_out:
4322 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004323 return rc;
4324}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004325
4326/*
4327 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4328 * The length field from io_parms must be at least 1 and indicates a number of
4329 * elements with data to write that begins with position 1 in iov array. All
4330 * data length is specified by count.
4331 */
4332int
4333SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4334 unsigned int *nbytes, struct kvec *iov, int n_vec)
4335{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004336 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004337 int rc = 0;
4338 struct smb2_write_req *req = NULL;
4339 struct smb2_write_rsp *rsp = NULL;
4340 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004341 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004342 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004343 unsigned int total_len;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004344 struct TCP_Server_Info *server;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004345
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004346 *nbytes = 0;
4347
4348 if (n_vec < 1)
4349 return rc;
4350
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004351 if (!io_parms->server)
4352 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4353 server = io_parms->server;
4354 if (server == NULL)
4355 return -ECONNABORTED;
4356
4357 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4358 (void **) &req, &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004359 if (rc)
4360 return rc;
4361
Steve French5a77e752018-05-09 17:43:08 -05004362 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004363 flags |= CIFS_TRANSFORM_REQ;
4364
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004365 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004366
4367 req->PersistentFileId = io_parms->persistent_fid;
4368 req->VolatileFileId = io_parms->volatile_fid;
4369 req->WriteChannelInfoOffset = 0;
4370 req->WriteChannelInfoLength = 0;
4371 req->Channel = 0;
4372 req->Length = cpu_to_le32(io_parms->length);
4373 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004374 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004375 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004376 req->RemainingBytes = 0;
4377
Steve Frenchd323c2462019-02-25 00:52:43 -06004378 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4379 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4380 io_parms->offset, io_parms->length);
4381
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004382 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004383 /* 1 for Buffer */
4384 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004385
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004386 memset(&rqst, 0, sizeof(struct smb_rqst));
4387 rqst.rq_iov = iov;
4388 rqst.rq_nvec = n_vec + 1;
4389
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004390 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4391 &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004392 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004393 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004394
4395 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004396 trace_smb3_write_err(xid, req->PersistentFileId,
4397 io_parms->tcon->tid,
4398 io_parms->tcon->ses->Suid,
4399 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004400 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004401 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05004402 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004403 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05004404 trace_smb3_write_done(xid, req->PersistentFileId,
4405 io_parms->tcon->tid,
4406 io_parms->tcon->ses->Suid,
4407 io_parms->offset, *nbytes);
4408 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004409
ZhangXiaoxu6a3eb332019-04-06 15:47:38 +08004410 cifs_small_buf_release(req);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004411 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004412 return rc;
4413}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004414
Aurelien Aptel69dda302020-03-02 17:53:22 +01004415int posix_info_sid_size(const void *beg, const void *end)
Aurelien Aptel349e13a2020-02-08 15:50:57 +01004416{
4417 size_t subauth;
4418 int total;
4419
4420 if (beg + 1 > end)
4421 return -1;
4422
4423 subauth = *(u8 *)(beg+1);
4424 if (subauth < 1 || subauth > 15)
4425 return -1;
4426
4427 total = 1 + 1 + 6 + 4*subauth;
4428 if (beg + total > end)
4429 return -1;
4430
4431 return total;
4432}
4433
4434int posix_info_parse(const void *beg, const void *end,
4435 struct smb2_posix_info_parsed *out)
4436
4437{
4438 int total_len = 0;
4439 int sid_len;
4440 int name_len;
4441 const void *owner_sid;
4442 const void *group_sid;
4443 const void *name;
4444
4445 /* if no end bound given, assume payload to be correct */
4446 if (!end) {
4447 const struct smb2_posix_info *p = beg;
4448
4449 end = beg + le32_to_cpu(p->NextEntryOffset);
4450 /* last element will have a 0 offset, pick a sensible bound */
4451 if (end == beg)
4452 end += 0xFFFF;
4453 }
4454
4455 /* check base buf */
4456 if (beg + sizeof(struct smb2_posix_info) > end)
4457 return -1;
4458 total_len = sizeof(struct smb2_posix_info);
4459
4460 /* check owner sid */
4461 owner_sid = beg + total_len;
4462 sid_len = posix_info_sid_size(owner_sid, end);
4463 if (sid_len < 0)
4464 return -1;
4465 total_len += sid_len;
4466
4467 /* check group sid */
4468 group_sid = beg + total_len;
4469 sid_len = posix_info_sid_size(group_sid, end);
4470 if (sid_len < 0)
4471 return -1;
4472 total_len += sid_len;
4473
4474 /* check name len */
4475 if (beg + total_len + 4 > end)
4476 return -1;
4477 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4478 if (name_len < 1 || name_len > 0xFFFF)
4479 return -1;
4480 total_len += 4;
4481
4482 /* check name */
4483 name = beg + total_len;
4484 if (name + name_len > end)
4485 return -1;
4486 total_len += name_len;
4487
4488 if (out) {
4489 out->base = beg;
4490 out->size = total_len;
4491 out->name_len = name_len;
4492 out->name = name;
4493 memcpy(&out->owner, owner_sid,
4494 posix_info_sid_size(owner_sid, end));
4495 memcpy(&out->group, group_sid,
4496 posix_info_sid_size(group_sid, end));
4497 }
4498 return total_len;
4499}
4500
4501static int posix_info_extra_size(const void *beg, const void *end)
4502{
4503 int len = posix_info_parse(beg, end, NULL);
4504
4505 if (len < 0)
4506 return -1;
4507 return len - sizeof(struct smb2_posix_info);
4508}
4509
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004510static unsigned int
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004511num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4512 size_t size)
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004513{
4514 int len;
4515 unsigned int entrycount = 0;
4516 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03004517 char *entryptr;
4518 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004519
4520 if (bufstart == NULL)
4521 return 0;
4522
Dan Carpenter56446f22018-09-06 12:48:22 +03004523 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004524
4525 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03004526 if (entryptr + next_offset < entryptr ||
4527 entryptr + next_offset > end_of_buf ||
4528 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004529 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004530 break;
4531 }
4532
Dan Carpenter56446f22018-09-06 12:48:22 +03004533 entryptr = entryptr + next_offset;
4534 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4535
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004536 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4537 len = posix_info_extra_size(entryptr, end_of_buf);
4538 else
4539 len = le32_to_cpu(dir_info->FileNameLength);
4540
4541 if (len < 0 ||
4542 entryptr + len < entryptr ||
Dan Carpenter56446f22018-09-06 12:48:22 +03004543 entryptr + len > end_of_buf ||
4544 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004545 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4546 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004547 break;
4548 }
4549
Dan Carpenter56446f22018-09-06 12:48:22 +03004550 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004551 entrycount++;
4552
Dan Carpenter56446f22018-09-06 12:48:22 +03004553 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004554 if (!next_offset)
4555 break;
4556 }
4557
4558 return entrycount;
4559}
4560
4561/*
4562 * Readdir/FindFirst
4563 */
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004564int SMB2_query_directory_init(const unsigned int xid,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004565 struct cifs_tcon *tcon,
4566 struct TCP_Server_Info *server,
4567 struct smb_rqst *rqst,
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004568 u64 persistent_fid, u64 volatile_fid,
4569 int index, int info_level)
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004570{
4571 struct smb2_query_directory_req *req;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004572 unsigned char *bufptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004573 __le16 asteriks = cpu_to_le16('*');
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004574 unsigned int output_size = CIFSMaxBufSize -
4575 MAX_SMB2_CREATE_RESPONSE_SIZE -
4576 MAX_SMB2_CLOSE_RESPONSE_SIZE;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004577 unsigned int total_len;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004578 struct kvec *iov = rqst->rq_iov;
4579 int len, rc;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004580
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004581 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4582 (void **) &req, &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004583 if (rc)
4584 return rc;
4585
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004586 switch (info_level) {
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004587 case SMB_FIND_FILE_DIRECTORY_INFO:
4588 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004589 break;
4590 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4591 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004592 break;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004593 case SMB_FIND_FILE_POSIX_INFO:
4594 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4595 break;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004596 default:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004597 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004598 info_level);
4599 return -EINVAL;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004600 }
4601
4602 req->FileIndex = cpu_to_le32(index);
4603 req->PersistentFileId = persistent_fid;
4604 req->VolatileFileId = volatile_fid;
4605
4606 len = 0x2;
4607 bufptr = req->Buffer;
4608 memcpy(bufptr, &asteriks, len);
4609
4610 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004611 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004612 req->FileNameLength = cpu_to_le16(len);
4613 /*
4614 * BB could be 30 bytes or so longer if we used SMB2 specific
4615 * buffer lengths, but this is safe and close enough.
4616 */
4617 output_size = min_t(unsigned int, output_size, server->maxBuf);
4618 output_size = min_t(unsigned int, output_size, 2 << 15);
4619 req->OutputBufferLength = cpu_to_le32(output_size);
4620
4621 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004622 /* 1 for Buffer */
4623 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004624
4625 iov[1].iov_base = (char *)(req->Buffer);
4626 iov[1].iov_len = len;
4627
Steve Frenchd323c2462019-02-25 00:52:43 -06004628 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4629 tcon->ses->Suid, index, output_size);
4630
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004631 return 0;
4632}
4633
4634void SMB2_query_directory_free(struct smb_rqst *rqst)
4635{
4636 if (rqst && rqst->rq_iov) {
4637 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4638 }
4639}
4640
4641int
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004642smb2_parse_query_directory(struct cifs_tcon *tcon,
4643 struct kvec *rsp_iov,
4644 int resp_buftype,
4645 struct cifs_search_info *srch_inf)
4646{
4647 struct smb2_query_directory_rsp *rsp;
4648 size_t info_buf_size;
4649 char *end_of_smb;
4650 int rc;
4651
4652 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4653
4654 switch (srch_inf->info_level) {
4655 case SMB_FIND_FILE_DIRECTORY_INFO:
4656 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4657 break;
4658 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4659 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4660 break;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004661 case SMB_FIND_FILE_POSIX_INFO:
4662 /* note that posix payload are variable size */
4663 info_buf_size = sizeof(struct smb2_posix_info);
4664 break;
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004665 default:
4666 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4667 srch_inf->info_level);
4668 return -EINVAL;
4669 }
4670
4671 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4672 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4673 info_buf_size);
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004674 if (rc) {
4675 cifs_tcon_dbg(VFS, "bad info payload");
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004676 return rc;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004677 }
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004678
4679 srch_inf->unicode = true;
4680
4681 if (srch_inf->ntwrk_buf_start) {
4682 if (srch_inf->smallBuf)
4683 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4684 else
4685 cifs_buf_release(srch_inf->ntwrk_buf_start);
4686 }
4687 srch_inf->ntwrk_buf_start = (char *)rsp;
4688 srch_inf->srch_entries_start = srch_inf->last_entry =
4689 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4690 end_of_smb = rsp_iov->iov_len + (char *)rsp;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004691
4692 srch_inf->entries_in_buffer = num_entries(
4693 srch_inf->info_level,
4694 srch_inf->srch_entries_start,
4695 end_of_smb,
4696 &srch_inf->last_entry,
4697 info_buf_size);
4698
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004699 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4700 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4701 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4702 srch_inf->srch_entries_start, srch_inf->last_entry);
4703 if (resp_buftype == CIFS_LARGE_BUFFER)
4704 srch_inf->smallBuf = false;
4705 else if (resp_buftype == CIFS_SMALL_BUFFER)
4706 srch_inf->smallBuf = true;
4707 else
Joe Perchesa0a30362020-04-14 22:42:53 -07004708 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004709
4710 return 0;
4711}
4712
4713int
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004714SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4715 u64 persistent_fid, u64 volatile_fid, int index,
4716 struct cifs_search_info *srch_inf)
4717{
4718 struct smb_rqst rqst;
4719 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4720 struct smb2_query_directory_rsp *rsp = NULL;
4721 int resp_buftype = CIFS_NO_BUFFER;
4722 struct kvec rsp_iov;
4723 int rc = 0;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004724 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004725 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004726 int flags = 0;
4727
YueHaibingc4985c32020-01-17 10:57:17 +08004728 if (!ses || !(ses->server))
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004729 return -EIO;
4730
4731 if (smb3_encryption_required(tcon))
4732 flags |= CIFS_TRANSFORM_REQ;
4733
4734 memset(&rqst, 0, sizeof(struct smb_rqst));
4735 memset(&iov, 0, sizeof(iov));
4736 rqst.rq_iov = iov;
4737 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4738
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004739 rc = SMB2_query_directory_init(xid, tcon, server,
4740 &rqst, persistent_fid,
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004741 volatile_fid, index,
4742 srch_inf->info_level);
4743 if (rc)
4744 goto qdir_exit;
4745
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004746 rc = cifs_send_recv(xid, ses, server,
4747 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004748 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004749
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004750 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004751 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004752 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004753 trace_smb3_query_dir_done(xid, persistent_fid,
4754 tcon->tid, tcon->ses->Suid, index, 0);
Pavel Shilovsky52755802014-08-18 20:49:57 +04004755 srch_inf->endOfSearch = true;
4756 rc = 0;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004757 } else {
4758 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4759 tcon->ses->Suid, index, 0, rc);
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08004760 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004761 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004762 goto qdir_exit;
4763 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004764
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004765 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4766 srch_inf);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004767 if (rc) {
4768 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4769 tcon->ses->Suid, index, 0, rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004770 goto qdir_exit;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004771 }
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004772 resp_buftype = CIFS_NO_BUFFER;
4773
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004774 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4775 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004776
4777qdir_exit:
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004778 SMB2_query_directory_free(&rqst);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004779 free_rsp_buf(resp_buftype, rsp);
4780 return rc;
4781}
4782
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004783int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004784SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4785 struct smb_rqst *rqst,
4786 u64 persistent_fid, u64 volatile_fid, u32 pid,
4787 u8 info_class, u8 info_type, u32 additional_info,
4788 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004789{
4790 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004791 struct kvec *iov = rqst->rq_iov;
4792 unsigned int i, total_len;
4793 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004794
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004795 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
4796 (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004797 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004798 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004799
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004800 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004801 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004802 req->FileInfoClass = info_class;
4803 req->PersistentFileId = persistent_fid;
4804 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004805 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004806
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004807 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004808 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004809 req->BufferLength = cpu_to_le32(*size);
4810
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004811 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004812 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004813
4814 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004815 /* 1 for Buffer */
4816 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004817
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004818 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004819 le32_add_cpu(&req->BufferLength, size[i]);
4820 iov[i].iov_base = (char *)data[i];
4821 iov[i].iov_len = size[i];
4822 }
4823
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004824 return 0;
4825}
4826
4827void
4828SMB2_set_info_free(struct smb_rqst *rqst)
4829{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10004830 if (rqst && rqst->rq_iov)
4831 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004832}
4833
4834static int
4835send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4836 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4837 u8 info_type, u32 additional_info, unsigned int num,
4838 void **data, unsigned int *size)
4839{
4840 struct smb_rqst rqst;
4841 struct smb2_set_info_rsp *rsp = NULL;
4842 struct kvec *iov;
4843 struct kvec rsp_iov;
4844 int rc = 0;
4845 int resp_buftype;
4846 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004847 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004848 int flags = 0;
4849
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004850 if (!ses || !server)
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004851 return -EIO;
4852
4853 if (!num)
4854 return -EINVAL;
4855
4856 if (smb3_encryption_required(tcon))
4857 flags |= CIFS_TRANSFORM_REQ;
4858
4859 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4860 if (!iov)
4861 return -ENOMEM;
4862
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004863 memset(&rqst, 0, sizeof(struct smb_rqst));
4864 rqst.rq_iov = iov;
4865 rqst.rq_nvec = num;
4866
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004867 rc = SMB2_set_info_init(tcon, server,
4868 &rqst, persistent_fid, volatile_fid, pid,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004869 info_class, info_type, additional_info,
4870 data, size);
4871 if (rc) {
4872 kfree(iov);
4873 return rc;
4874 }
4875
4876
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004877 rc = cifs_send_recv(xid, ses, server,
4878 &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004879 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004880 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004881 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004882
Steve Frencheccb4422018-05-17 21:16:55 -05004883 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004884 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05004885 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4886 ses->Suid, info_class, (__u32)info_type, rc);
4887 }
Steve French7d3fb242013-11-18 09:56:28 -06004888
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004889 free_rsp_buf(resp_buftype, rsp);
4890 kfree(iov);
4891 return rc;
4892}
4893
4894int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004895SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004896 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004897{
4898 struct smb2_file_eof_info info;
4899 void *data;
4900 unsigned int size;
4901
4902 info.EndOfFile = *eof;
4903
4904 data = &info;
4905 size = sizeof(struct smb2_file_eof_info);
4906
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10004907 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004908 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4909 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004910}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004911
4912int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004913SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4914 u64 persistent_fid, u64 volatile_fid,
4915 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4916{
4917 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4918 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4919 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004920}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004921
4922int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004923SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4924 u64 persistent_fid, u64 volatile_fid,
4925 struct smb2_file_full_ea_info *buf, int len)
4926{
4927 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4928 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4929 0, 1, (void **)&buf, &len);
4930}
4931
4932int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004933SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4934 const u64 persistent_fid, const u64 volatile_fid,
4935 __u8 oplock_level)
4936{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004937 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004938 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10004939 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004940 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004941 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004942 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004943 unsigned int total_len;
4944 struct kvec iov[1];
4945 struct kvec rsp_iov;
4946 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004947
Joe Perchesf96637b2013-05-04 22:12:25 -05004948 cifs_dbg(FYI, "SMB2_oplock_break\n");
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004949 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
4950 (void **) &req, &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004951 if (rc)
4952 return rc;
4953
Steve French5a77e752018-05-09 17:43:08 -05004954 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004955 flags |= CIFS_TRANSFORM_REQ;
4956
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004957 req->VolatileFid = volatile_fid;
4958 req->PersistentFid = persistent_fid;
4959 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004960 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004961
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10004962 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11004963
4964 iov[0].iov_base = (char *)req;
4965 iov[0].iov_len = total_len;
4966
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004967 memset(&rqst, 0, sizeof(struct smb_rqst));
4968 rqst.rq_iov = iov;
4969 rqst.rq_nvec = 1;
4970
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004971 rc = cifs_send_recv(xid, ses, server,
4972 &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004973 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004974
4975 if (rc) {
4976 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004977 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004978 }
4979
4980 return rc;
4981}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004982
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10004983void
4984smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4985 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004986{
4987 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4988 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4989 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05304990 kst->f_bfree = kst->f_bavail =
4991 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004992 return;
4993}
4994
Steve French2d304212018-06-24 23:28:12 -05004995static void
4996copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4997 struct kstatfs *kst)
4998{
4999 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5000 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5001 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5002 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5003 kst->f_bavail = kst->f_bfree;
5004 else
5005 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5006 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5007 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5008 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5009 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5010
5011 return;
5012}
Steve French2d304212018-06-24 23:28:12 -05005013
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005014static int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005015build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5016 struct TCP_Server_Info *server,
5017 int level, int outbuf_len, u64 persistent_fid,
5018 u64 volatile_fid)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005019{
5020 int rc;
5021 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11005022 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005023
Joe Perchesf96637b2013-05-04 22:12:25 -05005024 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005025
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005026 if ((tcon->ses == NULL) || server == NULL)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005027 return -EIO;
5028
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005029 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5030 (void **) &req, &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005031 if (rc)
5032 return rc;
5033
5034 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5035 req->FileInfoClass = level;
5036 req->PersistentFileId = persistent_fid;
5037 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11005038 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005039 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11005040 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005041 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005042 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005043
5044 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11005045 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005046 return 0;
5047}
5048
Steve French2d304212018-06-24 23:28:12 -05005049int
5050SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5051 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5052{
5053 struct smb_rqst rqst;
5054 struct smb2_query_info_rsp *rsp = NULL;
5055 struct kvec iov;
5056 struct kvec rsp_iov;
5057 int rc = 0;
5058 int resp_buftype;
5059 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005060 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Steve French2d304212018-06-24 23:28:12 -05005061 FILE_SYSTEM_POSIX_INFO *info = NULL;
5062 int flags = 0;
5063
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005064 rc = build_qfs_info_req(&iov, tcon, server,
5065 FS_POSIX_INFORMATION,
Steve French2d304212018-06-24 23:28:12 -05005066 sizeof(FILE_SYSTEM_POSIX_INFO),
5067 persistent_fid, volatile_fid);
5068 if (rc)
5069 return rc;
5070
5071 if (smb3_encryption_required(tcon))
5072 flags |= CIFS_TRANSFORM_REQ;
5073
5074 memset(&rqst, 0, sizeof(struct smb_rqst));
5075 rqst.rq_iov = &iov;
5076 rqst.rq_nvec = 1;
5077
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005078 rc = cifs_send_recv(xid, ses, server,
5079 &rqst, &resp_buftype, flags, &rsp_iov);
Steve French2d304212018-06-24 23:28:12 -05005080 cifs_small_buf_release(iov.iov_base);
5081 if (rc) {
5082 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5083 goto posix_qfsinf_exit;
5084 }
5085 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5086
5087 info = (FILE_SYSTEM_POSIX_INFO *)(
5088 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005089 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5090 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5091 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05005092 if (!rc)
5093 copy_posix_fs_info_to_kstatfs(info, fsdata);
5094
5095posix_qfsinf_exit:
5096 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5097 return rc;
5098}
Steve French2d304212018-06-24 23:28:12 -05005099
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005100int
5101SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5102 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5103{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005104 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005105 struct smb2_query_info_rsp *rsp = NULL;
5106 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005107 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005108 int rc = 0;
5109 int resp_buftype;
5110 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005111 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005112 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005113 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005114
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005115 rc = build_qfs_info_req(&iov, tcon, server,
5116 FS_FULL_SIZE_INFORMATION,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005117 sizeof(struct smb2_fs_full_size_info),
5118 persistent_fid, volatile_fid);
5119 if (rc)
5120 return rc;
5121
Steve French5a77e752018-05-09 17:43:08 -05005122 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005123 flags |= CIFS_TRANSFORM_REQ;
5124
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005125 memset(&rqst, 0, sizeof(struct smb_rqst));
5126 rqst.rq_iov = &iov;
5127 rqst.rq_nvec = 1;
5128
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005129 rc = cifs_send_recv(xid, ses, server,
5130 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005131 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005132 if (rc) {
5133 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05005134 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005135 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005136 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005137
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005138 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10005139 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005140 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5141 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5142 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005143 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005144 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005145
Steve French34f62642013-10-09 02:07:00 -05005146qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005147 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05005148 return rc;
5149}
5150
5151int
5152SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05005153 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05005154{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005155 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05005156 struct smb2_query_info_rsp *rsp = NULL;
5157 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005158 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05005159 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05005160 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05005161 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005162 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Steve French34f62642013-10-09 02:07:00 -05005163 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005164 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05005165
Steven French21671142013-10-09 13:36:35 -05005166 if (level == FS_DEVICE_INFORMATION) {
5167 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5168 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5169 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5170 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5171 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005172 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5173 max_len = sizeof(struct smb3_fs_ss_info);
5174 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05005175 } else if (level == FS_VOLUME_INFORMATION) {
5176 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5177 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05005178 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005179 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05005180 return -EINVAL;
5181 }
5182
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005183 rc = build_qfs_info_req(&iov, tcon, server,
5184 level, max_len,
Steve French34f62642013-10-09 02:07:00 -05005185 persistent_fid, volatile_fid);
5186 if (rc)
5187 return rc;
5188
Steve French5a77e752018-05-09 17:43:08 -05005189 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005190 flags |= CIFS_TRANSFORM_REQ;
5191
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005192 memset(&rqst, 0, sizeof(struct smb_rqst));
5193 rqst.rq_iov = &iov;
5194 rqst.rq_nvec = 1;
5195
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005196 rc = cifs_send_recv(xid, ses, server,
5197 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005198 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05005199 if (rc) {
5200 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5201 goto qfsattr_exit;
5202 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005203 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05005204
5205 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5206 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005207 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05005208 if (rc)
5209 goto qfsattr_exit;
5210
5211 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005212 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10005213 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05005214 rsp_len, max_len));
5215 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005216 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10005217 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005218 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5219 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005220 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005221 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5222 tcon->perf_sector_size =
5223 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05005224 } else if (level == FS_VOLUME_INFORMATION) {
5225 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5226 (offset + (char *)rsp);
5227 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5228 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005229 }
Steve French34f62642013-10-09 02:07:00 -05005230
5231qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005232 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005233 return rc;
5234}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005235
5236int
5237smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5238 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5239 const __u32 num_lock, struct smb2_lock_element *buf)
5240{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005241 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005242 int rc = 0;
5243 struct smb2_lock_req *req = NULL;
5244 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005245 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005246 int resp_buf_type;
5247 unsigned int count;
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10005248 int flags = CIFS_NO_RSP_BUF;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005249 unsigned int total_len;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005250 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005251
Joe Perchesf96637b2013-05-04 22:12:25 -05005252 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005253
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005254 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5255 (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005256 if (rc)
5257 return rc;
5258
Steve French5a77e752018-05-09 17:43:08 -05005259 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005260 flags |= CIFS_TRANSFORM_REQ;
5261
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005262 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005263 req->LockCount = cpu_to_le16(num_lock);
5264
5265 req->PersistentFileId = persist_fid;
5266 req->VolatileFileId = volatile_fid;
5267
5268 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005269
5270 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005271 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005272 iov[1].iov_base = (char *)buf;
5273 iov[1].iov_len = count;
5274
5275 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005276
5277 memset(&rqst, 0, sizeof(struct smb_rqst));
5278 rqst.rq_iov = iov;
5279 rqst.rq_nvec = 2;
5280
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005281 rc = cifs_send_recv(xid, tcon->ses, server,
5282 &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005283 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005284 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005285 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005286 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005287 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05005288 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5289 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005290 }
5291
5292 return rc;
5293}
5294
5295int
5296SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5297 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5298 const __u64 length, const __u64 offset, const __u32 lock_flags,
5299 const bool wait)
5300{
5301 struct smb2_lock_element lock;
5302
5303 lock.Offset = cpu_to_le64(offset);
5304 lock.Length = cpu_to_le64(length);
5305 lock.Flags = cpu_to_le32(lock_flags);
5306 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5307 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5308
5309 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5310}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005311
5312int
5313SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5314 __u8 *lease_key, const __le32 lease_state)
5315{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005316 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005317 int rc;
5318 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005319 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005320 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005321 unsigned int total_len;
5322 struct kvec iov[1];
5323 struct kvec rsp_iov;
5324 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05005325 __u64 *please_key_high;
5326 __u64 *please_key_low;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005327 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005328
Joe Perchesf96637b2013-05-04 22:12:25 -05005329 cifs_dbg(FYI, "SMB2_lease_break\n");
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005330 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5331 (void **) &req, &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005332 if (rc)
5333 return rc;
5334
Steve French5a77e752018-05-09 17:43:08 -05005335 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005336 flags |= CIFS_TRANSFORM_REQ;
5337
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005338 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005339 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005340 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005341
5342 memcpy(req->LeaseKey, lease_key, 16);
5343 req->LeaseState = lease_state;
5344
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10005345 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005346
5347 iov[0].iov_base = (char *)req;
5348 iov[0].iov_len = total_len;
5349
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005350 memset(&rqst, 0, sizeof(struct smb_rqst));
5351 rqst.rq_iov = iov;
5352 rqst.rq_nvec = 1;
5353
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005354 rc = cifs_send_recv(xid, ses, server,
5355 &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005356 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005357
Aurelien Apteld339adc2019-01-31 13:46:07 +01005358 please_key_low = (__u64 *)lease_key;
5359 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005360 if (rc) {
5361 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05005362 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5363 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05005364 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05005365 } else
5366 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5367 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005368
5369 return rc;
5370}